Let's create the required pages for the application. The Login.jsp page allows a user to enter a User Email ID and Password , which are stored in a database.
Login.jsp
<!DOCTYPE html>
<!--[if lt IE 7 ]> <html lang="en" class="ie6 ielt8"> <![endif]-->
<!--[if IE 7 ]> <html lang="en" class="ie7 ielt8"> <![endif]-->
<!--[if IE 8 ]> <html lang="en" class="ie8"> <![endif]-->
<!--[if (gte IE 9)|!(IE)]><!--> <html lang="en"> <!--<![endif]-->
<head>
<meta charset="utf-8">
<title>Login</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<div class="container">
<section id="content">
<form action="/LoginPage.jsp">
<h1>Login Form</h1>
<div>
<input type="text" placeholder="Email_ID" required="" id="username" name="username" />
</div>
<div>
<input type="password" placeholder="Password" required="" id="password" name="password"/>
</div>
<div>
<input type="submit" value="Log in" />
<a href="#">Lost your password?</a>
<a href="#">Register</a>
</div>
</form><!-- form -->
</section><!-- content -->
</div><!-- container -->
</body>
</html>
After entering the user Email ID and Password, the LoginPage.jsp page is called to handle the request. In LoginPage.jsp ,which imports the java.sql package. The java.sql package helps the LoginPage.jsp page to access the user table in the database that stores user information. After a user submits a request, the LoginPage.jsp page is used to generate the appropriate response for each request made.
LoginPage.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@page import="java.util.ArrayList"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
Connection con=null;
PreparedStatement ps=null;
ResultSet rs=null;
int flag=0;
try
{
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/gateschema","root","root");
}
catch (Exception e)
{
e.printStackTrace();
}
PrintWriter pw=response.getWriter();
String uid=request.getParameter("username");
String Pwd=request.getParameter("password");
if(con!=null)
{
try
{
Statement stmt=con.createStatement();
rs=stmt.executeQuery("Select * from user;");
while(rs.next())
{
if(uid.equals(rs.getString(4)) && Pwd.equals(rs.getString(6)))
{
String tempFname=rs.getString(2);
String tempLname=rs.getString(3);
String tempEmail=rs.getString(4);
session.setAttribute("fname",tempFname);
session.setAttribute("lname",tempLname);
session.setAttribute("Email",tempEmail);
flag=0;
break;
}
else
{
flag=1;
}
}
if(flag==1)
{
response.sendRedirect("Login.jsp");
}
else
{
pw.print("welcome jaya");
response.sendRedirect("Welcome.jsp");
}
}
catch (SQLException e)
{
e.printStackTrace();
}
}
%>
</body>
</html>
The LoginPage.jsp page also includes another JSP page, named Welcome.jsp, which is used to display the currently logged in user information.
Welcome.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@page import="java.util.ArrayList"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
if ((session.getAttribute("Email") == null) || (session.getAttribute("Email") == "")) {
%>
You are not logged in<br/>
<a href="Login.jsp">Please Login</a>
<%} else
{
%>
<% String n=(String)session.getAttribute("fname");
String l=(String)session.getAttribute("lname");
out.print("Welcome "+n + " " +l);
//out.close();
%>
<br><a href='Logout.jsp'>Log out</a><br>
<%
}
%>
</body>
</html>
The Welcome.jsp page also includes another JSP page, named Logout.jsp, which is used to logout the currently logged in user.
Logout.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
out.print("you have successfully Logout");
session.setAttribute("Email", null);
session.invalidate();
response.sendRedirect("Index.jsp");
%>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
out.print("you have successfully Logout");
session.setAttribute("Email", null);
session.invalidate();
response.sendRedirect("Index.jsp");
%>
</body>
</html>
Database:User
User_ID | First_Name | Lasr_Name | Email_ID | User_Mobile | Password |
1 | Annie | jambhulkar | annie.jambhulkar@gmail.com | 9999999999 | annie123 |
2 | Anaya | gadge | Anaya.gadge@gmail.com | 8888888888 | anaya123 |