简体   繁体   中英

HTML form method POST calls java servlet doGet method

I have an HTML form like this:

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:

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. Please help, Im completly have no idea why it not working properly...

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 Moved Temporarily

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)

routing logic:

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.

Try accessing "/post2" directly in your browser and see what happens.

You need to add a value attribute to the input tag:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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