In this tutorial you will learn how to make JSP login and logout system using session. I have used MySQL as a database in this example.
This system has following files.
index.jsp: It contains a login form which is displayed to user.
loginRequestHandler.jsp: When login form is submitted, this page handles the login request.
home.jsp: If the login details are correct then the user will be redirect to home page. It contain welcome message with a logout link.
logout.jsp: It invalidates the session and logout the user from system.
DBConnection.java: It handles connectivity with the database.
LoginBean.java: It is a bean class that has getter and setter methods.
LoginDAO.java: This class verifies the email and password from the database.
The database table that I have used has following structure.
I have done proper session tracking in this example. If the user is not logged in and tries to open home.jsp page then he/she will be redirected to index.jsp page for login. If he/she is already logged in and tries to open index.jsp then he/she will be directly redirected to home.jsp.
Below I have shared the code for each of these files.
index.jsp
<html> <head> <title>Login System</title> </head> <body> <% String email=(String)session.getAttribute("email"); //redirect user to home page if already logged in if(email!=null){ response.sendRedirect("home.jsp"); } String status=request.getParameter("status"); if(status!=null){ if(status.equals("false")){ out.print("Incorrect login details!"); } else{ out.print("Some error occurred!"); } } %> <form action="loginRequestHandler.jsp"> <table cellpadding="5"> <tr> <td><b>Email:</b></td> <td><input type="text" name="email" required/></td> </tr> <tr> <td><b>Password:</b></td> <td><input type="password" name="password" required/></td> </tr> <tr> <td colspan="2" align="center"><input type="submit" value="Login"/></td> </tr> </table> </form> </body> </html>
loginRequestHandler.jsp
<%@page import="com.LoginDAO"%> <jsp:useBean id="loginBean" class="com.LoginBean" scope="session"/> <jsp:setProperty name="loginBean" property="*"/> <% String result=LoginDAO.loginCheck(loginBean); if(result.equals("true")){ session.setAttribute("email",loginBean.getEmail()); response.sendRedirect("home.jsp"); } if(result.equals("false")){ response.sendRedirect("index.jsp?status=false"); } if(result.equals("error")){ response.sendRedirect("index.jsp?status=error"); } %>
home.jsp
<html> <head> <title>Login System</title> </head> <body> <% String email=(String)session.getAttribute("email"); //redirect user to login page if not logged in if(email==null){ response.sendRedirect("index.jsp"); } %> <p>Welcome <%=email%></p> <a href="logout.jsp">Logout</a> </body> </html>
logout.jsp
<% session.invalidate(); response.sendRedirect("index.jsp"); %>
DBConnection.java
package com; import java.sql.Connection; import java.sql.DriverManager; public class DBConnection { static final String URL="jdbc:mysql://localhost:3306/"; static final String DATABASE_NAME="test"; static final String USERNAME="root"; static final String PASSWORD="root"; public static Connection getConnection(){ Connection con=null; try{ Class.forName("com.mysql.jdbc.Driver"); con=DriverManager.getConnection(URL+DATABASE_NAME,USERNAME,PASSWORD); }catch(Exception e){ e.printStackTrace(); } return con; } }
LoginBean.java
package com; public class LoginBean { private String email; private String password; public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
LoginDAO.java
package com; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; public class LoginDAO { public static String loginCheck(LoginBean loginBean){ String query="select * from login where email=? and password=?"; try{ Connection con=DBConnection.getConnection(); PreparedStatement ps=con.prepareStatement(query); ps.setString(1,loginBean.getEmail()); ps.setString(2,loginBean.getPassword()); ResultSet rs=ps.executeQuery(); if(rs.next()){ return "true"; } else{ return "false"; } }catch(Exception e){ e.printStackTrace(); } return "error"; } }
Screenshots
Comment below if you are facing difficulty to understand anything in above JSP login and logout system.
Happy Coding!! 🙂 🙂
Loved The Clarity in the Code !!. Can you please share a spring based program too. I have mentioned my email address below.
Thanks
Login form for employees to validate the status of employee using JSP and EJB. please help me for this scenario
Thank you very much. Can you please help me to create log in form and a form generating a data value from my excel file.
greate!!!!!! but when i am clicking on back button it is going to home.
it is not secure.
please help me when i click on back button control must stay on index page after logout.
You can disable the back button on logout page, check this http://stackoverflow.com/questions/23357394/disable-browsers-back-button-if-the-session-is-invalidated
Hi, I have written same code in eclipse IDE and i am getting false value from LoginDao.java. I think something is wrong with the data in DB.
created table is:
create table emp(“name” varchar2(4000),
“email” varchar2(4000),
“pass” varchar2(4000));
select * from emp ;
and values are:
iyanka hbcspp@gmail.com 12376sga45
Priyanka pp@gmail.com 12345
but when i am giving pp@gmail.com as email and 12345 as password it is giving me false value.
Could you please help me here!!??
values I am giving(pp@gmail.com 12345) are there in DB but still I am getting false value everytime.
iyanka hbcspp@gmail.com 12376sga45
Priyanka pp@gmail.com 12345
Can you made any change in the code?
Very Very Thanks to you I searched many website simplicity and understandable code find on your codes
once again thank you very much
I didn’t understand why you use
ps.setString(1,loginBean.getEmail());
ps.setString(2,loginBean.getPassword());
Since setString is used to store data in DataBase and here we only retrieving and comparing .
how to solve seesion in java servlet using java eclipse
Sir im totally Confused About the HIBERNATE framework.Hibernate While sending data into Database or with the data sends the Image too. please share Some Tutorials on my Email:- Mandeepsainims19@gmail.com
if I press back button previous page displaying after logout
How to avoid use of scriplet in the above code.