logo

Example Of Registration Form In Servlet


Show

Today We will study how to make a Simple Registration form in Servlet. We are going to use the Oracle 10g Database. Firstly we need to create a table as given below:

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

To make the registration page in servlet, we are up to separate the database logic from the servlet. But here, we're blending the database logic withinside the servlet best for simplicity of the program. We will expand this web page in JSP following DAO, DTO and Singleton layout samples later.

Registration Form in Servlet- Example

We have created 3 pages in this Example:

  • register.html
  • Register.java
  • web.xml

Register.html

In this page, we have acquired input from the user using text field and combo box. The details entered by the user are sent on to Register Servlet, which is responsible to stock the data into the database.

<html>  
<body>  
<form action="servlet/Register" method="post">    

Name:<input type="text" name="userName"/><br/><br/>  
Password:<input type="password" name="userPass"/><br/><br/>  
Email Id:<input type="text" name="userEmail"/><br/><br/>  
Country:  
<select name="userCountry">  
<option>India</option>  
<option>Pakistan</option>  
<option>other</option>  
</select>  

<br/><br/>  

<input type="submit" value="register"/>    

</form>  
</body>  
</html>  

Register.java

This Servlet class receives all the data entered by the user and stocks it into the database. Here, we are Performing the database logic. But you can separate it which will be better for the web application.

import java.io.*;  
import java.sql.*;  
import javax.servlet.ServletException;  
import javax.servlet.http.*;  

public class Register extends HttpServlet {  
public void doPost(HttpServletRequest request, HttpServletResponse response)  
            throws ServletException, IOException {    

response.setContentType("text/html");  
PrintWriter out = response.getWriter();           

String n=request.getParameter("userName");  
String p=request.getParameter("userPass");  
String e=request.getParameter("userEmail");  
String c=request.getParameter("userCountry");  

try{  
Class.forName("oracle.jdbc.driver.OracleDriver");  
Connection con=DriverManager.getConnection(  
"jdbc:oracle:thin:@localhost:1521:xe","system","oracle");   

PreparedStatement ps=con.prepareStatement(  
"insert into registeruser values(?,?,?,?)");   

ps.setString(1,n);  
ps.setString(2,p);  
ps.setString(3,e);  
ps.setString(4,c);  
          
int i=ps.executeUpdate();  
if(i>0)  
out.print("You are successfully registered...");  
          
}catch (Exception e2) {System.out.println(e2);}  
          
out.close();  
}  
  
}  

Web.xml file

This is the configuration file providing t6he information about Servlet.

<web-app>  

<servlet>  
<servlet-name>Register</servlet-name>  
<servlet-class>Register</servlet-class>  
</servlet>    

<servlet-mapping>  
<servlet-name>Register</servlet-name>  
<url-pattern>/servlet/Register</url-pattern>  
</servlet-mapping>  
  
<welcome-file-list>  
    <welcome-file>register.html</welcome-file>  
</welcome-file-list>   

</web-app>

download this example

download the war file

download this example (developed using Myeclipse IDE)

download this example (developed using Eclipse IDE)

download this example (developed using Netbeans IDE)

To Connect java application with the oracle database ojdbc14.jar file is essential to be loaded. Put this jar file in WEB-INF/lib folder.

download the jar file ojdbc14.jar´╗┐