Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

Login Form


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. <h3>Login Form</h3>
  4. <%
  5. String profile_msg=(String)request.getAttribute(“profile_msg”);
  6. if(profile_msg!=null){
  7. out.print(profile_msg);
  8. }
  9. String login_msg=(String)request.getAttribute(“login_msg”);
  10. if(login_msg!=null){
  11. out.print(login_msg);
  12. }
  13.  %>
  14.  <br/>
  15. <form action=“loginprocess.jsp” method=“post”>
  16. Email:<input type=“text” name=“email”/><br/><br/>
  17. Password:<input type=“password” name=“password”/><br/><br/>
  18. <input type=“submit” value=“login”/>”
  19. </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. <jsp:setProperty property=“*” name=“obj”/>
  4. <%
  5. boolean status=LoginDao.validate(obj);
  6. if(status){
  7. out.println(“You r successfully logged in”);
  8. session.setAttribute(“session”,“TRUE”);
  9. }
  10. else
  11. {
  12. out.print(“Sorry, email or password error”);
  13. %>
  14. <jsp:include page=“index.jsp”></jsp:include>
  15. <%
  16. }
  17. %>

LoginBean.java

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

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

Provider.java

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

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. public class ConnectionProvider {
  5. private static Connection con=null;
  6. static{
  7. try{
  8. Class.forName(DRIVER);
  9. con=DriverManager.getConnection(CONNECTION_URL,USERNAME,PASSWORD);
  10. }catch(Exception e){}
  11. }
  12. public static Connection getCon(){
  13.     return con;
  14. }
  15. }

LoginDao.java

This class varifies the emailid and password.

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

Comments

comments