Here we have created a Servlet application in which if a user logged in successfully then he will be redirected to the welcome page which will display the user credentials and if their is a authentication failure then it will be redirected to the error page.
Java Program -
index.html -
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form method="post" action="login">
Username: <input type="text" name="username"><br><br>
Password: <input type="password" name="password"><br><br>
<input type="submit" value="Login">
</form>
</body>
</html>
web.xml -
<?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_3_1.xsd"
version="3.1">
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>LoginClass</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>UserProfileServlet</servlet-name>
<servlet-class>UserProfile</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UserProfileServlet</servlet-name>
<url-pattern>/WelcomeUser</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
LoginClass.java -
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class LoginClass extends HttpServlet{
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try{
String username = request.getParameter("username");
String password = request.getParameter("password");
if(username.equals("manthan")&& password.equals("manthan23"))
{
RequestDispatcher rd = request.getRequestDispatcher("/WelcomeUser");
rd.forward(request,response);
}
else
{
RequestDispatcher rd = request.getRequestDispatcher("/errorpage.html");
rd.forward(request,response);
}
}
finally
{
out.close();
}
}
}
UserProfile.java -
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class UserProfile extends HttpServlet{
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try{
String username = request.getParameter("username");
String password = request.getParameter("password");
out.println("Username > "+username);
out.println("<br>");
out.println("Password > "+password);
}
finally{
out.close();
}
}
}
errorpage.html -
<!DOCTYPE html>
<html>
<head>
<title>Error Page</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<h2>Authentication Failure</h2>
</body>
</html>
Output -
Here we entered the username and password -
If successful login the user credentials will be displayed -
If user has entered wrong user credentials -
User will be redirected to the Authentication Failure page -
If you have any queries feel free to comment:)
0 Comments