JAVA:Registration form using JSP,Bean,and Mysql

Hello  friends,

In this section you will learn about the handling of user registration form using jsp and bean.

STEP 1Create simple registration form using jsp/html

<body >
<form action="process.jsp" method="post"><center>
<span style="font-size: x-large;">User Registration</span>

<span style="color: red; font-size: small;"><sup>*</sup> Required Fields</span>
<table border="1" cellspacing="1" cellpadding="2" bgcolor="lightblue">
<tr bgcolor="lightblue">
<td valign="top">
<strong>First Name<sup>*</sup></strong>

<input type="text" name="firstName" value="" size="20" maxlength="20" /></td>
<td valign="top">
<strong>Last Name<sup>*</sup></strong>

<input type="text" name="lastName" value="" size="15" maxlength="20" /></td>
<tr bgcolor="lightblue">
<td valign="top">

<input type="text" name="email" value="" size="25" maxlength="125" /></td>
<td valign="top">

<input type="text" name="age" value="" size="6" maxlength="5" /></td>
<tr bgcolor="lightblue">
<td valign="top">

<input type="text" name="city" value="" size="20" maxlength="20" /></td>
<td valign="top">
<strong>Zip Code<sup>*</sup></strong>

<input type="text" name="zip" value="" size="10" maxlength="8" /></td>
<tr bgcolor="lightblue">
<td colspan="2" valign="top">
<strong>User Name<sup>*</sup></strong>

<input type="text" name="userName" value="" size="20" maxlength="15" /></td>
<tr bgcolor="lightblue">
<td valign="top">

<input type="password" name="password1" value="" size="15" maxlength="15" /></td>
<td valign="top">
<strong>Confirm Password<sup>*</sup></strong>

<input type="password" name="password2" value="" size="15" maxlength="15" /></td>
<tr bgcolor="lightblue">
<td colspan="2" align="center"><input type="submit" value="Submit" /><input type="reset" value="Reset" /></td>

Step 2:  Create controller JSP .These pages would still specify the bulk of the processing to component beans, but they would also contain some conditional logic to respond to a user’s actions. But these controller pages would never contain presentation logic to display UI elements. This task would always be externalized into separate JSPs, which will be invoked as needed by the controller

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%@ page language="java" %>
<%@ page import="java.util.*" %>
<jsp:useBean id="formHandler" scope="request">
<jsp:setProperty name="formHandler" property="*"/>

if (formHandler.validate())
<jsp:forward page="successful.jsp"/>
<%   }
else  {
<jsp:forward page="retry.jsp"/>

Step3:Java Bean
In this step when developing beans for processing form data, you can follow a common design pattern 
by matching the names of the beans properties with the names of the form input elements. you would also need to 
define the corresponding getter and setter methods for each property within the bean.

package register;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.Hashtable;
import java.util.logging.Level;
import java.util.logging.Logger;
public class RegistrationDetails
private String firstName,lastName,email,userName,password1,password2,zip,city,notify;
private int age;
private Hashtable errors;
private Connection connection=null;
Statement st;

public boolean validate() {
boolean bool=true;

if (firstName.equals(""))
errors.put("firstName","Please enter your first name");
if (lastName.equals(""))
errors.put("lastName","Please enter your last name");
if (email.equals("") || (email.indexOf('@') == -1))
errors.put("email","Please enter a valid email address");

if (age130)
errors.put("age","Please enter a valid Age");

if (city.equals(""))
errors.put("city","Please enter your city name");

if (zip.equals("") || zip.length() !=6 )
errors.put("zip","Please enter a valid zip code");
try {
int x = Integer.parseInt(zip);
} catch (NumberFormatException e)
errors.put("zip","Please enter a valid zip code");

if (userName.equals(""))
errors.put("userName","Please enter a username");

if (password1.equals("") )
errors.put("password1","Please enter a valid password");

if (!password1.equals("") && (password2.equals("") || !password1.equals(password2)))
errors.put("password2","Please confirm your password");
return bool;

public String getErrorMsg(String s)
String errorMsg =(String)errors.get(s.trim());
return (errorMsg == null) ? "":errorMsg;
public void insertData()
try {
try {
} catch (Exception ex)
System.out.println("Error in class.forname"+ex.getCause());
Logger.getLogger(RegistrationDetails.class.getName()).log(Level.SEVERE, null, ex);
connection = (Connection) DriverManager getConnection. ("jdbc:mysql://localhost:3306/RegistrationForm","root", "root");
st= (Statement) connection.createStatement();
String table = "create table if not exists Registration(First_Name varchar(20),Last_Name varchar(20),
                                EmailAddress varchar(40),Age int(4),Cityvarchar(15),Zip_Code varchar(10),
                                UserName varchar(15),Password varchar(15))";
st.executeUpdate("insert into Registration(First_Name,Last_Name,EmailAddress,Age,City,Zip_Code,
                                UserName,Password) values('"+firstName+"','"+lastName+"','"+email+"','"+age+"','"
System.out.println("Store into the database");
} catch (Exception ex)
System.out.println("Error in connection::"+ex.getCause());

public RegistrationDetails()
errors = new Hashtable();

public String getFirstName() {
return firstName;}
public String getLastName() {
return lastName;}

public String getEmail() {
return email;}

public int getAge() {
return age;}

public String getUserName() {
return userName;}

public String getPassword1() {
return password1;}

public String getPassword2() {
return password2;}

public String getCity() {
return city;}

public String getZip() {
return zip;}

public void setFirstName(String fname) {
firstName =fname;}

public void setLastName(String lname) {
lastName =lname;}

public void setEmail(String eml) {

public void setAge(int age) {

public void setUserName(String u) {

public void setPassword1(String p1) {

public void setPassword2(String p2) {

public void setCity(String city) {;}

public void setZip(String z) {

Step 4: Retry Page.

Create a "retry.jsp" file for displaying error. If  the user submits the form without fulfilling field, proces.jsp 
will return on retry.jsp page. The processing logic within the controller page is straight forward. After the bean 
is instantiated, its validate() method is invoked. The validate() method has a two-pronged effect. If an error is 
encountered during the validation of any form input element, the validate() method not only resets the value of 
the corresponding bean property, but also sets an appropriate error message, which can later be displayed for that 
input element. If any of the required form elements cannot be successfully validated, the controller forwards the 
request to the JSP page retry.jsp allowing the user to make changes and resubmit the form. If there are no 
validation errors, the request is forwarded to success.jsp.

This page will be mostly same as index/first page expect some error message so I am not posting the code for retry.

Step5: Success Page

Create a "success.jsp" file for displaying all data entered by user. As stated earlier, the controller forwards 
the request to success.jsp only after all of the submitted form data has been successfully validated.Success.jsp 
in turn extracts the bean component from the request and confirms the registration to the client.

<span style="font-size: x-large;">User Registration Successfull!</span>
<table border="1" cellspacing="1" cellpadding="1">
<td valign="top"><strong>First Name</strong></td>
<td valign="top"><strong>Last Name</strong></td>
<td valign="top"><strong>E-Mail</strong></td>
<td valign="top"><strong>Age</strong></td>
<td valign="top"><strong>City</strong></td>
<td valign="top"><strong>Zip Code</strong></td>
<td colspan="2" valign="top"><strong>User Name</strong></td>

Hope it helps .

Posted in JAVA. 2 Comments »

2 Responses to “JAVA:Registration form using JSP,Bean,and Mysql”

  1. swaminathan Says:

    good example for registration

  2. chinna Says:

    if i want to login already reg page … how can i take the username and password from reg page please type code….

Speak Your Mind

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: