0

I am trying to create a simple login page using JSP and servlets. When I click on the login button after entering admin and admin, it gives me an error 404 and does not open the success.jsp page.

The code for login.jsp and LoginController.java(servlet) is below. Please help me.

PS: I am using a mac and eclipse IDE.

login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<form action="LoginController" method="post">
Enter username :<input type="text" name="username"> <br>
Enter password :<input type="password" name="password"><br>
<input type="submit" value="Login">
</form>
</body>
</html>

LoginController.java

            import java.io.IOException;
            import javax.servlet.ServletException;
            import javax.servlet.http.HttpServlet;
            import javax.servlet.http.HttpServletRequest;
            import javax.servlet.http.HttpServletResponse;
            /**
             * Servlet implementation class LoginController
             */
            public class LoginController extends HttpServlet {

                protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                    String un=request.getParameter("username");
                    String pw=request.getParameter("password");

                    if(un.equals("admin") && pw.equals("admin"))
                    {
                        response.sendRedirect("success.jsp");
                        return;
                    }
                    else
                    {
                        response.sendRedirect("error.html");
                        return;
                    }
                }
            }

web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
      <display-name>Login</display-name>
      <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
      </welcome-file-list>
    </web-app>
Ashwani Jha
  • 355
  • 5
  • 11

0 Answers0