Search code examples
jspservletshttp-status-code-404

Why i am getting 404 found error in JSP&servlets in IntelliJ IDEA Ultimate 2020.2?


I am creating simple Login web app in jsp and servlet in IntelliJ IDEA Ultimate 2020.2.

The following is my index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Calculator</title>
</head>
<body>
<form method="post" action="/LoginServlet">
    UerName:<input type="text" name="t1"><br>
    Password:<input type="text" name="t2"><br>
    <input type="submit" value="Ok"><br>
</form>
</body>
</html>

This is my servlet. It is located at src->main->java->francis

@WebServlet(name = "LoginServlet")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        out.println("Welcome.....");
    }
}

Web.xml is

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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_4_0.xsd"
         version="4.0">

    <servlet>
        <servlet-name>LoginServlet</servlet-name>
        <servlet-class>francis.LoginServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>LoginServlet</servlet-name>
        <url-pattern>/LoginServlet</url-pattern>
    </servlet-mapping>

</web-app>

But the Output screen is...

My Output Screen

Please help me. Thanks in advance


Solution

  • Replace

    <form method="post" action="/LoginServlet">
    

    with

    <form method="post" action="LoginServlet">
    

    which will be automatically be translated into the-context-path/LoginServlet. Note that the path you put into action attribute is relative to the context path.