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:
We assume that there are many records in this table.
It simply provides three links for login, logout and profile.
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.
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.
It is the bean class that have 2 properties email and pass with its setter and getter methods.
|This interface contains four constants that may differ from database to database.|
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.
This class varifies the emailid and password.