Search code examples
javatomcatservletsnetbeanshttp-status-code-404

404 error in tomcat, Java Servlet, Apache Netbeans


I am beginner in Java servlets, whenever I try to submit a simple form (which is supposed to be handled by a servlet) it gives me a 404 error

Error Message:

enter image description here

Project directory structure:

Image

index.html:

<!DOCTYPE html>
<html>
    <head>
        <title>Start Page</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>
        <h1><a href="AddServlet">Click here!</a></h1>
        <form action="/AddServlet">
            Enter Number 1: <input type="number" name="num1" ><br /><br />
            Enter Number 2: <input type="number" name="num2" ><br /><br />
            <input type="submit" name="submit" >
        </form>
    </body>
</html>

AddServlet.java:

package mycompany.webapplication;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class AddServlet extends HttpServlet {
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            /* TODO output your page here. You may use following sample code. */
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet AddServlet</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet AddServlet at " + request.getContextPath() + "</h1>");
            out.println("</body>");
            out.println("</html>");
        }
    }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
        int i = Integer.parseInt(request.getParameter("num1"));
        int j = Integer.parseInt(request.getParameter("num2"));
        int k = i + j;
        PrintWriter out = response.getWriter();
        out.println("result is " + k);
    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
    @Override
    public String getServletInfo() {
        return "Short description";
    }
}

index.html Output:

enter image description here

But if I click on the link that says "Click Here" it goes to AddServlet.java as expected

enter image description here

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <servlet>
        <servlet-name>AddServlet</servlet-name>
        <servlet-class>mycompany.webapplication.AddServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>AddServlet</servlet-name>
        <url-pattern>/AddServlet</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
</web-app>

Solution

  • You use a absolute URI path /AddServlet, but the correct path is of the form /<webapp>/AddServlet, where <webapp> is the name of your applications context.

    Since you most certainly don't want to hardcode the name of your application, use a relative URI path AddServlet.