Simple Registration Application using Servlet

In this tutorial, I have written code about, Simple Registration Application using Servlet, JSP, and JDBC. The application was tested in MySQL Database and same has been shared in the article.

Project Structure

Simple Registration Application using Servlet

Table Query

CREATE TABLE student_details(firstName varchar(30), secondName varchar(30), age int, emailId varchar(50), dob date, contactNumber varchar(12), qualification varchar(30), address varchar(100));

Simple Registration Application using Servlet, JSP, and JDBC Example

RegistrationController.java

package com.dineshkrish.controller;
import java.io.IOException;
import java.sql.Date;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.dineshkrish.bo.StudentService;
import com.dineshkrish.dto.Student;
/**
* 
* @author Dinesh Krishnan
*
*/
public class RegistrationController extends HttpServlet {
private String firstName;
private String secondName;
private int age;
private String emailId;
private Date dob;
private String contactNumber;
private String qualification;
private String address;
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// Defining Student Service Object
StudentService service = new StudentService();
// Getting all Parameter value from HTML form
firstName = req.getParameter("firstName");
secondName = req.getParameter("secondName");
age = Integer.parseInt(req.getParameter("age"));
emailId = req.getParameter("emailId");
dob = convertStringToDate(req.getParameter("dob"));
contactNumber = req.getParameter("contactNumber");
qualification = req.getParameter("qualification");
address = req.getParameter("address");
// Defining Student Object
Student student = new Student();
student.setFirstName(firstName);
student.setSecondName(secondName);
student.setAge(age);
student.setEmailId(emailId);
student.setDob(dob);
student.setContactNumber(contactNumber);
student.setQualification(qualification);
student.setAddress(address);
if(service.addStudent(student)) {
// Dispatching Success Page
RequestDispatcher dispatcher = req.getRequestDispatcher("success.jsp");
dispatcher.forward(req, resp);
} else {
// Dispatching Error Page
RequestDispatcher dispatcher = req.getRequestDispatcher("error.jsp");
dispatcher.forward(req, resp);
}
}
// Method to Convert String java.sql.Date Object
private Date convertStringToDate(String strDate) {
DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
java.util.Date d = null;
try {
d = format.parse(strDate);
} catch (ParseException e) {
e.printStackTrace();
}
return new Date(d.getTime());
}
}

Student.java

package com.dineshkrish.dto;
import java.sql.Date;
/**
* 
* @author Dinesh Krishnan
*
*/
public class Student {
private String firstName;
private String secondName;
private int age;
private String emailId;
private Date dob;
private String contactNumber;
private String qualification;
private String address;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getSecondName() {
return secondName;
}
public void setSecondName(String secondName) {
this.secondName = secondName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getEmailId() {
return emailId;
}
public void setEmailId(String emailId) {
this.emailId = emailId;
}
public Date getDob() {
return dob;
}
public void setDob(Date dob) {
this.dob = dob;
}
public String getContactNumber() {
return contactNumber;
}
public void setContactNumber(String contactNumber) {
this.contactNumber = contactNumber;
}
public String getQualification() {
return qualification;
}
public void setQualification(String qualification) {
this.qualification = qualification;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}

StudentService.java

package com.dineshkrish.bo;
import com.dineshkrish.dao.StudentDAO;
import com.dineshkrish.dto.Student;
/**
* 
* @author Dinesh Krishnan
*
*/
public class StudentService {
public boolean addStudent(Student student) {
StudentDAO dao = new StudentDAO();
// Validation Should go here....
return dao.add(student);
}
}

ConnectionProvider.java

package com.dineshkrish.db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
* 
* @author Dinesh Krishnan
*
*/
public class ConnectionProvider {
private static Connection connection;
public static Connection getConnection() {
try {
// Loading Driver Class
Class.forName("com.mysql.jdbc.Driver");
// Getting the Connection
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/student", "root", "root");
} catch (ClassNotFoundException e) {
System.out.println(e.getMessage());
e.printStackTrace();
} catch (SQLException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
return connection;
}
}

StudentDAO

package com.dineshkrish.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import com.dineshkrish.db.ConnectionProvider;
import com.dineshkrish.dto.Student;
/**
* 
* @author Dinesh Krishnan
*
*/
public class StudentDAO {
public boolean add(Student student) {
// Getting Connection Object
Connection connection = ConnectionProvider.getConnection();
try {
PreparedStatement ps = connection.prepareStatement("INSERT INTO student_details(firstName, secondName, "
+ "age, emailId, dob, contactNumber, qualification, address) VALUES (?,?,?,?,?,?,?,?)");
ps.setString(1, student.getFirstName());
ps.setString(2, student.getSecondName());
ps.setInt(3, student.getAge());
ps.setString(4, student.getEmailId());
ps.setDate(5, student.getDob());
ps.setString(6, student.getContactNumber());
ps.setString(7, student.getQualification());
ps.setString(8, student.getAddress());
// It will return 1 if its Inserted successfully..
if(ps.executeUpdate() > 0) {
return true;
}
} catch (SQLException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
return false;
}
}

index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Simple Registration Page</title>
</head>
<body>
<h1>Student Registration</h1>
<form action="doRegister" method="post">
<table cellpadding="8" cellspacing="8">
<tr>
<td><label>First Name</label></td>
<td><input type="text" name="firstName"></td>
</tr>
<tr>
<td><label>Second Name</label></td>
<td><input type="text" name="secondName"></td>
</tr>
<tr>
<td><label>Age</label></td>
<td><input type="number" name="age"></td>
</tr>
<tr>
<td><label>Email ID</label></td>
<td><input type="text" name="emailId"></td>
</tr>
<tr>
<td><label>DOB</label></td>
<td><input type="date" name="dob"></td>
</tr>
<tr>
<td><label>Contact Number</label></td>
<td><input type="text" name="contactNumber"></td>
</tr>
<tr>
<td><label>Qualification</label></td>
<td>
<select name="qualification">
<option value="">Select Qualification</option>
<option value="BS">BS</option>
<option value="MS">MS</option>
</select>
</td>
</tr>
<tr>
<td><label>Address</label></td>
<td>
<textarea cols="30" rows="5" name="address"> 
</textarea>
</td>
</tr>
<tr>
<td></td>
<td align="right">
<input type="submit" value="Register">
<input type="Reset" value="Cancel">
</td>
</tr>
</table>
</form>
</body>
</html>

success.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Hi, You have Registered Successfully....</h1>
</body>
</html>

error.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Application Error Occurred..</h1>
</body>
</html>

Output

Welcome Screen

Simple Registration Application using Servlet, JSP, and JDBC Example

Success Page

Simple Registration Application using Servlet, JSP, and JDBC Example

Table Result

Simple Registration Application using Servlet, JSP, and JDBC Example

Download Code

Download the Example from GitHub

No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *