问题描述
我有一个这样的HTML表单:
I have an HTML form like this:
form.html:
form.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:
The servlet to serve this request:
post2.class:
post2.class:
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方法。请帮忙,我完全不知道为什么它不能正常工作......
what is wrong ? For me it seems that the servlet don't see post method from form. Please help, Im completly have no idea why it not working properly...
来自wireshark的结果:
the result from the 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.456916 192.168.1.100 87.105.184.89 HTTP 239 HTTP / 1.1 302已移动
暂时
953 379.456916 192.168.1.100 87.105.184.89 HTTP 239 HTTP/1.1 302 Moved Temporarily
955 379.462518 192.168.1.100 87.105.184.89 HTTP 470 GET / post2 /
HTTP / 1.1
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 OK
(text / html)
957 379.463979 192.168.1.100 87.105.184.89 HTTP 431 HTTP/1.1 200 OK (text/html)
路由逻辑:
tomcat \ webapps\ROOT\form.html
- > \ tomcat\webapps \ postt2 \ WEB-INF \classes \ postt2.class
推荐答案
这可能是由于重定向造成的吗?如果/ post2重定向到/ post2 /,您的POST请求将转换为GET,丢失所有发布数据。
Could this be due to a redirect? If "/post2" redirects to "/post2/" your POST request would be transformed into a GET, losing all post data.
尝试直接访问/ post2你的浏览器,看看会发生什么。
Try accessing "/post2" directly in your browser and see what happens.
这篇关于HTML表单方法POST调用java servlet的doGet方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!