简体   繁体   English

HTML表单方法POST调用Java servlet doGet方法

[英]HTML form method POST calls java servlet doGet method

I have an HTML form like this: 我有这样的HTML表单:

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>

The servlet to serve this request: 服务此请求的servlet:

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);
}

}

and the result is : 结果是:

hello null


info:

SERVER_NAME=localhost
REQUEST_METHOD=GET
QUERY_STRING=null
REMOTE_HOST=127.0.0.1
REMOTE_ADDR=127.0.0.1 

what is wrong ? 怎么了 ? For me it seems that the servlet don't see post method from form. 对我来说,该servlet似乎看不到来自表单的post方法。 Please help, Im completly have no idea why it not working properly... 请帮助,我完全不知道为什么它不能正常工作...

the result from the wireshark: Wireshark的结果:

648 126.229267 87.105.184.89 192.168.1.100 HTTP 557 POST /post2 HTTP/1.1 (application/x-www-form-urlencoded) 648 126.229267 87.105.184.89 192.168.1.100 HTTP 557 POST / post2 HTTP / 1.1(应用程序/ x-www-form-urlencoded)

953 379.456916 192.168.1.100 87.105.184.89 HTTP 239 HTTP/1.1 302 Moved Temporarily 953 379.456916 192.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 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(文本/ html)

routing logic: 路由逻辑:

tomcat\\webapps\\ROOT\\form.html --> \\tomcat\\webapps\\post2\\WEB-INF\\classes\\post2.class tomcat\\webapps\\ROOT\\form.html > \\tomcat\\webapps\\post2\\WEB-INF\\classes\\post2.class

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”重定向到“ / post2 /”,则您的POST请求将转换为GET,从而丢失所有发布数据。

Try accessing "/post2" directly in your browser and see what happens. 尝试直接在浏览器中访问“ / post2”,看看会发生什么。

You need to add a value attribute to the input tag: 您需要向输入标签添加一个value属性:

<input name="name" type="text" id="name" value=""/>

And put (enter by typing) a value in there - by default blank fields are not submitted. 并在其中输入值(输入)-默认情况下,不提交空白字段。

For simplicity, try this, which pre-sets the value: 为简单起见,请尝试以下操作,该操作可预先设置值:

<input name="name" type="text" id="name" value="john"/>

发生null问题是因为您没有将表单提交到服务器。

这可以解决您的问题:

String name = request.getParameter("name").toString();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM