日期:2014-05-18  浏览次数:20628 次

为什么会出现如此奇怪的问题,求助!
登陆页面:login.jsp 其中定义一个表单
<form id="form1" name="form1" method="get" action="porttraservlet">
<input type="text" name="name" />
<input type="password" name="psw" />
</form>

porttraservlet对应的Servlet为 PorttraServlet 源文件如下:
package ht;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class PorttraServlet extends HttpServlet
{
public void doPost(HttpServletRequest req,HttpServletResponse resp)
  throws ServletException, java.io.IOException
{
doGet(req,resp);
}

public void doGet(HttpServletRequest req,HttpServletResponse resp)
throws ServletException, java.io.IOException
{

String name=req.getParameter("name");
String password=req.getParameter("psw");


ServletContext sc=getServletContext();
RequestDispatcher rd=sc.getRequestDispatcher("/count.html");
rd.forward(req,resp);
}

}

login.jsp的form中,如果将提交动作为get 那么porttraservlet能正常执行,rd.forward(req,resp)可以正常的执行转发请求操作。
但是当我把from 中,提交动作改为post后,提交页面时则会报错:
type Status report

message HTTP method POST is not supported by this URL

description The specified HTTP method is not allowed for the requested resource (HTTP method POST is not supported by this URL).

并且,我如果稍微把PorttraServlet 中的内容改一改:

ServletContext sc=getServletContext();
RequestDispatcher rd=sc.getRequestDispatcher("/count.html");
rd.forward(req,resp);
这段不要,改为:
  resp.setContentType("text/html;charset=gb2312");
PrintWriter out=resp.getWriter();
out.write("<html><title>登录页面</title><body>");
  out.println("欢迎您光临"+name+"</body><html>");
out.close();

当login页面提交(还是post)后,则会正常的显示页面(证明doPost方法起作用了)!

所以,请问一下,这个问题是怎么出现的,急盼回复!
   



------解决方案--------------------
问题在这儿
RequestDispatcher rd=sc.getRequestDispatcher("/count.html");
rd.forward(req,resp); 

你转向的是一个静态页面,不能用foward。
很显然错误消息也告诉你了,这个url不支持post。
所以你应该用的是redirect