HTML form method POST calls java servlet doGet method

29,079

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.

Share:
29,079
mynameismarcin
Author by

mynameismarcin

Updated on May 31, 2020

Comments

  • mynameismarcin
    mynameismarcin almost 4 years

    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