Monday 4 April 2016

REGISTRATION FORM IN JSP

For creating registration form, you must have a table in the database. You can write the database logic in JSP file, but separating it from the JSP page is better approach. Here, we are going to use DAO, Factory Method, DTO and Singletion design patterns. There are many files:
  • index.jsp for getting the values from the user
  • User.java, a bean class that have properties and setter and getter methods.
  • process.jsp, a jsp file that processes the request and calls the methods
  • Provider.java, an interface that contains many constants like DRIVER_CLASS, CONNECTION_URL, USERNAME and PASSWORD
  • ConnectionProvider.java, a class that returns an object of Connection. It uses the Singleton and factory method design pattern.
  • RegisterDao.java, a DAO class that is responsible to get access to the database


Example of Registration Form in JSP

In this example, we are using the Oracle10g database to connect with the database. Let's first create the table in the Oracle database:

  1. CREATE TABLE  "USER432"   
  2.    (    "NAME" VARCHAR2(4000),   
  3.     "EMAIL" VARCHAR2(4000),   
  4.     "PASS" VARCHAR2(4000)  
  5.    )  
  6. /  
We have created the table named user432 here.

index.jsp

We are having only three fields here, to make the concept clear and simplify the flow of the application. You can have other fields also like country, hobby etc. according to your requirement.

  1. <form action="process.jsp">  
  2. <input type="text" name="uname" value="Name..." onclick="this.value=''"/><br/>  
  3. <input type="text" name="uemail"  value="Email ID..." onclick="this.value=''"/><br/>  
  4. <input type="password" name="upass"  value="Password..." onclick="this.value=''"/><br/>  
  5. <input type="submit" value="register"/>  
  6. </form>  

process.jsp

This jsp file contains all the incoming values to an object of bean class which is passed as an argument in the register method of the RegisterDao class.

  1. <%@page import="bean.RegisterDao"%>  
  2. <jsp:useBean id="obj" class="bean.User"/>  
  3.   
  4. <jsp:setProperty property="*" name="obj"/>  
  5.   
  6. <%  
  7. int status=RegisterDao.register(obj);  
  8. if(status>0)  
  9. out.print("You are successfully registered");  
  10.   
  11. %>  

User.java

It is the bean class that have 3 properties uname, uemail and upass with its setter and getter methods.

  1. package bean;  
  2.   
  3. public class User {  
  4. private String uname,upass,uemail;  
  5.   
  6. public String getUname() {  
  7.     return uname;  
  8. }  
  9.   
  10. public void setUname(String uname) {  
  11.     this.uname = uname;  
  12. }  
  13.   
  14. public String getUpass() {  
  15.     return upass;  
  16. }  
  17.   
  18. public void setUpass(String upass) {  
  19.     this.upass = upass;  
  20. }  
  21.   
  22. public String getUemail() {  
  23.     return uemail;  
  24. }  
  25.   
  26. public void setUemail(String uemail) {  
  27.     this.uemail = uemail;  
  28. }  
  29.   
  30. }  

Provider.java

This interface contains four constants that can vary 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 is responsible to return the object of Connection. Here, driver class is loaded only once and connection object gets memory only once.

  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. }  

RegisterDao.java

This class inserts the values of the bean component into the database.

  1. package bean;  
  2.   
  3. import java.sql.*;  
  4.   
  5. public class RegisterDao {  
  6.   
  7. public static int register(User u){  
  8. int status=0;  
  9. try{  
  10. Connection con=ConnectionProvider.getCon();  
  11. PreparedStatement ps=con.prepareStatement("insert into user432 values(?,?,?)");  
  12. ps.setString(1,u.getUname());  
  13. ps.setString(2,u.getUemail());  
  14. ps.setString(3,u.getUpass());  
  15.               
  16. status=ps.executeUpdate();  
  17. }catch(Exception e){}  
  18.       
  19. return status;  
  20. }  
  21.   
  22. }  

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. }  

No comments:

Post a Comment