Servlet Project Book Shop Application in eclipse
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 ::
video link : https://youtu.be/4Fyd-k3eG_I
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>BookWebApp</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">
</head>
<body class="container-fluid card" style="width:40rem;">
<h2 class="bg-danger text-white text-center">Book Registration</h2>
<form action="register" method="post">
<table class="table table-hover">
<tr>
<td>Book Name</td>
<td><input type="text" name="bookName"></td>
</tr>
<tr>
<td>Book Edition</td>
<td><input type="text" name="bookEdition"></td>
</tr>
<tr>
<td>Book Price</td>
<td><input type="text" name="bookPrice"></td>
</tr>
<tr>
<td><input type="submit" value="register"></td>
<td><input type="reset" value="cancel"></td>
</tr>
</table>
<a href='bookList'>Book List</a>
</form>
</body>
</html>
Register Servlet .java file code
package com.idiot.servlet;
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 static final String query = "INSERT INTO BOOKDATA(BOOKNAME,BOOKEDITION,BOOKPRICE) 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");
//GET THE book info
String bookName = req.getParameter("bookName");
String bookEdition = req.getParameter("bookEdition");
float bookPrice = Float.parseFloat(req.getParameter("bookPrice"));
//LOAD jdbc driver
try {
Class.forName("com.mysql.cj.jdbc.Driver");
}catch(ClassNotFoundException cnf) {
cnf.printStackTrace();
}
//generate the connection
try(Connection con = DriverManager.getConnection("jdbc:mysql:///book","root","root");
PreparedStatement ps = con.prepareStatement(query);){
ps.setString(1, bookName);
ps.setString(2, bookEdition);
ps.setFloat(3, bookPrice);
int count = ps.executeUpdate();
if(count==1) {
pw.println("<h2>Record Is Registered Sucessfully</h2>");
}else {
pw.println("<h2>Record not Registered Sucessfully");
}
}catch(SQLException se) {
se.printStackTrace();
pw.println("<h1>"+se.getMessage()+"</h2>");
}catch(Exception e) {
e.printStackTrace();
pw.println("<h1>"+e.getMessage()+"</h2>");
}
pw.println("<a href='home.html'>Home</a>");
pw.println("<br>");
pw.println("<a href='bookList'>Book List</a>");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
doGet(req,res);
}
}
Book List Servlet .java file code
package com.idiot.servlet;
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("/bookList")
public class BookListServlet extends HttpServlet {
private static final String query = "SELECT ID,BOOKNAME,BOOKEDITION,BOOKPRICE FROM BOOKDATA";
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
//get PrintWriter
PrintWriter pw = res.getWriter();
//set content type
res.setContentType("text/html");
//LOAD jdbc driver
try {
Class.forName("com.mysql.cj.jdbc.Driver");
}catch(ClassNotFoundException cnf) {
cnf.printStackTrace();
}
//generate the connection
try(Connection con = DriverManager.getConnection("jdbc:mysql:///book","root","root");
PreparedStatement ps = con.prepareStatement(query);){
ResultSet rs = ps.executeQuery();
pw.println("<table border='1' align='center'>");
pw.println("<tr>");
pw.println("<th>Book Id</th>");
pw.println("<th>Book Name</th>");
pw.println("<th>Book Edition</th>");
pw.println("<th>Book Price</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.getFloat(4)+"</td>");
pw.println("<td><a href='editScreen?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) {
se.printStackTrace();
pw.println("<h1>"+se.getMessage()+"</h2>");
}catch(Exception e) {
e.printStackTrace();
pw.println("<h1>"+e.getMessage()+"</h2>");
}
pw.println("<a href='home.html'>Home</a>");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
doGet(req,res);
}
}
Edit Screen Servlet .java file code
package com.idiot.servlet;
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("/editScreen")
public class EditScreenServlet extends HttpServlet {
private static final String query = "SELECT BOOKNAME,BOOKEDITION,BOOKPRICE FROM BOOKDATA 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 of record
int id = Integer.parseInt(req.getParameter("id"));
//LOAD jdbc driver
try {
Class.forName("com.mysql.cj.jdbc.Driver");
}catch(ClassNotFoundException cnf) {
cnf.printStackTrace();
}
//generate the connection
try(Connection con = DriverManager.getConnection("jdbc:mysql:///book","root","root");
PreparedStatement ps = con.prepareStatement(query);){
ps.setInt(1, id);
ResultSet rs = ps.executeQuery();
rs.next();
pw.println("<form action='editurl?id="+id+"' method='post'>");
pw.println("<table align='center'>");
pw.println("<tr>");
pw.println("<td>Book Name</td>");
pw.println("<td><input type='text' name='bookName' value='"+rs.getString(1)+"'></td>");
pw.println("</tr>");
pw.println("<tr>");
pw.println("<td>Book Edition</td>");
pw.println("<td><input type='text' name='bookEdition' value='"+rs.getString(2)+"'></td>");
pw.println("</tr>");
pw.println("<tr>");
pw.println("<td>Book Price</td>");
pw.println("<td><input type='text' name='bookPrice' value='"+rs.getFloat(3)+"'></td>");
pw.println("</tr>");
pw.println("<tr>");
pw.println("<td><input type='submit' value='Edit'></td>");
pw.println("<td><input type='reset' value='cancel'></td>");
pw.println("</tr>");
pw.println("</table>");
pw.println("</form>");
}catch(SQLException se) {
se.printStackTrace();
pw.println("<h1>"+se.getMessage()+"</h2>");
}catch(Exception e) {
e.printStackTrace();
pw.println("<h1>"+e.getMessage()+"</h2>");
}
pw.println("<a href='home.html'>Home</a>");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
doGet(req,res);
}
}
EditServlet .java file code
package com.idiot.servlet;
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 EditServlet extends HttpServlet {
private static final String query = "update BOOKDATA set BOOKNAME=?,BOOKEDITION=?,BOOKPRICE=? 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 of record
int id = Integer.parseInt(req.getParameter("id"));
//get the edit data we want to edit
String bookName = req.getParameter("bookName");
String bookEdition = req.getParameter("bookEdition");
float bookPrice = Float.parseFloat(req.getParameter("bookPrice"));
//LOAD jdbc driver
try {
Class.forName("com.mysql.cj.jdbc.Driver");
}catch(ClassNotFoundException cnf) {
cnf.printStackTrace();
}
//generate the connection
try(Connection con = DriverManager.getConnection("jdbc:mysql:///book","root","root");
PreparedStatement ps = con.prepareStatement(query);){
ps.setString(1, bookName);
ps.setString(2, bookEdition);
ps.setFloat(3, bookPrice);
ps.setInt(4, id);
int count = ps.executeUpdate();
if(count==1) {
pw.println("<h2>Record is Edited Successfully</h2>");
}else {
pw.println("<h2>Record is not Edited Successfully</h2>");
}
}catch(SQLException se) {
se.printStackTrace();
pw.println("<h1>"+se.getMessage()+"</h2>");
}catch(Exception e) {
e.printStackTrace();
pw.println("<h1>"+e.getMessage()+"</h2>");
}
pw.println("<a href='home.html'>Home</a>");
pw.println("<br>");
pw.println("<a href='bookList'>Book List</a>");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
doGet(req,res);
}
}
DeleteServlet .java file code
package com.idiot.servlet;
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 static final String query = "delete from BOOKDATA 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 of record
int id = Integer.parseInt(req.getParameter("id"));
//LOAD jdbc driver
try {
Class.forName("com.mysql.cj.jdbc.Driver");
}catch(ClassNotFoundException cnf) {
cnf.printStackTrace();
}
//generate the connection
try(Connection con = DriverManager.getConnection("jdbc:mysql:///book","root","root");
PreparedStatement ps = con.prepareStatement(query);){
ps.setInt(1, id);
int count = ps.executeUpdate();
if(count==1) {
pw.println("<h2>Record is Deleted Successfully</h2>");
}else {
pw.println("<h2>Record is not deleted Successfully</h2>");
}
}catch(SQLException se) {
se.printStackTrace();
pw.println("<h1>"+se.getMessage()+"</h2>");
}catch(Exception e) {
e.printStackTrace();
pw.println("<h1>"+e.getMessage()+"</h2>");
}
pw.println("<a href='home.html'>Home</a>");
pw.println("<br>");
pw.println("<a href='bookList'>Book List</a>");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
doGet(req,res);
}
}
Comments
Post a Comment