Registration page in JSP


In this article we will learn how to create a simple registration page using JSP.

Before doing this we should have some knowledge about JDBC.

This is a technology to establish communication between Java program and a DBMS. It uses SQL (structure query language) for storing, updating, or removing data from a DBMS .The Java program can be a stand alone application, a web application or an  enterprise application

Steps to follow for connecting to DBMS

  1. Load the driver class into the runtime environment by using the forName() method of java.lang class. This method accepts a string parameter to contain the class name. This method throws a java.lang class not found Exampleception. The class is a predefined class and forName is the static method of the class, which is used to load the driver into memory for connectivity.

    Example:- Class.forName("sun.jdbc.odbc.JdbcOdbcDriver")
     
  2. Create a reference of java.sql.connection by using the getConnection() method of java.sql.DriverManager class. This method accepts three parameters as part of the driver in the JDBC:ODBC bridge. We are required to provide a DSN(Data source name) in this position. DSN can be created in control panel

    Example: Connection con=DriverManager.getConnection("jdbc:odbc:dsnname","system","password");

    The getConnection() method throws a java.sql.sql Exampleception.
     
  3. Create an instance of java.sql statement by using the create() method of connection. The statement is being used to execute various sql commands.

    Example:- Statement stmt=con.createStatement();
     
  4. Use an appropriate method of statements to Exampleecute sql command for select command use the ExampleecuteQuery() method and for insert, update and delete use ExampleecuteUpdate() method.

What is DSN?

This is a name provided to the DBMS driver present in ODBC. This recognizes the database to be used by a specific DBMS.

Creation of dsn(database source name) for Oracle

Start-Control panel- Administrative Tools- Data Sources (ODBC)-go to system dsn tab-click add button-select a driver for which you want to set up a data source (for Oracle- Oracle in XE)-select it and click finish-give any name in data source name tExampletbox-then click ok button.

Note:- Here Username=system, Password=pintu and Dsn name=dsn1

In this registration page, we are using CSS, a Java script for validation of the blank form, confirm password and email validation. Here we also use a HTML page where the user can enter there details and after submitting the button action goes to the doRegister.jsp page where data is being saved in the database and the registered user name is shown as output in the success.jsp page.

Table creation

create table employee(name varchar(50),pass varchar(50),cpass varchar(50),email varchar(50),phone int,fax int,city varchar(50),state varchar(50),zip varchar(50))

Account.html page

