Registration form Example in Servlet

In this example you will see how to develop a registration form in Servlet. To develop a registration form you will need to connect your servlet application with database. That means we will implement JDBC in servlet. Here we are using Oracle10g database.

This article explains how to create a simple registration form in a Servlet. In this registration form the following tools are used:

  • Oracle 10g database
  • Apache Tomcat server
  • Eclipse IDE

We need to create the Required files:

  1. registration table
  2. reg.html file
  3. MyServlet.java file
  4. web.xml file

step-1: Create a table in database

create table registration
(
name varchar2(100),
email varchar2(100),
password varchar(100),
country varchar2(100)
);

Step-2: reg.html

<!DOCTYPE html>
<html>
<head>
<title>Registration Form</title>
</head>
<body bgcolor="pink">
<center><h1>Registration Form</h1></center><br>
<form action="MyServlet"method=”post”>
<table>
<tr>
<td>
Name:
</td>
<td>
<input type="text" name="uname">
</td>
</tr>
<td>
Create Password:
</td>
<td>
<input type="password" name="pwd">
</td>
<tr>
<td>
Confirm Password:
</td>
<td>
<input type="password" name="pwd">
</td>
</tr>
<tr>
<td>
Email-Id:
</td>
<td>
<input type="text" name="email">
</td>
</tr>
<tr>
< td>
Country:
</td>
<td>
<select name="ucountry">
<option>India </option>
<option>Pakistan</option>
<option>Australia</option>
<option>Japan</option>
<option>Canada</option>
</select>
</td>
</tr>
<tr>
<td>
<input type="submit" value="register">
</td>
<td>
<input type="reset" value="reset">
</td>
</tr>
</table>
</form>
</body>
</html>

Step-3: MyServlet.java

Now we will create a servlet file with the name "MyServlet" and provide the following code for it.

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
public class MyServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String n = request.getParameter("uname");
String p = request.getParameter("pwd");
String e = request.getParameter("email");
String c = request.getParameter("ucountry");
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "system", "oracle");
PreparedStatement stmt = con.prepareStatement("insert into registration values(?,?,?,?)");
stmt.setString(1, n);
stmt.setString(2, p);
stmt.setString(3, e);
stmt.setString(4, c);
int k = stmt.executeUpdate();
if (k > 0) {
out.println("You are successfully registered");
}
} catch (Exception e) {
System.out.println(e);
}
out.close();
}
}

Step-4:web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/MyServlet</url-pattern>
</servlet-mapping>
</web-app>

Our Services:

Java Training | Python Training | Machine Learning with Python Training | Deep Learning with Python Training | Data Science with Python Training | Projects

SILAN Technology, BBSR
Call to: 9439202111  |  9658317757  |  0674-2361252

Comment