logo

Login And Logout Example In JSP


Show

In this instance of creating a login form, we've got used the DAO (Data Access Object), Factory technique and DTO (Data Transfer Object) layout patterns. There are many files:

  • index.jsp it offers three links for login, logout and profile
  • login.jsp for getting the values from the user
  • loginprocess.jsp, a jsp document that strategies the request and calls the methods.
  • LoginBean.java, a bean class which has properties and setter and getter methods.
  • Provider.java, an interface that consists of many constants like DRIVER_CLASS, CONNECTION_URL, USERNAME and PASSWORD
  • ConnectionProvider.java, a class that is responsible for returning the object of Connection. It makes use of the Singleton and factory technique design pattern.
  • LoginDao.java, a DAO class that verifies the emailId and password from the database.
  • logout.jsp invalidates the session.
  • profile.jsp offers an easy message if the user is logged in, in any other case forwards the request to the login.jsp page.

In this instance, we're using the Oracle10g database to fit the emailId and password with the database. The table name is user432 that has many fields like name, email, pass etc. You can also additionally use this question to create the table:

CREATE TABLE "USER432"
( "NAME" VARCHAR2(4000),
"EMAIL" VARCHAR2(4000),
"PASS" VARCHAR2(4000)
)
/

We presume that there is a lot of data in this table.

index.jsp

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

"login.jsp">login'

"logout.jsp">logout'

"profile.jsp">profile

login.jsp

This file generates a login form for two input fields name and password. It is an easy login form, you can alter it for a finer focus and feel. We are concentrating on the concept only.

<%@ include file="index.jsp" %>  
<hr/>  
  
<h3>Login Form</h3>  
<%  
String profile_msg=(String)request.getAttribute("profile_msg");  
if(profile_msg!=null){  
out.print(profile_msg);  
}  
String login_msg=(String)request.getAttribute("login_msg");  
if(login_msg!=null){  
out.print(login_msg);  
}  
 %>  
 <br/>  
<form action="loginprocess.jsp" method="post">  
Email:<input type="text" name="email"/><br/><br/>  
Password:<input type="password" name="password"/><br/><br/>  
<input type="submit" value="login"/>"  
</form>

loginprocess.jsp

This jsp file carries 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 email id and password is correct, it shows a message saying you are successfully logged in! and handle the session so that we may understand the user.

 <%@page import="bean.LoginDao"%>
"obj" class="bean.LoginBean"/>
"*" name="obj"/>
<%
boolean status=LoginDao.validate(obj);
if(status){
out.println("You r successfully logged in");
session.setAttribute("session","TRUE");
}
else
{
out.print("Sorry, email or password error");
%>
"index.jsp">
<%
}
%> 

LoginBean.java

It is the bean class that has 2 properties: email and passes with its setter and getter ways.

 package bean;
public class LoginBean {
private String email,pass;
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
} 

Provider.java

This interface carries four constants that may vary from database to database.

package bean;
public interface Provider {
String DRIVER="oracle.jdbc.driver.OracleDriver";
String CONNECTION_URL="jdbc:oracle:thin:@localhost:1521:xe";
String USERNAME="system";
String PASSWORD="oracle";
}

ConnectionProvider.java

This class offers a factory way that gives back the object of Connection. Here, the driver class is loaded only once and the connection object gets memory only once because it is static.

package bean;
import java.sql.*;
import static bean.Provider.*;
public class ConnectionProvider {
private static Connection con=null;
static{
try{
Class.forName(DRIVER);
con=DriverManager.getConnection(CONNECTION_URL,USERNAME,PASSWORD);
}catch(Exception e){}
}
public static Connection getCon(){
return con;
}
} 

LoginDao.java

This class verified the email id and password.

package bean;
import java.sql.*;
public class LoginDao {
public static boolean validate(LoginBean bean){
boolean status=false;
try{
Connection con=ConnectionProvider.getCon();
PreparedStatement ps=con.prepareStatement(
"select * from user432 where email=? and pass=?");
ps.setString(1,bean.getEmail());
ps.setString(2, bean.getPass());
ResultSet rs=ps.executeQuery();
status=rs.next();
}catch(Exception e){}
return status;
}
}

download this example

download myeclipse code