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:
|
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:
CREATE TABLE "USER432" ( "NAME" VARCHAR2(4000), "EMAIL" VARCHAR2(4000), "PASS" VARCHAR2(4000) ) /
We assume that there are many records in this table.
index.jsp
It simply provides three links for login, logout and profile.
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.
<%@ include file="index.jsp" %>
Login Form
<% 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); } %>
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.
<%@page import="bean.LoginDao"%> <% 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"); %> <% } %>
LoginBean.java
It is the bean class that have 2 properties email and pass with its setter and getter methods.
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 contains four constants that may differ 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 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.
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 varifies the emailid 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; } }
Simply want to say your article is as astonishing. The clarity in your
post is simply spectacular and i can assume you’re an expert on this subject.
Fine with your permission allow me to grab your feed to
keep up to date with forthcoming post. Thanks a million and
please keep up the rewarding work.