By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,665 Members | 2,212 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,665 IT Pros & Developers. It's quick & easy.

Servlet Problem

P: 24
Please I need help with this so bad. I have been struggling with it for 2weeks now.

on line 206, I what to create a link that will direct you to the detail of the chosen items, like in the screenshot



Yes, I know the rules. This is a home and the class is for graduate student and I am an undergraduate. the teacher doesn't render any help whatsoever. check the comment on her rating,the last comment is the class I am taking with her.

http://209.85.165.104/search?q=cache...ient=firefox-a

anyone that feels like giving me a personal assistant can pm me.
Thanks in Advance

Expand|Select|Wrap|Line Numbers
  1. /*
  2.  * OrderPage.java
  3.  *
  4.  * Created on October 16, 2007, 12:31 PM
  5.  */
  6.  
  7. package ITIS5166;
  8.  
  9. import java.io.*;
  10. import javax.servlet.*;
  11. import javax.servlet.http.*;
  12. import java.util.*;
  13. import java.text.*;
  14.  
  15. /** Shows all items currently in ShoppingCart. Clients
  16.  *  have their own session that keeps track of which
  17.  *  ShoppingCart is theirs. If this is their first visit
  18.  *  to the order page, a new shopping cart is created.
  19.  *  Usually, people come to this page by way of a page
  20.  *  showing catalog entries, so this page adds an additional
  21.  *  item to the shopping cart. But users can also
  22.  *  bookmark this page, access it from their history list,
  23.  *  or be sent back to it by clicking on the "Update Order"
  24.  *  button after changing the number of items ordered.
  25.  *  <P>
  26.  *  Taken from Core Servlets and JavaServer Pages 2nd Edition
  27.  *  from Prentice Hall and Sun Microsystems Press,
  28.  *  http://www.coreservlets.com/.
  29.  *  &copy; 2003 Marty Hall; may be freely used or adapted.
  30.  */
  31.  
  32. public class ShopCartServletHW3 extends HttpServlet {
  33.   public void doGet(HttpServletRequest request,
  34.                     HttpServletResponse response)
  35.       throws ServletException, IOException {
  36.     HttpSession session = request.getSession();
  37.     ShopCartDataHW3 cart;
  38.     synchronized(session) {
  39.       cart = (ShopCartDataHW3)session.getAttribute("ShopCartDataHW3");
  40.       // New visitors get a fresh shopping cart.
  41.       // Previous visitors keep using their existing cart.
  42.       if (cart == null){
  43.           cart = new ShopCartDataHW3();
  44.           session.setAttribute("ShopCartDataHW3", cart);
  45.       }
  46.       String itemID = request.getParameter("itemID");
  47.       if (itemID != null) {
  48.           String numItemsString = request.getParameter("numItems");
  49.           if (numItemsString == null) {
  50.               cart.addItem(itemID);
  51.         } else {
  52.           // If request specified an ID and number, then
  53.           // customers came here via an "Update Order" button
  54.           // after changing the number of items in order.
  55.           // Note that specifying a number of 0 results
  56.           // in item being deleted from cart.
  57.           int numItems;
  58.           try {
  59.             numItems = Integer.parseInt(numItemsString);
  60.           } catch(NumberFormatException nfe) {
  61.             numItems = 1;
  62.           }
  63.           cart.setNumOrdered(itemID, numItems);
  64.         }
  65.       }
  66.     }
  67.     // Whether or not the customer changed the order, show
  68.     // order status.
  69.     String straddress=request.getParameter("address");
  70.     String strshippingtype=request.getParameter("shippingtype");
  71.     String strcardtype=request.getParameter("cardtype");
  72.     String strcardnum=request.getParameter("cardnum");
  73.     String strcardname=request.getParameter("personname");
  74.  
  75.     response.setContentType("text/html");
  76.     PrintWriter out = response.getWriter();
  77.     String title = "Status of Your Order";
  78.     String docType =
  79.       "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
  80.       "Transitional//EN\">\n";
  81.     out.println(docType +
  82.                 "<HTML>\n" +
  83.                 "<HEAD><TITLE>" + title + "</TITLE></HEAD>\n" +
  84.                 "<BODY BGCOLOR=\"#FDF5E6\">\n" +
  85.                 "<H1 ALIGN=\"CENTER\">" + title + "</H1>");
  86.     synchronized(session) {
  87.       List itemsOrdered = cart.getItemsOrdered();
  88.       if (itemsOrdered.size() == 0) {
  89.         out.println("<H2><I>No items in your cart...</I></H2>");
  90.         out.println
  91.           ("<TABLE BORDER=1 ALIGN=\"CENTER\">\n" +
  92.            "<TR BGCOLOR=\"#FFAD00\">\n" +
  93.            "  <TH>Item ID<TH>Description\n" +
  94.            "  <TH>Unit Cost<TH>Number<TH>Total Cost");
  95.  
  96.           out.println            
  97.             ("<TR>\n" +
  98.              "  <TR bgcolor=#339900>" +
  99.              "  <TD height=30> Shipping Information </TD></TR>" +
  100.              "  <TR><TD WIDTH=200>" +
  101.              "  <textarea name=address cols=50 rows=4></textarea></form></td>" +
  102.              "  <TD><P>"+"<LABEL>" +
  103.              "  <input type=radio name=shippingtype value=UPS Grounds>" +
  104.              "  UPS Ground </label><br>" +
  105.              "  <label>" +
  106.                   "<input type=radio name=shippingtype value=UPS 2nd Day Air>" +
  107.                   "UPS 2nd Day Air </label><br>" +
  108.                   "<label><input type=radio name=shipping values=FedEx Priority>" +
  109.                   "FedEx Priority </label><br>" +
  110.                   "<label><input type=radio name=shippingtype value=FedEx Ultra>" +
  111.                   "FedEx Ultra</label></p></form></td></tr>" +
  112.                   "<tr bgcolor=#339900>" +
  113.                   "<td height=30>Credit Card Information</td></tr>" +
  114.                   "<tr><td height=40>Card Type: </td>" +
  115.                   "<td colspan=4>" +
  116.                   "<select name=cardtype value=visa>" +
  117.                   "<option selected value=visa>Visa</option>" +
  118.                   "<option value=MasterCard>MasterCard</option>" +
  119.                   "<option value=America Express>America Express</option>" +
  120.                   "<select></form></td></tr>" +
  121.                   "<td height=30>Card Number:</td>" +
  122.                   "<td colspan=4><input type=text name=cardnum> </form></td></tr>" +
  123.                   "</form></td></tr>" +
  124.                   "<tr><t height=30>Cardholder Name:</td>" +
  125.                   "<td colspan=4><input type=text name=personname></form></td></tr><br>");
  126.       }
  127.  
  128.       else {
  129.           out.println ("<table border=1>" +
  130.                   "<tr bgcolor=#339900>" +
  131.                   "<th width=90>Item ID<th>Description" +
  132.                   "<th>Unit Cost <th>Number<th>Total Cost");
  133.           ItemOrder order;
  134.  
  135.           NumberFormat formatter =
  136.           NumberFormat.getCurrencyInstance();
  137.         // For each entry in shopping cart, make
  138.         // table row showing ID, description, per-item
  139.         // cost, number ordered, and total cost.
  140.         // Put number ordered in textfield that user
  141.         // can change, with "Update Order" button next
  142.         // to it, which resubmits to this same page
  143.         // but specifying a different number of items.
  144.         for(int i=0; i<itemsOrdered.size(); i++) {
  145.           order = (ItemOrder)itemsOrdered.get(i);
  146.           out.println
  147.             ("<TR>\n" +
  148.              "  <TD>" + order.getItemID() + "\n" +
  149.              "  <TD>" + order.getShortDescription() + "\n" +
  150.              "  <TD>" +
  151.              formatter.format(order.getUnitCost()) + "\n" +
  152.              "  <TD>" +
  153.              "<FORM>\n" +  // Submit to current URL
  154.              "<INPUT TYPE=\"HIDDEN\" NAME=\"itemID\"\n" +
  155.              "       VALUE=\"" + order.getItemID() + "\">\n" +
  156.              "<INPUT TYPE=\"TEXT\" NAME=\"numItems\"\n" +
  157.              "       SIZE=3 VALUE=\"" + 
  158.              order.getNumItems() + "\">\n" +
  159.              "<SMALL>\n" +
  160.              "<INPUT TYPE=\"SUBMIT\"\n "+
  161.              "       VALUE=\"Update Order\">\n" +
  162.              "</SMALL>\n" +
  163.              "</FORM>\n" +
  164.              "  <TD>" +
  165.              formatter.format(order.getTotalCost()));
  166.         }
  167.  
  168.    if ((straddress!=null)&&(strshippingtype!=null)
  169.        &&(strcardtype!=null)&&(strcardnum!=null)&&(strcardname!=null))
  170.    {
  171.               out.println("<form action=PurchConfServletHW3 methed=Get>");
  172.    }
  173.    else{
  174.               out.println("<form action=ShopCartServletHW3 method=Get>");
  175.               out.println("Please enter your Information");
  176.    }
  177.           out.println(
  178.  
  179.                   "</tr>"+
  180.                   "<tr bgcolor=#339900><td height=30 colspan=5>" +
  181.                   "Shipping Information</td></tr>" +
  182.                   "<tr><td with=200><textarea name=address cols=50 rows=4></textarea></td>" +
  183.                   "<td colspan=4><p>"+"<label><br />" +
  184.                   "<input type=radio name=shippingtype value=UPS Ground>" +
  185.                   "UPS Ground </label><br>" +
  186.                   "<label><input type=raio name=shippingtype value=UPS 2nd Day Air>" +
  187.                   "UPS 2nd Day Air </label>" +
  188.                   "<label><input type=radio name=shippingtype value=FedEx Priority>" +
  189.                   "FedEx Priority <label>" +
  190.                   "<label><input type=radio name=shippingtype value=FedEx Ultra>" +
  191.                   "FedEx Ultra </label></p></td></tr>" +
  192.                   "<tr bgcolor=#339900><td height=30 colspan=5>" +
  193.                   "Credit Card Information</td></tr>" +
  194.                   "<tr><td height=30>Card Type:</td>" +
  195.                   "<td colspan=4><select anme=cardtype value=visa>" +
  196.                   "option selected value=Visa>Visa</option>" +
  197.                   "option value=MasterCard>MasterCard</option>" +
  198.                   "option value=America Express>America Express</option></select></td></tr>" +
  199.                   "<tr><td height=30>Card Number:</td>" +
  200.                   "<td colspan=4><input type=text name=cardnum></td></tr></td></tr>" +
  201.                   "<tr><td height=30>Cardholder Name:</td>" +
  202.                   "<td colspan=4><input type=text name=personname></td></tr>" +
  203.                   "tr bgcolor=#339900><td height=30><input type=submit name=Sumbit4 value=Purchase></td></tr>" +
  204.                   "</form>");
  205.         String checkoutURL =
  206.           response.encodeURL("../Checkout.html");
  207.         // "Proceed to Checkout" button below table
  208.         out.println
  209.           ("</TABLE>\n" +
  210.            "<FORM ACTION=\"" + checkoutURL + "\">\n" +
  211.            "<BIG><CENTER>\n" +
  212.            "<INPUT TYPE=\"SUBMIT\"\n" +
  213.            "       VALUE=\"Proceed to Checkout\">\n" +
  214.            "</CENTER></BIG></FORM>");
  215.       }
  216.       out.println("</BODY></HTML>");
  217.     }
  218.   }
  219.  
  220.   }
  221.  
Oct 19 '07 #1
Share this Question
Share on Google+
3 Replies


ak1dnar
Expert 100+
P: 1,584
wrap your item description values with a anchor tag and pass the relevant URL parameters with the links to product_details page. all it depends on the way you have created your pages. for a example let's assume that these are your link to products on the description column on your shopping cart;

<a href="product_details.jsp?item_id=1001">Item 1001 description</a>
<a href="product_details.jsp?item_id=1002">Item 1002 description</a>

Then from the product_details.jsp get the "item_id" display the records for the item.
Oct 20 '07 #2

Expert 10K+
P: 11,448
I'll move your question over to the Java Forum section.

kind regards,

Jos
Oct 21 '07 #3

Expert 10K+
P: 11,448
Why don't you use a simple JSP page for the actual html output? If you try to
output everything from your single Servlet it becomes an untangible mess as
you can see already.

kind regards,

Jos
Oct 21 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.