javaservlet-3.0servlet-mapping

The code is not running after clicking on submit it show 404. I wright similer code on my desktop in my desktop it is running. can any one help me out


<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <form action="/details">
        First name: <input type="text" name="fname"><br> 
        Last name: <input type="text" name="lname"><br>
        <br> <input type="submit" value="Submit">
    </form>
</body>
</html>
@WebServlet("/details")  
public class ShowDetails extends HttpServlet {
    
    /**
     * 
     */
    private static final long serialVersionUID = 7384650098624517113L;

    public void service(HttpServletRequest request, HttpServletResponse response) {
        String fName = request.getParameter("fname");
        String lName = request.getParameter("lname");
        
        System.out.println(fName + " " + lName );
        
    }

}

after clicking submit enter image description here

it must print the value on console enter image description here

HTML page enter image description here


Solution

  • Change <form action="/details"> to <form action="/DemoServlet/details">.

    Since the servlet path is relative to /DemoServlet (you can set this in web.xml), the ShowDetails servlet will be at localhost:2222/DemoServlet/details and not just localhost:2222/details.