Servlet Project User Management Project using eclipse, mysql , tomcat , bootstrap
For Explanation Watch video::
Note :: In this Project you must configure web server (for example tomcat) with eclipse ide
Download Bootstrap from :: https://getbootstrap.com/docs/4.3/getting-started/download/
Download mysql jar file from :: https://mvnrepository.com/artifact/mysql/mysql-connector-java/8.0.22
adding MySQL Connector/J jar file in eclipse ide for jdbc video ::
Directory Structure:
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
<welcome-file-list>
<welcome-file>home.html</welcome-file>
</welcome-file-list>
<display-name>UserMgmt</display-name>
</web-app>
Home.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
<link rel="stylesheet" href="css/bootstrap.css"></link>
<style>
#frm{
width:500px;
margin:auto;
margin-top:100px;
}
marquee{
margin-top:10px;
}
</style>
</head>
<body class="container-fluid">
<marquee><h2 class="text-primary">Register Here</h2></marquee>
<form action="register" method="post" class="form card" id="frm">
<h2 class="bg-danger text-white card-header">Registration Form</h2>
<table class="table table-hover">
<tr>
<td>Name</td>
<td><input type="text" name="userName" required></td>
</tr>
<tr>
<td>Email</td>
<td><input type="email" name="email" required></td>
</tr>
<tr>
<td>Mobile</td>
<td><input type="text" name="mobile" required></td>
</tr>
<tr>
<td>DOB</td>
<td><input type="date" name="dob" required></td>
</tr>
<tr>
<td>City</td>
<td><select name="city">
<option value="pune">Pune</option>
<option value="Mumbai">Mumbai</option>
<option value="Delhi">Delhi</option>
<option value="Hydrabad">Hydrabad</option>
</select></td>
</tr>
<tr>
<td>Gender</td>
<td><input type="radio" name="gender" value="male">Male
<input type="radio" name="gender" value="female">Female
</td>
</tr>
<tr class="card-footer">
<td><button type="submit" class="btn btn-outline-success">Register</button></td>
<td><button type="reset" class="btn btn-outline-danger">cancel</button></td>
</tr>
</table>
<button class="btn btn-outline-success d-block"><a href="showdata">Show Users</a></button>
</form>
</body>
</html>
RegisterServlet.java
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.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/register")
public class RegisterServlet extends HttpServlet {
private final static String query = "insert into user(name,email,mobile,dob,city,gender) values(?,?,?,?,?,?)";
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
//get PrintWriter
PrintWriter pw = res.getWriter();
//set content type
res.setContentType("text/html");
//link the bootstrap
pw.println("<link rel='stylesheet' href='css/bootstrap.css'></link>");
//get the values
String name = req.getParameter("userName");
String email = req.getParameter("email");
String mobile = req.getParameter("mobile");
String dob = req.getParameter("dob");
String city = req.getParameter("city");
String gender = req.getParameter("gender");
//load the JDBC driver
try {
Class.forName("com.mysql.cj.jdbc.Driver");
}catch(Exception e) {
e.printStackTrace();
}
//generate the connection
try(Connection con = DriverManager.getConnection("jdbc:mysql:///usermgmt","root","root");
PreparedStatement ps = con.prepareStatement(query);){
//set the values
ps.setString(1, name);
ps.setString(2, email);
ps.setString(3, mobile);
ps.setString(4, dob);
ps.setString(5, city);
ps.setString(6, gender);
//execute the query
int count = ps.executeUpdate();
pw.println("<div class='card' style='margin:auto;width:300px;margin-top:100px'>");
if(count==1) {
pw.println("<h2 class='bg-danger text-light text-center'>Record Registered Successfully</h2>");
}else {
pw.println("<h2 class='bg-danger text-light text-center'>Record Not Registered</h2>");
}
}catch(SQLException se) {
pw.println("<h2 class='bg-danger text-light text-center'>"+se.getMessage()+"</h2>");
se.printStackTrace();
}catch(Exception e) {
e.printStackTrace();
}
pw.println("<a href='home.html'><button class='btn btn-outline-success'>Home</button></a>");
pw.println("</div>");
//close the stram
pw.close();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
doGet(req,res);
}
}
ShowUserServlet.java
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/showdata")
public class ShowUserServlet extends HttpServlet {
private final static String query = "select id,name,email,mobile,dob,city,gender from user";
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
//get PrintWriter
PrintWriter pw = res.getWriter();
//set content type
res.setContentType("text/html");
//link the bootstrap
pw.println("<link rel='stylesheet' href='css/bootstrap.css'></link>");
pw.println("<marquee><h2 class='text-primary'>User Data</h2></marquee>");
//load the JDBC driver
try {
Class.forName("com.mysql.cj.jdbc.Driver");
}catch(Exception e) {
e.printStackTrace();
}
//generate the connection
try(Connection con = DriverManager.getConnection("jdbc:mysql:///usermgmt","root","root");
PreparedStatement ps = con.prepareStatement(query);){
//resultSet
ResultSet rs = ps.executeQuery();
pw.println("<div style='margin:auto;width:900px;margin-top:100px;'>");
pw.println("<table class='table table-hover table-striped'>");
pw.println("<tr>");
pw.println("<th>ID</th>");
pw.println("<th>Name</th>");
pw.println("<th>Email</th>");
pw.println("<th>Mobile No</th>");
pw.println("<th>DOB</th>");
pw.println("<th>City</th>");
pw.println("<th>Gender</th>");
pw.println("<th>Edit</th>");
pw.println("<th>Delete</th>");
pw.println("</tr>");
while(rs.next()) {
pw.println("<tr>");
pw.println("<td>"+rs.getInt(1)+"</td>");
pw.println("<td>"+rs.getString(2)+"</td>");
pw.println("<td>"+rs.getString(3)+"</td>");
pw.println("<td>"+rs.getString(4)+"</td>");
pw.println("<td>"+rs.getString(5)+"</td>");
pw.println("<td>"+rs.getString(6)+"</td>");
pw.println("<td>"+rs.getString(7)+"</td>");
pw.println("<td><a href='editurl?id="+rs.getInt(1)+"'>Edit</a></td>");
pw.println("<td><a href='deleteurl?id="+rs.getInt(1)+"'>Delete</a></td>");
pw.println("</tr>");
}
pw.println("</table>");
}catch(SQLException se) {
pw.println("<h2 class='bg-danger text-light text-center'>"+se.getMessage()+"</h2>");
se.printStackTrace();
}catch(Exception e) {
e.printStackTrace();
}
pw.println("<button class='btn btn-outline-success d-block'><a href='home.html'>Home</a></button>");
pw.println("</div>");
//close the stream
pw.close();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
doGet(req,res);
}
}
DeleteServlet.java
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.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/deleteurl")
public class DeleteServlet extends HttpServlet {
private final static String query = "delete from user where id = ?";
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
//get PrintWriter
PrintWriter pw = res.getWriter();
//set content type
res.setContentType("text/html");
//link the bootstrap
pw.println("<link rel='stylesheet' href='css/bootstrap.css'></link>");
//get the values
int id = Integer.parseInt(req.getParameter("id"));
//load the JDBC driver
try {
Class.forName("com.mysql.cj.jdbc.Driver");
}catch(Exception e) {
e.printStackTrace();
}
//generate the connection
try(Connection con = DriverManager.getConnection("jdbc:mysql:///usermgmt","root","root");
PreparedStatement ps = con.prepareStatement(query);){
//set the values
ps.setInt(1, id);
//execute the query
int count = ps.executeUpdate();
pw.println("<div class='card' style='margin:auto;width:300px;margin-top:100px'>");
if(count==1) {
pw.println("<h2 class='bg-danger text-light text-center'>Record Deleted Successfully</h2>");
}else {
pw.println("<h2 class='bg-danger text-light text-center'>Record Not Deleted</h2>");
}
}catch(SQLException se) {
pw.println("<h2 class='bg-danger text-light text-center'>"+se.getMessage()+"</h2>");
se.printStackTrace();
}catch(Exception e) {
e.printStackTrace();
}
pw.println("<a href='home.html'><button class='btn btn-outline-success'>Home</button></a>");
pw.println(" ");
pw.println("<a href='showdata'><button class='btn btn-outline-success'>Show User</button></a>");
pw.println("</div>");
//close the stram
pw.close();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
doGet(req,res);
}
}
EditScreenServlet.java
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/editurl")
public class EditScreenServlet extends HttpServlet {
private final static String query = "select name,email,mobile,dob,city,gender from user where id=?";
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
//get PrintWriter
PrintWriter pw = res.getWriter();
//set content type
res.setContentType("text/html");
//get the id
//get the values
int id = Integer.parseInt(req.getParameter("id"));
//link the bootstrap
pw.println("<link rel='stylesheet' href='css/bootstrap.css'></link>");
//load the JDBC driver
try {
Class.forName("com.mysql.cj.jdbc.Driver");
}catch(Exception e) {
e.printStackTrace();
}
//generate the connection
try(Connection con = DriverManager.getConnection("jdbc:mysql:///usermgmt","root","root");
PreparedStatement ps = con.prepareStatement(query);){
//set value
ps.setInt(1, id);
//resultSet
ResultSet rs = ps.executeQuery();
rs.next();
pw.println("<div style='margin:auto;width:500px;margin-top:100px;'>");
pw.println("<form action='edit?id="+id+"' method='post'>");
pw.println("<table class='table table-hover table-striped'>");
pw.println("<tr>");
pw.println("<td>Name</td>");
pw.println("<td><input type='text' name='name' value='"+rs.getString(1)+"'></td>");
pw.println("</tr>");
pw.println("<tr>");
pw.println("<td>Email</td>");
pw.println("<td><input type='email' name='email' value='"+rs.getString(2)+"'></td>");
pw.println("</tr>");
pw.println("<tr>");
pw.println("<td>Mobile</td>");
pw.println("<td><input type='text' name='mobile' value='"+rs.getString(3)+"'></td>");
pw.println("</tr>");
pw.println("<tr>");
pw.println("<td>DOB</td>");
pw.println("<td><input type='date' name='dob' value='"+rs.getString(4)+"'></td>");
pw.println("</tr>");
pw.println("<tr>");
pw.println("<td>City</td>");
pw.println("<td><input type='text' name='city' value='"+rs.getString(5)+"'></td>");
pw.println("</tr>");
pw.println("<tr>");
pw.println("<td>Gender</td>");
pw.println("<td><input type='text' name='gender' value='"+rs.getString(6)+"'></td>");
pw.println("</tr>");
pw.println("<tr>");
pw.println("<td><button type='submit' class='btn btn-outline-success'>Edit</button></td>");
pw.println("<td><button type='reset' class='btn btn-outline-danger'>Cancel</button></td>");
pw.println("</tr>");
pw.println("</table>");
pw.println("</form>");
}catch(SQLException se) {
pw.println("<h2 class='bg-danger text-light text-center'>"+se.getMessage()+"</h2>");
se.printStackTrace();
}catch(Exception e) {
e.printStackTrace();
}
pw.println("<a href='home.html'><button class='btn btn-outline-success'>Home</button></a>");
pw.println("</div>");
//close the stream
pw.close();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
doGet(req,res);
}
}
EditRecordServlet.java
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.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/edit")
public class EditRecordServlet extends HttpServlet {
private final static String query = "update user set name=?,email=?,mobile=?,dob=?,city=?,gender=? where id=?";
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
//get PrintWriter
PrintWriter pw = res.getWriter();
//set content type
res.setContentType("text/html");
//link the bootstrap
pw.println("<link rel='stylesheet' href='css/bootstrap.css'></link>");
//get the values
int id = Integer.parseInt(req.getParameter("id"));
String name = req.getParameter("name");
String email = req.getParameter("email");
String mobile = req.getParameter("mobile");
String dob = req.getParameter("dob");
String city = req.getParameter("city");
String gender = req.getParameter("gender");
//load the JDBC driver
try {
Class.forName("com.mysql.cj.jdbc.Driver");
}catch(Exception e) {
e.printStackTrace();
}
//generate the connection
try(Connection con = DriverManager.getConnection("jdbc:mysql:///usermgmt","root","root");
PreparedStatement ps = con.prepareStatement(query);){
//set the values
ps.setString(1, name);
ps.setString(2, email);
ps.setString(3, mobile);
ps.setString(4, dob);
ps.setString(5, city);
ps.setString(6, gender);
ps.setInt(7, id);
//execute the query
int count = ps.executeUpdate();
pw.println("<div class='card' style='margin:auto;width:300px;margin-top:100px'>");
if(count==1) {
pw.println("<h2 class='bg-danger text-light text-center'>Record Edited Successfully</h2>");
}else {
pw.println("<h2 class='bg-danger text-light text-center'>Record Not Edited</h2>");
}
}catch(SQLException se) {
pw.println("<h2 class='bg-danger text-light text-center'>"+se.getMessage()+"</h2>");
se.printStackTrace();
}catch(Exception e) {
e.printStackTrace();
}
pw.println("<a href='home.html'><button class='btn btn-outline-success'>Home</button></a>");
pw.println(" ");
pw.println("<a href='showdata'><button class='btn btn-outline-success'>Show User</button></a>");
pw.println("</div>");
//close the stram
pw.close();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
doGet(req,res);
}
}