My assignment was to create a shopping cart servlet, it works locally on tomcat but when I deploy it to the schools server (also tomcat) it fails to refresh the front page and just prints the HTML Code not the page. Any thoughts or a helping hand would be appreciated.
Code:
import java.io.*;
import java.net.*;
import java.text.NumberFormat;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
/**
*
* @author Farrar
*/
public class Assign04 extends HttpServlet {
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
// uses the makeHTML() to create the page and session
response.setContentType("text/html");
HttpSession session = request.getSession();
PrintWriter out = response.getWriter();
out.println(makeHTML(session));
out.close();
}
/**
* Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
*/
protected void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
String msg;
HttpSession session = req.getSession(true);
PrintWriter out1 = res.getWriter();
NumberFormat formatter = NumberFormat.getCurrencyInstance();
if (req.getParameter("addclick") != null && req.getParameter("item") != null &&
req.getParameter("item").length() > 0)
{
String itemName = req.getParameter("item").toLowerCase().trim();
String qty = req.getParameter("quantity");
if (qty == null)
qty = "1";
if (session.getAttribute(itemName) == null)
{
session.setAttribute(itemName, Integer.parseInt(qty));
}
else
{
int oldQuantity = (Integer)session.getAttribute(itemName);
session.setAttribute(itemName, oldQuantity + Integer.parseInt(qty));
}
// sets oldItems to default to 0
int oldNumItems = 0;
// checks to see if numItem sess attrib is not null if it is oldItem is
// set to =(Integer)session.getAttribute("numItems")
if (session.getAttribute("numItems") != null)
oldNumItems = (Integer)session.getAttribute("numItems");
// checks to see if item parameter attrib is not null and is not empty takes the
//it incements the total Number of Items by the quantity 1
if (req.getParameter("item") != null && !req.getParameter("item").isEmpty())
session.setAttribute("numItems", Integer.parseInt(qty) + oldNumItems);
// sets oldTotalCost to 0.00 default
double oldTotalCost = 0.00;
// if totalCost sess attrib is not null oldTotalCost = session.getAttribute
if (session.getAttribute("totalCost") != null)
oldTotalCost = (Double)session.getAttribute("totalCost");
// logic for currentCost takes the itemName and multiplies it by 10
double currentCost = itemName.length() * 10.0* Integer.parseInt(qty);
// sets the session attribute totalCost to oldTotalCost plus
session.setAttribute("totalCost", oldTotalCost + currentCost);
out1.println(makeHTML(session));
}
// kills the session by invalidating it
else if (req.getParameter("checkoutclick") != null)
{
session.invalidate();
out1.println(makeCheckoutHTML(session));
}
//redirects the user to the view04 page to see the shopping list table
else if (req.getParameter("viewclick") != null )
{
res.sendRedirect("/FarrarJD04/servlet/assign04.View04");
}
else
{
out1.println(makeHTML(session));
}
}
//makes the checkout HTML page
public String makeCheckoutHTML(HttpSession session)
{
String docType =
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
"Transitional//EN\">\n";
String checkoutPage = docType +
"<html><head>" +
"</head>" +
"<body bgcolor='#C35817'>\n" +
"<p align=center><h4>Thank you for shopping! Goodbye!</h4>\n" +
// link for debugging
"<a href='/FarrarJD04/servlet/assign04.Assign04'>Go Shopping!</a></div>\n" +
"</body>\n" +
"</html>";
return checkoutPage;
}
// makes the HTMLpage used to for Assign04
public String makeHTML(HttpSession session)
{
String docType =
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
"Transitional//EN\">\n";
int numItems = 0;
//gets the session Attribute numItems if it is not null and casts that object to a
// interger called numItems.
if (session.getAttribute("numItems") != null)
numItems = (Integer)session.getAttribute("numItems");
double cost = 0.00;
//gets the session Attribute totalcost if it is not null and casts that object to a
// double called cost.
if (session.getAttribute("totalCost") != null)
cost = (Double)session.getAttribute("totalCost");
String itemPage = docType +
"<html><head>" +
"<title>Web Assignment 04</title>" +
"</head>" +
"<BODY BGCOLOR='#C35817' onLoad = document.itemInput.item.focus();>\n" +
"<center>\n" +
"<h1><Big><i>Joe Farrar's General Store</i></Big></h1>\n" +
"<h2>dont bother turning off the cookies </h2>\n" +
"<FORM name=itemInput Action='/FarrarJD04/servlet/assign04.Assign04' Method=Post>\n" +
"Item to buy <INPUT TYPE='TEXT' NAME='item'></INPUT>\n" +
"<INPUT TYPE='Hidden' name='quantity' value='1'></INPUT><br><br>\n" +
"<INPUT TYPE='SUBMIT' name='addclick' VALUE='Add to Cart'></INPUT>\n" +
"<INPUT TYPE='SUBMIT' name='viewclick' VALUE='View Cart'></INPUT>\n" +
"<INPUT TYPE='SUBMIT' name='checkoutclick' VALUE='Checkout'></INPUT><br><br>\n" +
"</form>\n" +
"<table align='center'>\n" +
"<tr>" +
"<th>Item Count</th>\n" +
"<th>Total Cost</th>\n" +
"</tr>" +
"<tr>" +
"<td>" + numItems + "</td>\n" +
"<td>" + cost + "0</td>\n" +
"</tr>\n" +
"</table>\n" +
"</center>\n" +
"</body>\n" +
"</html>";
return itemPage;
}
}
Comment