Search code examples
javajsppostservletsget

POST request to servlet returns result of GET method


Background:

I am writing a servlet application and would like to have a servlet handle GET and POST requests. The servlet is built into a WAR file and hosted locally using a Tomcat server on my development machine (localhost:8080) for testing.

The GET requests to the servlet work fine (returns the index.jsp page), whereas, POSTing to the same url does not run the code in the doPost() method (also returns the index.jsp page).

This is my servlet class code:

public class SelfserviceServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        RequestDispatcher view = request.getRequestDispatcher("/WEB-INF/index.jsp");
        view.forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String ip_address = request.getParameter("someparam");
        String port = request.getParameter("someotherparam");

        // Do some operation here to produce html_output

        response.setContentType("text/plain");
        response.setCharacterEncoding("UTF-8");
        response.getWriter().write(html_output.toString());
    }
}

This is my web.xml file:

    <servlet>
        <servlet-name>SelfservicePortal</servlet-name>
        <servlet-class>com.somename.module.SelfserviceServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>SelfservicePortal</servlet-name>
        <url-pattern>/portal</url-pattern>
    </servlet-mapping>

EDIT: This is the index.jsp file:

<html>
<head>
<script>
    function sendPOST(){
        // First URL try
        $.ajax({
                url: "/SelfservicePortal",
                type: "POST",
                contentType: "application/x-www-form-urlencoded",
                data: "someparam=" + encodeURIComponent(someparam) + "&someotherparam=" + encodeURIComponent(someotherparam),
                success: function(data) {
                    alert(data);
                },
                error: function() {
                    alert("Error");
                }
            });
        // Second URL try
        $.ajax({
                url: "/SelfservicePortal/portal/",
                type: "POST",
                contentType: "application/x-www-form-urlencoded",
                data: "someparam=" + encodeURIComponent(someparam) + "&someotherparam=" + encodeURIComponent(someotherparam),
                success: function(data) {
                    alert(data);
                },
                error: function() {
                    alert("Error");
                }
            });
}
</script>
<body>
     <button type="button" onclick="sendPOST();">Send POST</button>
</body>
</html>

These are my results:

GET localhost:8080/SelfservicePortal -> index.jsp

GET localhost:8080/SelfservicePortal/portal -> 404 Not Found

POST localhost:8080/SelfservicePortal -> index.jsp [ THIS SHOULD BE html_output ]

POST localhost:8080/SelfservicePortal/portal -> 404 Not Found

How do I get the POST requests to work correctly and why do the requests to the url-mapping pattern return a 404?


Solution

  • I think you forgot to add annotations in your servlet try using below code and see if it works or not. also add @MultipartConfig if you want to use form data instead of url-encoded data.

        @WebServlet("/portal")    
        @MultipartConfig
        public class SelfserviceServlet extends HttpServlet {
    
                @Override
                protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                    RequestDispatcher view = request.getRequestDispatcher("/WEB-INF/index.jsp");
                    view.forward(request, response);
                }
    
                @Override
                protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                    String ip_address = request.getParameter("someparam");
                    String port = request.getParameter("someotherparam");
    
                    // Do some operation here to produce html_output
    
                    response.setContentType("text/plain");
                    response.setCharacterEncoding("UTF-8");
                    response.getWriter().write(html_output.toString());
                }
            }