Here we are creating a Servlet application using which the user can login with the username and the password and if the password or the username is wrong then the program will show Authentication Failure else if successful login it will display Login Successful.
Java Program -
index.html -
<!DOCTYPE html>
<html>
<head>
<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>
<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();
String result;
try{
String username = request.getParameter("username");
String password = request.getParameter("password");
if(username.equals("manthan")&&password.equals("manthan23"))
{
result="Login Successful";
}
else
{
result="Authentication failure";
}
out.println("<h2>"+result+"</h2>");
}
finally
{
out.close();
}
}
}
Output -
0 Comments