MITS5502 Project: Developing Product Maintenance System (Parts 3-6)

Verified

Added on  2023/01/17

|27
|3596
|63
Project
AI Summary
This document provides a comprehensive solution for the MITS5502 Developing Enterprise Systems project, focusing on product maintenance within a web application. The project is divided into several parts, starting with database integration using JDBC (Java Database Connectivity) and connection pooling in Part 3. Part 4 transitions to the Java Persistence API (JPA) and EclipseLink for database interactions. Part 5 implements secure connections using SSL and authentication to restrict access to sensitive pages. Finally, Part 6 integrates the product maintenance functionality into a Music Store web site, adding an admin section with JSP files, CSS, and modified Java files, including the AdminController class. The solution includes code snippets, screenshots, and references to relevant literature.
Document Page
MITS5502
(Research/CaseStudy Title)
(Student Full Name)
(Student ID)
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
Contents
Part 3 Solution............................................................................................................................................2
Add product to shopping cart..................................................................................................................2
Edit product in cart..................................................................................................................................2
Remove product from cart......................................................................................................................3
Sample codes...........................................................................................................................................3
The ProductDB class............................................................................................................................3
The ConnectionPool class....................................................................................................................5
The updated CartServlet class.............................................................................................................6
Part 4 Solution............................................................................................................................................9
Add product to shopping cart..................................................................................................................9
Edit product in cart..................................................................................................................................9
Remove product from cart....................................................................................................................10
The project structure.............................................................................................................................10
The ProductDB class..........................................................................................................................10
The Product class...............................................................................................................................12
The DBUtil class.................................................................................................................................14
Part 5 Solution..........................................................................................................................................16
Index page.............................................................................................................................................16
Authenticated products page using https protocol...............................................................................16
On successful authentication.................................................................................................................17
Project Structure...................................................................................................................................17
Content.xml file for DB configurations..............................................................................................18
Web.xml file for Security configurations............................................................................................18
Stage 6 Solution........................................................................................................................................20
Migrated jsp files...................................................................................................................................20
Migrated Controller Servlet...................................................................................................................21
Modified index.jsp.................................................................................................................................21
Fortmatted jsp with css.........................................................................................................................22
AdminController class with getRequestUri method..............................................................................22
References................................................................................................................................................26
Page 1 of 28
Document Page
Page 2 of 28
Document Page
Part 3 Solution
This part of the project has implemented the use of databases in storing products data. Java
Database Connectivity is an application programming interface for the programming language
Java, which characterizes how a customer may get to a database (Chatterjee, Juvekar and Sen,
2009). JDBC has been used together with connection pool. ProductDB class is used in adding,
updating and deleting products. Below are sample screenshots and code samples that shows
this implementation.
Add product to shopping cart
Edit product in cart
Page 3 of 28
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
Remove product from cart
Sample codes
The ProductDB class
package murach.data;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import murach.business.Product;
public class ProductDB {
//This method returns null if a product isn't found.
public static Product selectProduct(String productCode) {
ConnectionPool pool = ConnectionPool.getInstance();
Connection connection = pool.getConnection();
PreparedStatement ps = null;
ResultSet rs = null;
String query = "SELECT * FROM product "
+ "WHERE productcode = ?";
try {
ps = connection.prepareStatement(query);
ps.setString(1, productCode);
rs = ps.executeQuery();
if (rs.next()) {
Product p = new Product();
p.setCode(rs.getString("productcode"));
p.setDescription(rs.getString("productdescription"));
Page 4 of 28
Document Page
p.setPrice(rs.getDouble("productprice"));
System.out.println("Product "+p.getCode());
return p;
} else {
return null;
}
} catch (SQLException e) {
System.err.println(e);
return null;
} finally {
DBUtil.closeResultSet(rs);
DBUtil.closePreparedStatement(ps);
pool.freeConnection(connection);
}
}
//This method returns null if a product isn't found.
public static List<Product> selectProducts() {
ConnectionPool pool = ConnectionPool.getInstance();
Connection connection = pool.getConnection();
PreparedStatement ps = null;
ResultSet rs = null;
String query = "SELECT * FROM product";
try {
ps = connection.prepareStatement(query);
rs = ps.executeQuery();
ArrayList<Product> products = new ArrayList();
while (rs.next()) {
Product p = new Product();
p.setCode(rs.getString("productpode"));
p.setDescription(rs.getString("productpescription"));
p.setPrice(rs.getDouble("productprice"));
products.add(p);
}
return products;
} catch (SQLException e) {
System.err.println(e);
return null;
} finally {
DBUtil.closeResultSet(rs);
DBUtil.closePreparedStatement(ps);
Page 5 of 28
Document Page
pool.freeConnection(connection);
}
}
}
The ConnectionPool class
package murach.data;
import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class ConnectionPool {
private static ConnectionPool pool = null;
private static DataSource dataSource = null;
public synchronized static ConnectionPool getInstance() {
if (pool == null) {
pool = new ConnectionPool();
}
return pool;
}
private ConnectionPool() {
try {
InitialContext ic = new InitialContext();
dataSource = (DataSource) ic.lookup("java:/comp/env/jdbc/musicDB");
} catch (NamingException e) {
System.err.println(e);
}
}
public Connection getConnection() {
try {
return dataSource.getConnection();
} catch (SQLException sqle) {
System.err.println(sqle);
return null;
}
}
Page 6 of 28
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
public void freeConnection(Connection c) {
try {
c.close();
} catch (SQLException sqle) {
System.err.println(sqle);
}
}
}
The updated CartServlet class
package murach.cart;
//import murach.data.ProductIO;
import murach.business.LineItem;
import murach.business.Cart;
import murach.business.Product;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import murach.data.ProductDB;
public class CartServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
String url = "/index.jsp";
ServletContext sc = getServletContext();
// get current action
String action = request.getParameter("action");
if (action == null) {
action = "cart"; // default action
}
// perform action and set URL to appropriate page
if (action.equals("shop")) {
url = "/index.jsp"; // the "index" page
}
else if (action.equals("cart")) {
Page 7 of 28
Document Page
String productCode = request.getParameter("productCode");
String quantityString = request.getParameter("quantity");
HttpSession session = request.getSession();
Cart cart = (Cart) session.getAttribute("cart");
if (cart == null) {
cart = new Cart();
}
//if the user enters a negative or invalid quantity,
//the quantity is automatically reset to 1.
int quantity;
try {
quantity = Integer.parseInt(quantityString);
if (quantity < 0) {
quantity = 1;
}
} catch (NumberFormatException nfe) {
quantity = 1;
}
// String path = sc.getRealPath("/WEB-INF/products.txt");
// Product product = ProductIO.getProduct(productCode, path);
//introduce new db implementation
Product product = ProductDB.selectProduct(productCode);
LineItem lineItem = new LineItem();
lineItem.setProduct(product);
lineItem.setQuantity(quantity);
// System.out.println("Line item "+lineItem.getProduct().getCode());
if (quantity > 0) {
cart.addItem(lineItem);
} else if (quantity == 0) {
cart.removeItem(lineItem);
}
session.setAttribute("cart", cart);
url = "/cart.jsp";
}
else if (action.equals("checkout")) {
url = "/checkout.jsp";
}
sc.getRequestDispatcher(url)
Page 8 of 28
Document Page
.forward(request, response);
}
@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
}
Page 9 of 28
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
Part 4 Solution
For this part, the application has been converted to use JPA while working with the database.
The Java Persistence API is a Java application programming interface particular that depicts the
administration of social information in applications utilizing the likes of Java Platform and
Standard Edition (Lai and ZhongZhi, 2010). JPA annotations have been added to class Product
and EclipseLink JPA provider used in ProductDB class. Below are sample screenshots and code
samples that shows this implementation.
Add product to shopping cart
Edit product in cart
Page 10 of 28
Document Page
Remove product from cart
The project structure
The ProductDB class
package murach.data;
import java.util.List;
Page 11 of 28
chevron_up_icon
1 out of 27
circle_padding
hide_on_mobile
zoom_out_icon
[object Object]