Create a JSP/Servlet based application to maintain a Customer Table with the fol
ID: 3719987 • Letter: C
Question
Create a JSP/Servlet based application to maintain a Customer Table with the following specifications. 1. [2] The Customer Table has the following fields: ID, Name, Address, Amount_Due, Preferred_Status. Create 5 records manually – have at least 2 preferred customers (DO NOT write java code to add data at this point) 2. [3] The GUI has radio button for Preferred_Status and uses text boxes for the rest.3. [10] The user can Search, Add and Modify Customer information. The user always starts with a Search. if a customer does not exist, you can add one; if the customer exists, you can modify contents. Search/Modify [Works - Coded not working - Not Attempted] Add 4. [5] When the user comes back to use the system, the ID field should be filled with the ID of the last customer whose data was accessed.
5. [7] If the searched customer’s balance is less than $100, the background for the Modify form should be Red for customers with Preferred_Status of ‘false’. Otherwise, the background is always white. (accomplish using a bean). 6. [3] Get the connection string from web.xml Create a JSP/Servlet based application to maintain a Customer Table with the following specifications. 1. [2] The Customer Table has the following fields: ID, Name, Address, Amount_Due, Preferred_Status. Create 5 records manually – have at least 2 preferred customers (DO NOT write java code to add data at this point) 2. [3] The GUI has radio button for Preferred_Status and uses text boxes for the rest.
3. [10] The user can Search, Add and Modify Customer information. The user always starts with a Search. if a customer does not exist, you can add one; if the customer exists, you can modify contents. Search/Modify [Works - Coded not working - Not Attempted] Add 4. [5] When the user comes back to use the system, the ID field should be filled with the ID of the last customer whose data was accessed.
5. [7] If the searched customer’s balance is less than $100, the background for the Modify form should be Red for customers with Preferred_Status of ‘false’. Otherwise, the background is always white. (accomplish using a bean). 6. [3] Get the connection string from web.xml Create a JSP/Servlet based application to maintain a Customer Table with the following specifications. 1. [2] The Customer Table has the following fields: ID, Name, Address, Amount_Due, Preferred_Status. Create 5 records manually – have at least 2 preferred customers (DO NOT write java code to add data at this point) 2. [3] The GUI has radio button for Preferred_Status and uses text boxes for the rest.
3. [10] The user can Search, Add and Modify Customer information. The user always starts with a Search. if a customer does not exist, you can add one; if the customer exists, you can modify contents. Search/Modify [Works - Coded not working - Not Attempted] Add 4. [5] When the user comes back to use the system, the ID field should be filled with the ID of the last customer whose data was accessed.
5. [7] If the searched customer’s balance is less than $100, the background for the Modify form should be Red for customers with Preferred_Status of ‘false’. Otherwise, the background is always white. (accomplish using a bean). 6. [3] Get the connection string from web.xml
Explanation / Answer
ANSWER:
According to the given data the below procedure follows ;
The JSP/servlet that is with respect to the given data as shown
Java script:
The below represents the data of customer
Customer.java
Customer.java
Bean class
public class Customer {
protected int id;
protected String name;
protected String address;
protected float amountDue;
protected boolean preferredStatus;
public Customer() {
}
public Customer(int id) {
this.id = id;
}
public Customer(String name, String address, float amountDue,
boolean preferredStatus) {
this.name = name;
this.address = address;
this.amountDue = amountDue;
this.preferredStatus = preferredStatus;
}
public Customer(int id, String name, String address, float amountDue,
boolean preferredStatus) {
this.id = id;
this.name = name;
this.address = address;
this.amountDue = amountDue;
this.preferredStatus = preferredStatus;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public float getAmountDue() {
return amountDue;
}
public void setAmountDue(float amountDue) {
this.amountDue = amountDue;
}
public boolean isPreferredStatus() {
return preferredStatus;
}
public void setPreferredStatus(boolean preferredStatus) {
this.preferredStatus = preferredStatus;
}
}
->CustomerControllerServlet.java
package com.chegg.webapplication;
import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
-> CustomerControllerServlet.java
Hence,
The below servlets been acts as a page controller for the Customer Application
public class CustomerControllerServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private CustomerDAO customerDAO;
public void init() {
String jdbcURL = getServletContext().getInitParameter("jdbcURL");
String jdbcUsername = getServletContext().getInitParameter("jdbcUsername");
String jdbcPassword = getServletContext().getInitParameter("jdbcPassword");
customerDAO = new CustomerDAO(jdbcURL, jdbcUsername, jdbcPassword);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String action = request.getServletPath();
try {
switch (action) {
case "/new":
showNewForm(request, response);
break;
case "/insert":
insertCustomer(request, response);
break;
case "/edit":
showEditForm(request, response);
break;
case "/update":
updateCustomer(request, response);
break;
default:
listCustomer(request, response);
break;
}
} catch (SQLException ex) {
throw new ServletException(ex);
}
}
private void listCustomer(HttpServletRequest request, HttpServletResponse response)
throws SQLException, IOException, ServletException {
List<Customer> listCustomer = customerDAO.listAllCustomers();
request.setAttribute("listCustomer", listCustomer);
RequestDispatcher dispatcher = request.getRequestDispatcher("CustomerList.jsp");
dispatcher.forward(request, response);
}
private void showNewForm(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
RequestDispatcher dispatcher = request.getRequestDispatcher("CustomerForm.jsp");
dispatcher.forward(request, response);
}
private void showEditForm(HttpServletRequest request, HttpServletResponse response)
throws SQLException, ServletException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
Customer existingCustomer = customerDAO.getCustomer(id);
RequestDispatcher dispatcher = request.getRequestDispatcher("CustomerForm.jsp");
request.setAttribute("customer", existingCustomer);
dispatcher.forward(request, response);
}
private void insertCustomer(HttpServletRequest request, HttpServletResponse response)
throws SQLException, IOException {
String name = request.getParameter("name");
String address = request.getParameter("address");
float amountDue = Float.parseFloat(request.getParameter("amountDue"));
String boolStatus = request.getParameter("preferredStatus");
boolean preferredStatus = (boolStatus == "True" ? true : false);
Customer newCustomer = new Customer(name, address, amountDue, preferredStatus);
customerDAO.insertCustomer(newCustomer);
response.sendRedirect("list");
}
private void updateCustomer(HttpServletRequest request, HttpServletResponse response)
throws SQLException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
String name = request.getParameter("name");
String address = request.getParameter("address");
float amountDue = Float.parseFloat(request.getParameter("amountDue"));
String boolStatus = request.getParameter("preferredStatus");
boolean preferredStatus = (boolStatus == "True" ? true : false);
Customer customerToUpdate = new Customer(id, name, address, amountDue, preferredStatus);
customerDAO.updateCustomer(customerToUpdate);
response.sendRedirect("list");
}
}
->CustomerDAO.java
package com.chegg.webapplication;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
CustomerDAO.java
Thus this,DAO provides the list like add,modify and then the search for the customer
public class CustomerDAO {
private String jdbcURL;
private String jdbcUsername;
private String jdbcPassword;
private Connection jdbcConnection;
public CustomerDAO(String jdbcURL, String jdbcUsername, String jdbcPassword) {
this.jdbcURL = jdbcURL;
this.jdbcUsername = jdbcUsername;
this.jdbcPassword = jdbcPassword;
}
public boolean insertCustomer(Customer customer) throws SQLException {
String sql = "INSERT INTO Customer (Name, Address, AmountDue, PreferredStatus) VALUES (?, ?, ?, ?)";
connect();
PreparedStatement statement = jdbcConnection.prepareStatement(sql);
statement.setString(1, customer.getName());
statement.setString(2, customer.getAddress());
statement.setFloat(3, customer.getAmountDue());
statement.setBoolean(4, customer.preferredStatus );
boolean rowInserted = statement.executeUpdate() > 0;
statement.close();
disconnect();
return rowInserted;
}
public List<Customer> listAllCustomers() throws SQLException {
List<Customer> listCustomer = new ArrayList<>();
String sql = "SELECT * FROM Customer";
connect();
Statement statement = jdbcConnection.createStatement();
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()) {
int id = resultSet.getInt("ID");
String name = resultSet.getString("(Name");
String address = resultSet.getString("Address");
float amountDue = resultSet.getFloat("AmountDue");
boolean preferredStatus = resultSet.getBoolean("PreferredStatus");
Customer customer = new Customer(id, name, address, amountDue,preferredStatus);
listCustomer.add(customer);
}
resultSet.close();
statement.close();
disconnect();
return listCustomer;
}
public boolean updateCustomer(Customer customer) throws SQLException {
String sql = "UPDATE Customer SET Name = ?, Address = ?, AmountDue = ?, PreferredStatus =?";
sql += " WHERE ID = ?";
connect();
PreparedStatement statement = jdbcConnection.prepareStatement(sql);
statement.setString(1, customer.getName());
statement.setString(2, customer.getAddress());
statement.setFloat(3, customer.getAmountDue());
statement.setBoolean(4, customer.preferredStatus);
statement.setInt(5, customer.getId());
boolean rowUpdated = statement.executeUpdate() > 0;
statement.close();
disconnect();
return rowUpdated;
}
public Customer getCustomer(int id) throws SQLException {
Customer customer = null;
String sql = "SELECT * FROM Customer WHERE ID = ?";
connect();
PreparedStatement statement = jdbcConnection.prepareStatement(sql);
statement.setInt(1, id);
ResultSet resultSet = statement.executeQuery();
if (resultSet.next()) {
String name = resultSet.getString("(Name");
String address = resultSet.getString("Address");
float amountDue = resultSet.getFloat("AmountDue");
boolean preferredStatus = resultSet.getBoolean("PreferredStatus");
customer = new Customer(name, address, amountDue,preferredStatus);
}
resultSet.close();
statement.close();
return customer;
}
protected void connect() throws SQLException {
if (jdbcConnection == null || jdbcConnection.isClosed()) {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
throw new SQLException(e);
}
jdbcConnection = DriverManager.getConnection(
jdbcURL, jdbcUsername, jdbcPassword);
}
}
protected void disconnect() throws SQLException {
if (jdbcConnection != null && !jdbcConnection.isClosed()) {
jdbcConnection.close();
}
}
}
->CustomerStore.sql
This represents the structured query language of the customer;
CREATE DATABASE 'CustomerStore';
USE CustomerStore;
CREATE TABLE `Customer` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`Name` varchar(128) NOT NULL,
`Address` varchar(45) NOT NULL,
`AmountDue` float NOT NULL,
`PreferredStatus` boolean NOT NULL,
PRIMARY KEY (`ID`),
UNIQUE KEY `ID_UNIQUE` (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=latin1
->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_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>Customer Web Application</display-name>
<context-param>
<param-name>jdbcURL</param-name>
<param-value>jdbc:mysql://localhost:4040/CustomerStore</param-value>
</context-param>
<context-param>
<param-name>jdbcUsername</param-name>
<param-value>hello123</param-value>
</context-param>
<context-param>
<param-name>jdbcPassword</param-name>
<param-value>hello123</param-value>
</context-param>
<servlet>
<servlet-name>CustomerControllerServlet</servlet-name>
<servlet-class>com.chegg.webapplication.CustomerControllerServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CustomerControllerServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/Error.jsp</location>
</error-page>
</web-app>
->CustomerForm.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>Customer Application</title>
</head>
<body>
<center>
<h1>Customer Management</h1>
<h2>
<a href="new">Add New Customer</a>
<a href="list">List All Customers</a>
</h2>
</center>
<div align="center">
<c:if test="${customer != null}">
<form action="update" method="post">
</c:if>
<c:if test="${customer == null}">
<form action="insert" method="post">
</c:if>
<table border="1" cellpadding="5">
<caption>
<h2>
<c:if test="${customer != null}">
Edit customer
</c:if>
<c:if test="${customer == null}">
Add New Customer
</c:if>
</h2>
</caption>
<c:if test="${customer != null}">
<input type="hidden" name="id" value="<c:out value='${customer.id}' />" />
</c:if>
<tr>
<th>Title: </th>
<td>
<input type="text" name="name" size="45"
value="<c:out value='${customer.name}' />"
/>
</td>
</tr>
<tr>
<th>Address: </th>
<td>
<input type="text" name="address" size="45"
value="<c:out value='${customer.address}' />"
/>
</td>
</tr>
<tr>
<th>Amount Due: </th>
<td>
<input type="text" name="amountDue" size="5"
value="<c:out value='${customer.amountDue}' />"
/>
</td>
</tr>
<tr>
<th>Preferred Status: </th>
<td>
<input type="text" name="preferredStatus" size="5"
value="<c:out value='${customer.preferredStatus}' />"
/>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="Save" />
</td>
</tr>
</table>
</form>
</div>
</body>
</html>
->CustomerList.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>Customer Application</title>
</head>
<body>
<center>
<h1>Customer Management</h1>
<h2>
<a href="new">Add New Customer</a>
<a href="list">List All Customers</a>
</h2>
</center>
<div align="center">
<table border="1" cellpadding="5">
<caption><h2>List of Customers</h2></caption>
<tr>
<th>ID</th>
<th>Name</th>
<th>Address</th>
<th>Amount Due</th>
<th>Preferred Status</th>
</tr>
<c:forEach var="customer" items="${listCustomer}">
<tr>
<td><c:out value="${customer.id}" /></td>
<td><c:out value="${customer.name}" /></td>
<td><c:out value="${customer.address}" /></td>
<td><c:out value="${customer.amountDue}" /></td>
<td><c:out value="${customer.preferredStatus}" /></td>
<td>
<a href="edit?id=<c:out value='${customer.id}' />">Modify</a>
</td>
</tr>
</c:forEach>
</table>
</div>
</body>
</html>
->CustomerSearch.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>Customer Application</title>
</head>
<body>
<center>
<h1>Customer Management</h1><br>
<form action ="edit" method="post">
Search Customer by ID: <input type="text" name="id">
<input type ="submit" value="Search">
</form>
</center>
<div align="center">
<c:if test="${customer != null}">
<form action="update" method="post">
</c:if>
<c:if test="${customer == null}">
<form action="insert" method="post">
</c:if>
<table border="1" cellpadding="5">
<caption>
<h2>
<c:if test="${customer != null}">
Modify customer
</c:if>
<c:if test="${customer == null}">
Add New Customer
</c:if>
</h2>
</caption>
<c:if test="${customer != null}">
<input type="hidden" name="id" value="<c:out value='${customer.id}' />" />
</c:if>
<tr>
<th>Name : </th>
<td>
<input type="text" name="title" size="45"
value="<c:out value='${customer.name}' />"
/>
</td>
</tr>
<tr>
<th>Address: </th>
<td>
<input type="text" name="address" size="45"
value="<c:out value='${customer.address}' />"
/>
</td>
</tr>
<tr>
<th>Amount Due: </th>
<td>
<input type="text" name="amountDue" size="5"
value="<c:out value='${customer.amountDue}' />"
/>
</td>
</tr>
<tr>
<th>Preferred Status: </th>
<td>
<input type="text" name="preferredStatus" size="5"
value="<c:out value='${customer.preferredStatus}' />"
/>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="Save" />
</td>
</tr>
</table>
</form>
</div>
</body>
</html>
->Error.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isErrorPage="true" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Error</title>
</head>
<body>
<center>
<h1>Error</h1>
<h2><%=exception.getMessage() %><br/> </h2>
</center>
</body>
</html>
->pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.chegg.webapplication</groupId>
<artifactId>CustomerWebapp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.30</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
</project>