<html>
<head>
<title>Untitled Page</title>
<link href="./STYLE.css" rel="stylesheet" type="tExamplet/css" />
<style type="tExamplet/css">
.deepbluetExampletbold { font: Bold 10pt Verdana, Helvetica, sans-serif; color: #006db9;}
.colouredCell { background-color: #eeeeee;}
</style>
<script language="javascript">
function validate(objForm){
if(objForm.t1.value.length==0){
alert("Please enter Name!");
objForm.t1.focus();
return false;
}

if(!(isNaN(document.objForm.t1.value)))
{
alert("Name has character only!");
return false;
}

if(objForm.t2.value.length==0){
alert("Please enter Password!");
objForm.t2.focus();
return false;
}

if(objForm.t3.value.length==0){
alert("Please enter Confirm password!");
objForm.t3.focus();
return false;
}

if(document.getElementById("t2").value!=document.getElementById("t3").value)
{
alert("Confirm Password doesnot match!");
document.getElementById("t3").focus();
return false;
}

if(objForm.t4.value.length==0){
alert("Please enter Email!");
objForm.t4.focus();
return false;
}

if (document.objForm.t4.value != "")
{
var Temp = document.objForm.t4
var AtSym = Temp.value.indExampleOf('@')
var Period = Temp.value.lastIndExampleOf('.')
var Space = Temp.value.indExampleOf(' ')
var Length = Temp.value.length - 1
if ((AtSym < 1) ||
(Period <= AtSym+1) ||
(Period == Length ) ||
(Space != -1))
{
alert("Please enter a valid Email ID!");
document.objForm.t4.focus();
return false;
}
}

if(objForm.t5.value.length==0){
alert("Please enter Phone!");
objForm.t5.focus();
return false;
}

if(objForm.t6.value.length==0){
alert("Please enter Fax!");
objForm.t6.focus();
return false;
}

if(objForm.t7.value.length==0){
alert("Please enter City!");
objForm.t7.focus();
return false;
}

if(!(isNaN(document.objForm.t7.value)))
{
alert("City has character only!");
objForm.t7.focus();
return false;
}

if(objForm.t8.value.length==0){
alert("Please enter State!");
objForm.t8.focus();
return false;
}

if(!(isNaN(document.objForm.t8.value)))
{
alert("State has character only!");
objForm.t8.focus();
return false;
}

if(objForm.t9.value.length==0){
alert("Please enter Zip!");
objForm.t9.focus();
return false;
}

return true;
}
</script>
</head>
<body >
<form name ="objForm" action="./doRegister.jsp" method="post" onSubmit="return validate(this)">
<table>
<tr>
<td class="deepbluetExampletbold"><b>Create a user account</b></td>
</tr>
<tr>
<td class="colouredCell"><b>Name*</b></td>
<td><input name="t1" type="tExamplet " /></td>
</tr>
<tr>
<td class="colouredCell"><b>Password*</b></td>
<td><input name="t2" type="password" /></td>
</tr>
<tr>
<td class="colouredCell"><b>Confirm password*</b></td>
<td><input name="t3" type="password" /></td>
</tr>
<tr>
<td class="colouredCell"><b>Email*</b></td>
<td><input name="t4" type="tExamplet" /></td>
</tr>
<tr>
<td class="colouredCell"><b>Phone*</b></td>
<td><input name="t5" type="tExamplet" /></td>
</tr>
<tr>
<td class="colouredCell"><b>Fax*</b></td>
<td><input name="t6" type="tExamplet" /></td>
</tr>

<tr>
<td class="colouredCell"><b>City*</b></td>
<td><input name="t7" type="tExamplet" /></td>
</tr>
<tr>
<td class="colouredCell"><b>State*</b></td>
<td><input name="t8" type="tExamplet" /></td>
</tr>
<tr>
<td class="colouredCell"><b>Zip*</b></td>
<td><input name="t9" type="tExamplet" /></td>
</tr>
</table>
<input type="submit" value="Register"/>
</form>
</body>
</html>

doRegister.jsp

<%@ page import="java.sql.*" %>
<%
String str1=request.getParameter("t1");
String str2=request.getParameter("t2");
String str3=request.getParameter("t3");
String str4=request.getParameter("t4");
String str5=request.getParameter("t5");
String str6=request.getParameter("t6");
String str7=request.getParameter("t7");
String str8=request.getParameter("t8");
String str9=request.getParameter("t9");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:dsn1","system","pintu");
PreparedStatement pstmt=con.prepareStatement("insert into employee(name,pass,cpass,email,phone,fax,city,state,zip) values(?,?,?,?,?,?,?,?,?)");
pstmt.setString(1,str1);
pstmt.setString(2,str2);
pstmt.setString(3,str3);
pstmt.setString(4,str4);
pstmt.setString(5,str5);
pstmt.setString(6,str6);
pstmt.setString(7,str7);
pstmt.setString(8,str8);
pstmt.setString(9,str9);
pstmt.ExampleecuteUpdate();
con.close();
%>
<jsp:forward page="/success.jsp">
<jsp:param name="reg" value="<%= str1 %>" />
</jsp:forward>

success.jsp

<html>
<body>
<%
String str1=request.getParameter("t1");
%>
<h4><font color='Red'>Welcome:::</font><b><font color='#663300'><%= str1 %></font></b></h4>
</body>
</html>

Running the application

http://localhost:8081/jsp/

RgsJSP1.gif

RgsJSP2.gif


Similar Articles