JSP Tutorials

Registration Form in JSP
Previous Home Next
adplus-dvertising

Registration Form in JSP

We have to creating registration form a table in the database and used Factory Method, DTO and Singletion design patterns.

  1. index.jsp : Getting the values from the user
  2. User.java : a bean class set setter and getter methods.
  3. process.jsp :Processes the request and calls the methods in jsp
  4. Provider.java :They have many constants like DRIVER_CLASS, CONNECTION_URL, USERNAME and PASSWORD
  5. ConnectionProvider.java :We uses the Singleton and factory method
  6. RegisterDao.java :DAO class is responsible for access to the database

Registration Form Example

Oracle10g database and first create the table in the Oracle database as follows

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

    CREATE TABLE  "USER432"   
       (    "NAME" VARCHAR2(4000),   
        "EMAIL" VARCHAR2(4000),   
        "PASS" VARCHAR2(4000)  
       )  
    /  
index.jsp
    <form action="process.jsp">  
    <input type="text" name="uname" value="Name..." onclick="this.value=''"/><br/>  
    <input type="text" name="uemail"  value="Email ID..." onclick="this.value=''"/><br/>  
    <input type="password" name="upass"  value="Password..." onclick="this.value=''"/><br/>  
    <input type="submit" value="register"/>  
    </form>  

    <form action="process.jsp">  
    <input type="text" name="uname" value="Name..." onclick="this.value=''"/><br/>  
    <input type="text" name="uemail"  value="Email ID..." onclick="this.value=''"/><br/>  
    <input type="password" name="upass"  value="Password..." onclick="this.value=''"/><br/>  
    <input type="submit" value="register"/>  
    </form>  
process.jsp
    <%@page import="bean.RegisterDao"%>  
    <jsp:useBean id="obj" class="bean.User"/>  
    <jsp:setProperty property="*" name="obj"/>  
    <%  
    int status=RegisterDao.register(obj);  
    if(status>0)  
    out.print("You are successfully registered");  
      
    %>
    <%@page import="bean.RegisterDao"%>  
    <jsp:useBean id="obj" class="bean.User"/> 
    <jsp:setProperty property="*" name="obj"/>  
    <%  
    int status=RegisterDao.register(obj);  
    if(status>0)  
    out.print("You are successfully registered");  
    %>  
User.java
    package bean;  
    public class User {  
    private String uname,upass,uemail; 
    public String getUname() {  
        return uname;  
    }  
    public void setUname(String uname) {  
        this.uname = uname;  
    } 
    public String getUpass() {  
        return upass;  
    } 
    public void setUpass(String upass) {  
        this.upass = upass;  
    }  
    public String getUemail() {  
        return uemail;  
    }  
    public void setUemail(String uemail) {  
        this.uemail = uemail;  }   
    }
    package bean;  
    public class User {  
    private String uname,upass,uemail;  
    public String getUname() {  
        return uname;  
    }  
    public void setUname(String uname) {  
        this.uname = uname;  
    }  
    public String getUpass() {  
        return upass;  
    }  
    public void setUpass(String upass) {  
        this.upass = upass;  
    } 
    public String getUemail() {  
        return uemail;  
    }  
    public void setUemail(String uemail) {  
        this.uemail = uemail;  
    }    
    }  
Provider.java
   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";  
    }  

    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
    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;  
    }   
    }
    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;  
    }    
    }  
RegisterDao.java
    package bean;  
    import java.sql.*; 
    public class RegisterDao {  
    public static int register(User u){  
    int status=0;  
    try{  
    Connection con=ConnectionProvider.getCon();  
    PreparedStatement ps=con.prepareStatement("insert into user432 values(?,?,?)");  
    ps.setString(1,u.getUname());  
    ps.setString(2,u.getUemail());  
    ps.setString(3,u.getUpass());  
    status=ps.executeUpdate();  
    }catch(Exception e){}     
    return status;  
    }   
    }
    package bean; 
    import java.sql.*;  
    public class RegisterDao {  
    public static int register(User u){  
    int status=0;  
    try{  
    Connection con=ConnectionProvider.getCon();  
    PreparedStatement ps=con.prepareStatement("insert into user432 values(?,?,?)");  
    ps.setString(1,u.getUname());  
    ps.setString(2,u.getUemail());  
    ps.setString(3,u.getUpass());
    status=ps.executeUpdate();  
    }catch(Exception e){}     
    return status;  
    }  
    }  
Previous Home Next