Login and Logout Example in JSP

Login form in jsp with examples of session tracking, implicit objects, el, jstl, mvc, custom tags, file upload, file download, interview questions etc.

 0
Login and Logout Example in JSP

Login and Logout Example in JSP

In this example of creating login form, we have used the DAO (Data Access Object), Factory method and DTO (Data Transfer Object) design patterns. There are many files:

  • index.jsp it provides three links for login, logout and profile
  • login.jsp for getting the values from the user
  • loginprocess.jsp, a jsp file that processes the request and calls the methods.
  • LoginBean.java, a bean class that have properties and setter and getter methods.
  • Provider.java, an interface that contains many constants like DRIVER_CLASS, CONNECTION_URL, USERNAME and PASSWORD
  • ConnectionProvider.java, a class that is responsible to return the object of Connection. It uses the Singleton and factory method design pattern.
  • LoginDao.java, a DAO class that verifies the emailId and password from the database.
  • logout.jsp it invalidates the session.
  • profile.jsp it provides simple message if user is logged in, otherwise forwards the request to the login.jsp page.

 

In this example, we are using the Oracle10g database to match the emailId and password with the database. The table name is user432 which have many fields like name, email, pass etc. You may use this query to create the table:

  1. CREATE TABLE  "USER432"   
  2.    (    "NAME" VARCHAR2(4000),   
  3.     "EMAIL" VARCHAR2(4000),   
  4.     "PASS" VARCHAR2(4000)  
  5.    )  
  6. /  

We assume that there are many records in this table.


index.jsp

It simply provides three links for login, logout and profile.

  1. <a href="login.jsp">login</a>|  
  2. <a href="logout.jsp">logout</a>|  
  3. <a href="profile.jsp">profile</a>  

login.jsp

This file creates a login form for two input fields name and password. It is the simple login form, you can change it for better look and feel. We are focusing on the concept only.

  1. <%@ include file="index.jsp" %>  
  2. <hr/>  
  3.   
  4. <h3>Login Form</h3>  
  5. <%  
  6. String profile_msg=(String)request.getAttribute("profile_msg");  
  7. if(profile_msg!=null){  
  8. out.print(profile_msg);  
  9. }  
  10. String login_msg=(String)request.getAttribute("login_msg");  
  11. if(login_msg!=null){  
  12. out.print(login_msg);  
  13. }  
  14.  %>  
  15.  <br/>  
  16. <form action="loginprocess.jsp" method="post">  
  17. Email:<input type="text" name="email"/><br/><br/>  
  18. Password:<input type="password" name="password"/><br/><br/>  
  19. <input type="submit" value="login"/>"  
  20. </form>  

loginprocess.jsp

This jsp file contains all the incoming values to an object of bean class which is passed as an argument in the validate method of the LoginDao class. If emailid and password is correct, it displays a message you are successfully logged in! and maintains the session so that we may recognize the user.

  1. <%@page import="bean.LoginDao"%>  
  2. <jsp:useBean id="obj" class="bean.LoginBean"/>  
  3.   
  4. <jsp:setProperty property="*" name="obj"/>  
  5.   
  6. <%  
  7. boolean status=LoginDao.validate(obj);  
  8. if(status){  
  9. out.println("You r successfully logged in");  
  10. session.setAttribute("session","TRUE");  
  11. }  
  12. else  
  13. {  
  14. out.print("Sorry, email or password error");  
  15. %>  
  16. <jsp:include page="index.jsp"></jsp:include>  
  17. <%  
  18. }  
  19. %>  

LoginBean.java

It is the bean class that have 2 properties email and pass with its setter and getter methods.

  1. package bean;  
  2.   
  3. public class LoginBean {  
  4. private String email,pass;  
  5.   
  6. public String getEmail() {  
  7.     return email;  
  8. }  
  9.   
  10. public void setEmail(String email) {  
  11.     this.email = email;  
  12. }  
  13.   
  14. public String getPass() {  
  15.     return pass;  
  16. }  
  17.   
  18. public void setPass(String pass) {  
  19.     this.pass = pass;  
  20. }  
  21.   
  22.   
  23. }  

Provider.java

This interface contains four constants that may differ from database to database.
  1. package bean;  
  2.   
  3. public interface Provider {  
  4. String DRIVER="oracle.jdbc.driver.OracleDriver";  
  5. String CONNECTION_URL="jdbc:oracle:thin:@localhost:1521:xe";  
  6. String USERNAME="system";  
  7. String PASSWORD="oracle";  
  8.   
  9.  

ConnectionProvider.java

This class provides a factory method that returns the object of Connection. Here, driver class is loaded only once and connection object gets memory only once because it is static.

  1. package bean;  
  2. import java.sql.*;  
  3. import static bean.Provider.*;  
  4.   
  5. public class ConnectionProvider {  
  6. private static Connection con=null;  
  7. static{  
  8. try{  
  9. Class.forName(DRIVER);  
  10. con=DriverManager.getConnection(CONNECTION_URL,USERNAME,PASSWORD);  
  11. }catch(Exception e){}  
  12. }  
  13.   
  14. public static Connection getCon(){  
  15.     return con;  
  16. }  
  17.   
  18. }  

LoginDao.java

This class varifies the emailid and password.

  1. package bean;  
  2. import java.sql.*;  
  3. public class LoginDao {  
  4.   
  5. public static boolean validate(LoginBean bean){  
  6. boolean status=false;  
  7. try{  
  8. Connection con=ConnectionProvider.getCon();  
  9.               
  10. PreparedStatement ps=con.prepareStatement(  
  11.     "select * from user432 where email=? and pass=?");  
  12.   
  13. ps.setString(1,bean.getEmail());  
  14. ps.setString(2, bean.getPass());  
  15.               
  16. ResultSet rs=ps.executeQuery();  
  17. status=rs.next();  
  18.               
  19. }catch(Exception e){}  
  20.   
  21. return status;  
  22.   
  23. }  
  24.