Registration form in jsp with database connectivity


This example shows you how to create simple registration form in Servlet Jsp with Mysql database connectivity


Environment used

    Servlet 3.1 jar

    Mysql 5.1 server or greater version and its mysql connector jar

    Eclipse luna EE or greater version

    tomcat 8


Registration page (registration.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>User Data</title>
</head>
<style>
div.ex {
text-align: right width:300px;
padding: 10px;
border: 5px solid grey;
margin: 0px
}
</style>
<body>
<h1>Registration Form</h1>
<div class="ex">
<form action="RegistrationController" method="post">
<table style="with: 50%">
<tr>
<td>Full Name</td>
<td><input type="text" name="fullname" /></td>
</tr>
<tr>
<td>Username</td>
<td><input type="text" name="userName" /></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="pass" /></td>
</tr>
<tr>
<td>Address</td>
<td><input type="text" name="address" /></td>
</tr>
<tr>
<td>Age</td>
<td><input type="text" name="age" /></td>
</tr>
<tr>
<td>Qualification</td>
<td><input type="text" name="qual" /></td>
</tr>
<tr>
<td>Percentage</td>
<td><input type="text" name="percent" /></td>
</tr>
<tr>
<td>Year Passed</td>
<td><input type="text" name="yop" /></td>
</tr>
</table>
<input type="submit" value="register" />
</form>
<br>

create a student table in test database before registering this form
<br> Syntax : <br>
<i>create table student(name varchar(100), userName varchar(100), pass varchar(100), addr varchar(100), age int, qual varchar(100), percent varchar(100), year varchar(100));</i>


</div>
</body>
</html>

web.xml mapping

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>registration</display-name>
<welcome-file-list>
<welcome-file>registration.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>RegistrationController</display-name>
<servlet-name>RegistrationController</servlet-name>
<servlet-class>com.candidjava.RegistrationController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RegistrationController</servlet-name>
<url-pattern>/RegistrationController</url-pattern>
</servlet-mapping>
</web-app>

Servlet registration example with Mysql Database connectivity

package com.candidjava;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* Servlet implementation class UserDataServlet
*/
public class RegistrationController extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String name = request.getParameter("fullname");
String userName = request.getParameter("userName");
String pass = request.getParameter("pass");
String addr = request.getParameter("address");
String age = request.getParameter("age");
String qual = request.getParameter("qual");
String percent = request.getParameter("percent");
String year = request.getParameter("yop");

// validate given input
if (name.isEmpty() || addr.isEmpty() || age.isEmpty() || qual.isEmpty() || percent.isEmpty() || year.isEmpty()) {
RequestDispatcher rd = request.getRequestDispatcher("registration.jsp");
out.println("<font color=red>Please fill all the fields</font>");
rd.include(request, response);
} else {
// inserting data into mysql database
// create a test database and student table before running this to create table
//create table student(name varchar(100), userName varchar(100), pass varchar(100), addr varchar(100), age int, qual varchar(100), percent varchar(100), year varchar(100));
try {
Class.forName("com.mysql.jdbc.Driver");
// loads mysql driver

Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");

String query = "insert into student values(?,?,?,?,?,?,?,?)";

PreparedStatement ps = con.prepareStatement(query); // generates sql query

ps.setString(1, name);
ps.setString(2, userName);
ps.setString(3, pass);
ps.setString(4, addr);
ps.setInt(5, Integer.parseInt(age));
ps.setString(6, qual);
ps.setString(7, percent);
ps.setString(8, year);

ps.executeUpdate(); // execute it on test database
System.out.println("successfuly inserted");
ps.close();
con.close();
} catch (ClassNotFoundException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
RequestDispatcher rd = request.getRequestDispatcher("home.jsp");
rd.forward(request, response);
}
}
}

Success page (home.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>Display</title>
<style>
table#nat{
width: 50%;
background-color: #c48ec5;
}
</style>
</head>
<body>
<%
String name = request.getParameter("fullname");
String userName = request.getParameter("userName");
String pass = request.getParameter("pass");
String addr = request.getParameter("address");
String age = request.getParameter("age");
String qual = request.getParameter("qual");
String percent = request.getParameter("percent");
String year = request.getParameter("yop");
%>
<table id ="nat">
<tr>
<td>Full Name</td>
<td><%= name %></td>
</tr>
<tr>
<td>User Name</td>
<td><%= userName %></td>
</tr>
<tr>
<td>Address</td>
<td><%= addr %></td>
</tr>
<tr>
<td>Age</td>
<td><%= age %></td>
</tr>
<tr>
<td>Qualification</td>
<td><%= qual %></td>
</tr>
<tr>
<td>Percentage</td>
<td><%= percent %></td>
</tr>
<tr>
<td>Year of Passout</td>
<td><%= year %></td>
</tr>
</table>
<br>
use " <i> select * from student; </i> " in mysql client to verify it.
</body>
</html>

Output Screen shot


Download

    Jsp Servlet registration with mysql database war

    Jsp Servlet registration with mysql database zip




Related Post

Comments


©candidjava.com