Transcript Ch24

Chapter 24
The Admin application
Murach’s Java Servlets/JSP (2nd Ed.), C24
© 2008, Mike Murach & Associates, Inc.
Slide 1
The URL for accessing the Admin application
without SSL
http://localhost:8080/musicStore/admin
The URL for accessing the Admin application with
SSL
https://localhost:8443/musicStore/admin
Murach’s Java Servlets/JSP (2nd Ed.), C24
© 2008, Mike Murach & Associates, Inc.
Slide 2
The Index page for the Admin application
Murach’s Java Servlets/JSP (2nd Ed.), C24
© 2008, Mike Murach & Associates, Inc.
Slide 3
The structure of the Admin application
index.jsp
music.admin package
DisplayInvoicesServlet
invoices.jsp
DisplayInvoiceServlet
invoice.jsp
ProcessInvoiceServlet
reports.jsp
parameters.jsp
DisplayReportServlet
table.xls
Murach’s Java Servlets/JSP (2nd Ed.), C24
© 2008, Mike Murach & Associates, Inc.
Slide 4
The Login page
Murach’s Java Servlets/JSP (2nd Ed.), C24
© 2008, Mike Murach & Associates, Inc.
Slide 5
The Invoices page
Murach’s Java Servlets/JSP (2nd Ed.), C24
© 2008, Mike Murach & Associates, Inc.
Slide 6
The Invoice page
Murach’s Java Servlets/JSP (2nd Ed.), C24
© 2008, Mike Murach & Associates, Inc.
Slide 7
The DisplayInvoicesServlet class
package music.admin;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.ArrayList;
import music.data.*;
import music.business.*;
public class DisplayInvoicesServlet extends HttpServlet
{
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
HttpSession session = request.getSession();
Murach’s Java Servlets/JSP (2nd Ed.), C24
© 2008, Mike Murach & Associates, Inc.
Slide 8
The DisplayInvoicesServlet class (cont.)
ArrayList<Invoice> unprocessedInvoices =
InvoiceDB.selectUnprocessedInvoices();
if (unprocessedInvoices == null ||
unprocessedInvoices.size() <= 0)
{
response.sendRedirect("/musicStore/admin");
}
else
{
session.setAttribute(
"unprocessedInvoices", unprocessedInvoices);
String url = "/admin/invoices.jsp";
RequestDispatcher dispatcher =
getServletContext().getRequestDispatcher(url);
dispatcher.forward(request, response);
}
}
Murach’s Java Servlets/JSP (2nd Ed.), C24
© 2008, Mike Murach & Associates, Inc.
Slide 9
The DisplayInvoicesServlet class
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
doGet(request, response);
}
}
Murach’s Java Servlets/JSP (2nd Ed.), C24
© 2008, Mike Murach & Associates, Inc.
Slide 10
The invoices.jsp file
<jsp:include page="/includes/header.html" />
<jsp:include page="/includes/column_left_no_links.jsp" />
<!-- begin middle column -->
<td valign="top">
<%@ taglib prefix="c"
uri="http://java.sun.com/jsp/jstl/core" %>
<h1>Invoices to be processed:</h1>
<table>
<tr>
<td width="100"></td>
<td width="150"><b>Customer Name</b></td>
<td><b>Invoice Date</b></td>
</tr>
Murach’s Java Servlets/JSP (2nd Ed.), C24
© 2008, Mike Murach & Associates, Inc.
Slide 11
The invoices.jsp file (cont.)
<c:forEach var="invoice" items="${unprocessedInvoices}">
<tr>
<td>
<a href="displayInvoice?invoiceNumber=
${invoice.invoiceNumber}">Click to View
</a>
</td>
<td>${invoice.user.firstName}
${invoice.user.lastName}</td>
<td>${invoice.invoiceDateDefaultFormat}</td>
</tr>
</c:forEach>
</table>
<br>
Murach’s Java Servlets/JSP (2nd Ed.), C24
© 2008, Mike Murach & Associates, Inc.
Slide 12
The invoices.jsp file (cont.)
<form action="index.jsp" method="post">
<input type=submit value="Go Back to Menu">
</form>
</td>
<!-- end middle column -->
<jsp:include page="/includes/footer.jsp" />
Murach’s Java Servlets/JSP (2nd Ed.), C24
© 2008, Mike Murach & Associates, Inc.
Slide 13
The DisplayInvoiceServlet class
package music.admin;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.ArrayList;
import music.business.*;
public class DisplayInvoiceServlet extends HttpServlet
{
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
HttpSession session = request.getSession();
Murach’s Java Servlets/JSP (2nd Ed.), C24
© 2008, Mike Murach & Associates, Inc.
Slide 14
The DisplayInvoiceServlet class (cont.)
String invoiceNumberString =
request.getParameter("invoiceNumber");
int invoiceNumber =
Integer.parseInt(invoiceNumberString);
ArrayList<Invoice> unprocessedInvoices =
(ArrayList<Invoice>)
session.getAttribute("unprocessedInvoices");
Invoice invoice = null;
for (int i = 0; i < unprocessedInvoices.size(); i++)
{
invoice = unprocessedInvoices.get(i);
if (invoice.getInvoiceNumber() == invoiceNumber)
{
break;
}
}
Murach’s Java Servlets/JSP (2nd Ed.), C24
© 2008, Mike Murach & Associates, Inc.
Slide 15
The DisplayInvoiceServlet class (cont.)
User user = invoice.getUser();
ArrayList<LineItem> lineItems =
invoice.getLineItems();
session.setAttribute("user", user);
session.setAttribute("invoice", invoice);
Cart cart = new Cart();
cart.setItems(lineItems);
session.setAttribute("cart", cart);
String url = "/admin/invoice.jsp";
RequestDispatcher dispatcher =
getServletContext().getRequestDispatcher(url);
dispatcher.forward(request, response);
}
Murach’s Java Servlets/JSP (2nd Ed.), C24
© 2008, Mike Murach & Associates, Inc.
Slide 16
The DisplayInvoiceServlet class (cont.)
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
doGet(request, response);
}
}
Murach’s Java Servlets/JSP (2nd Ed.), C24
© 2008, Mike Murach & Associates, Inc.
Slide 17
The invoice.jsp file
<td valign="top">
<%@ taglib prefix="c"
uri="http://java.sun.com/jsp/jstl/core" %>
<h1>Your invoice</h1>
<table border="0" cellspacing="5">
<tr>
<td><b>Date:</b></td>
<td width="400">${invoice.invoiceDate}</td>
<td></td>
</tr>
<tr valign="top">
<td><b>Ship To:</b></td>
<td>${user.addressHTMLFormat}</td>
<td></td>
</tr>
<tr><td colspan="3"><hr></td></tr>
Murach’s Java Servlets/JSP (2nd Ed.), C24
© 2008, Mike Murach & Associates, Inc.
Slide 18
The invoice.jsp file (cont.)
<tr>
<td><b>Qty</b></td>
<td><b>Description</b></td>
<td><b>Price</b></td>
</tr>
<c:forEach var="item" items="${invoice.lineItems}">
<tr>
<td>${item.quantity}</td>
<td>${item.product.description}</td>
<td>${item.totalCurrencyFormat}</td>
</tr>
</c:forEach>
<tr><td colspan="3"><hr></td></tr>
<tr>
<td><b>Total:</b></td>
<td></td>
<td><p>${invoice.invoiceTotalCurrencyFormat}</td>
</tr>
Murach’s Java Servlets/JSP (2nd Ed.), C24
© 2008, Mike Murach & Associates, Inc.
Slide 19
The invoice.jsp file (cont.)
<tr>
<td><b>Payment information:</b></td>
<td>${user.creditCardType}: ${user.creditCardNumber}
(${user.creditCardExpirationDate})</td>
</tr>
<tr>
<td><b>Email Address:</b></td>
<td><p>${user.emailAddress}</td>
</tr>
</table>
<form action="processInvoice" method="post">
<input type="submit" value="Process Invoice">
</form>
<form action="invoices.jsp" method="post">
<input type="submit" value="View Unprocessed Invoices">
</form>
</td>
Murach’s Java Servlets/JSP (2nd Ed.), C24
© 2008, Mike Murach & Associates, Inc.
Slide 20
The ProcessInvoiceServlet class
package music.admin;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import music.data.*;
import music.business.*;
public class ProcessInvoiceServlet extends HttpServlet
{
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
HttpSession session = request.getSession();
Murach’s Java Servlets/JSP (2nd Ed.), C24
© 2008, Mike Murach & Associates, Inc.
Slide 21
The ProcessInvoiceServlet class (cont.)
Invoice invoice =
(Invoice) session.getAttribute("invoice");
int invoiceID = invoice.getInvoiceNumber();
InvoiceDB.updateInvoiceIsProcessed(invoiceID);
String url = "/admin/displayInvoices";
RequestDispatcher dispatcher =
getServletContext().getRequestDispatcher(url);
dispatcher.forward(request, response);
}
}
Murach’s Java Servlets/JSP (2nd Ed.), C24
© 2008, Mike Murach & Associates, Inc.
Slide 22
The Reports page
Murach’s Java Servlets/JSP (2nd Ed.), C24
© 2008, Mike Murach & Associates, Inc.
Slide 23
The Parameters page
Murach’s Java Servlets/JSP (2nd Ed.), C24
© 2008, Mike Murach & Associates, Inc.
Slide 24
The User Email report
Murach’s Java Servlets/JSP (2nd Ed.), C24
© 2008, Mike Murach & Associates, Inc.
Slide 25
The Invoice Summary report
Murach’s Java Servlets/JSP (2nd Ed.), C24
© 2008, Mike Murach & Associates, Inc.
Slide 26
The Invoice Detail report
Murach’s Java Servlets/JSP (2nd Ed.), C24
© 2008, Mike Murach & Associates, Inc.
Slide 27
The DisplayReportServlet class
package music.admin;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import music.data.*;
public class DisplayReportServlet extends HttpServlet
{
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("application/vnd.ms-excel");
PrintWriter out = response.getWriter();
Murach’s Java Servlets/JSP (2nd Ed.), C24
© 2008, Mike Murach & Associates, Inc.
Slide 28
The DisplayReportServlet class (cont.)
String reportName =
request.getParameter("reportName");
String reportTitle =
request.getParameter("reportTitle");
String startDate =
request.getParameter("startDate");
String endDate = request.getParameter("endDate");
String reportString = null;
if (reportName.equalsIgnoreCase("userEmail"))
{
reportString =
ReportDB.getUserEmail(reportTitle);
}
else if
(reportName.equalsIgnoreCase("downloadDetail"))
{
reportString = ReportDB.getDownloadDetail(
reportTitle, startDate, endDate);
}
Murach’s Java Servlets/JSP (2nd Ed.), C24
© 2008, Mike Murach & Associates, Inc.
Slide 29
The DisplayReportServlet class (cont.)
else if
(reportName.equalsIgnoreCase("invoiceSummary"))
{
reportString = ReportDB.getOrderSummary(
reportTitle, startDate, endDate);
}
else if
(reportName.equalsIgnoreCase("invoiceDetail"))
{
reportString = ReportDB.getOrderDetail(
reportTitle, startDate, endDate);
}
out.println(reportString);
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
doGet(request, response);
}
}
Murach’s Java Servlets/JSP (2nd Ed.), C24
© 2008, Mike Murach & Associates, Inc.
Slide 30
The ReportDB class
package music.data;
import java.sql.*;
public class ReportDB
{
// The Order Summary report
public static String getOrderSummary(String reportTitle,
String startDate, String endDate)
{
ConnectionPool pool = ConnectionPool.getInstance();
Connection connection = pool.getConnection();
Statement statement = null;
ResultSet rs = null;
Murach’s Java Servlets/JSP (2nd Ed.), C24
© 2008, Mike Murach & Associates, Inc.
Slide 31
The ReportDB class (cont.)
String query =
"SELECT ProductCode, ProductDescription, "
+ "
ProductPrice, Quantity, "
+ "
SUM(Quantity) AS ProductQuantity, "
+ "
SUM(ProductPrice*Quantity) AS ProductTotal "
+ "FROM Invoice "
+ "
INNER JOIN LineItem "
+ "
ON Invoice.InvoiceID = LineItem.InvoiceID "
+ "
INNER JOIN Product "
+ "
ON LineItem.ProductID = Product.ProductID "
+ "WHERE InvoiceDate >= '" + startDate + "' "
+ "
AND InvoiceDate <= '" + endDate + "' "
+ "GROUP BY ProductCode, ProductDescription "
+ "ORDER BY ProductTotal DESC";
Murach’s Java Servlets/JSP (2nd Ed.), C24
© 2008, Mike Murach & Associates, Inc.
Slide 32
The ReportDB class (cont.)
try
{
statement = connection.createStatement();
rs = statement.executeQuery(query);
String d = "\t";
StringBuilder report = new StringBuilder(
reportTitle + "\n\n"
+ "Start Date: " + startDate + "\n"
+ "End Date: " + endDate + "\n\n"
+ "ProductCode" + d
+ "ProductDescription" + d
+ "ProductPrice" + d
+ "Quantity" + d
+ "Total" + "\n");
Murach’s Java Servlets/JSP (2nd Ed.), C24
© 2008, Mike Murach & Associates, Inc.
Slide 33
The ReportDB class (cont.)
while (rs.next())
{
report.append(
rs.getString("ProductCode") + d
+ rs.getString("ProductDescription") + d
+ rs.getDouble("ProductPrice") + d
+ rs.getInt("ProductQuantity") + d
+ rs.getDouble("ProductTotal") + "\n");
}
return report.toString();
}
catch(SQLException e)
{
e.printStackTrace();
return null;
}
Murach’s Java Servlets/JSP (2nd Ed.), C24
© 2008, Mike Murach & Associates, Inc.
Slide 34
The ReportDB class (cont.)
finally
{
DBUtil.closeResultSet(rs);
DBUtil.closeStatement(statement);
pool.freeConnection(connection);
}
}
// the methods for the other reports
}
Murach’s Java Servlets/JSP (2nd Ed.), C24
© 2008, Mike Murach & Associates, Inc.
Slide 35