我有一个这样的HTML表单:
表单.html:
<html>
<body>
your name is :<br><br>
<form ACTION="../post2" METHOD="POST">
<input name="name" type="text" id="name"/>
<input name="send" type="submit" value="send"/>
</form>
</body>
<html>
服务此请求的servlet:
post2.等级:
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.*;
public class post2 extends HttpServlet
{
protected void doDo(HttpServletRequest request,HttpServletResponse response)
throws IOException{
String name = request.getParameter("name");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<HTML><BODY>");
out.println("<H2>hello "+name+"</H2>");
out.println("<BR><BR>");
out.println("info:");
out.println("<BR><BR>");
out.println("<H2>metoda GET</H2>");
out.println("<BR><BR>");
out.println("SERVER_NAME="+request.getServerName()+"<BR>");
out.println("REQUEST_METHOD="+request.getMethod()+"<BR>");
out.println("QUERY_STRING="+request.getQueryString()+"<BR>");
out.println("REMOTE_HOST="+request.getRemoteHost()+"<BR>");
out.println("REMOTE_ADDR="+request.getRemoteAddr());
out.println("</BODY></HTML>");
}
@Override
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws IOException {
doDo(request,response);
}
@Override
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws IOException {
doDo(request,response);
}
}
结果是:
hello null
info:
SERVER_NAME=localhost
REQUEST_METHOD=GET
QUERY_STRING=null
REMOTE_HOST=127.0.0.1
REMOTE_ADDR=127.0.0.1
怎么了?对我来说,servlet似乎看不到表单中的post方法。请帮忙,我完全不知道为什么它不能正常工作。。。
wireshark的结果是:
648 126.229267 87.105.184.89 192.168.1.100 HTTP 557 POST/post2
HTTP/1.1(application/x-www-form-urlencoded)
953 379.4569192.168.1.100 87.105.184.89 HTTP 239 HTTP/1.1 302已移动
暂时
955 379.462518 192.168.1.100 87.105.184.89 HTTP 470 GET/post2/
HTTP/1.1协议
957 379.463979 192.168.1.100 87.105.184.89 HTTP 431 HTTP/1.1 200确定
(文本/html)
路由逻辑:
tomcat\webapps\ROOT\form.html
-->\tomcat\webapps\post2\WEB-INF\classes\post2.class
最佳答案
这可能是因为重定向吗?如果“/post2”重定向到“/post2/”,则POST请求将转换为GET,从而丢失所有POST数据。
尝试直接在浏览器中访问“/post2”,看看会发生什么。