|
 |
pro_jsp thread: Hastable?
Message #1 by "Low Kok Leong" <lousai@t...> on Fri, 17 Nov 2000 08:07:13 -0000
|
|
Hi,
as hareesh has noticed, you are missing a "simple" thing like:
out.println("<HTML>");
out.println("<HEAD><TITLE>You Title</TITLE></HEAD>");
out.println("<BODY >");
printCatalog(out, requestURI);
out.println("</BODY>");
out.println("</HTML>");
good luck!
----- Original Message -----
From: hareesh babu <venkata_hareesh@y...>
To: Pro_JavaServer_Pages <pro_jsp@p...>
Sent: Friday, November 17, 2000 3:29 AM
Subject: [pro_jsp] Re: Hastable?
> hai
> where are the begin and ending
> html & body tags in your generating html bodY
>
> --- Low Kok Leong <lousai@t...> wrote: > Hi can
> anyone try to figure out the mistake/error in
> > this code below:
> >
> ---------------------------------code---------------------------
> > import javax.servlet.*;
> > import javax.servlet.http.*;
> >
> > import java.io.*;
> > import java.sql.*;
> > import java.util.Properties;
> > import java.util.Enumeration;
> > import java.util.Hashtable;
> > import java.text.NumberFormat;
> >
> >
> >
> > public class ShoppingCart extends HttpServlet {
> >
> > private Connection dbc;
> >
> > /**
> > * The init method gets called when the servlet is
> > instantiated; ie only
> > once
> > * in its lifetime. Here is where you would
> > initialize resources that
> > should
> > * be available for the lifetime of the servlet.
> > */
> > public void init(ServletConfig config) throws
> > ServletException {
> > super.init(config);
> >
> > dbc = null;
> >
> > /*
> > * The database connection is instantiated here
> > and will be available
> > * for the lifetime of the servlet.
> > */
> > try {
> >
> >
> Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
> > dbc
> > DriverManager.getConnection("jdbc:odbc:TestCart");
> > } catch (Exception e) {
> > e.printStackTrace();
> > }
> > }
> >
> > /**
> > * The destory() method is called before the
> > servlet engine decides to
> > unload
> > * the servlet. This is where you would free
> > resources that aren't
> > needed
> > * anymore. In this case, the database connection
> > is closed.
> > */
> > public void destroy() {
> > try {
> > dbc.close();
> > } catch (SQLException e) {
> > e.printStackTrace();
> > }
> > }
> >
> > /**
> > * This is the brain of the servlet, where the
> > logic resides in deciding
> > * whether or not to display the product list, add
> > an item to the
> > * customer's shopping cart, or to display the cart
> > contents.
> > */
> > public void doGet (HttpServletRequest request,
> > HttpServletResponse
> > response)
> > throws ServletException, IOException {
> > Hashtable cart;
> > PrintWriter out;
> > HttpSession session;
> >
> > // get an output stream handle, a PrintWriter
> > out = response.getWriter();
> > // Set the content type in the response headers
> > response.setContentType("text/html");
> > // get a reference to the session object
> > session = request.getSession(true);
> >
> > // Get the "id" and "command" form paramters;
> > // if they're not available, the getParameter()
> > method
> > // will return null.
> > String id = request.getParameter("id");
> > String command = request.getParameter("command");
> >
> > // getRequestURI() is only one of many methods
> > available in the
> > // HttpRequest object to inspect request
> > properties.
> > String requestURI = request.getRequestURI();
> >
> > // get the customer's cart; if it the cart can't
> > be retrieved
> > // from the HttpSession object, create a new cart.
> > cart = (Hashtable) session.getValue("cart");
> > if (cart == null) {
> > cart = new Hashtable();
> > }
> >
> > out.println("<h1>Shopping Cart</h1>");
> >
> > try {
> > out.println("Just Kidding");
> > if (command.equals("add")) {
> > // add an item to the cart and then
> > // display the product list
> >
> > // get how many items of productID = id
> > // from the Hashtable
> > Integer num = (Integer) cart.get(id);
> > if (num == null) { num = new Integer(0); }
> >
> > // increment the number if items of the
> > // specified productID by one, and put
> > // it back into the cart Hashtable
> > cart.put(id, new Integer(num.intValue() + 1) );
> >
> >
> > printCatalog(out, requestURI);
> > } else if (command.equals("viewcart")) {
> > // view the contents of the cart
> > printCart(cart, out, requestURI);
> > } else {
> > // by default, print the product list
> > printCatalog(out, requestURI);
> > }
> > } catch (NullPointerException e) {
> > // catch this type of exception should the
> > command parameter
> > // not be available.
> > e.printStackTrace();
> > }
> > // put the newly modified cart back into storage
> > into the
> > // session object, so that the changes that
> > happened in the
> > // current request can be available to future
> > requests
> > session.putValue("cart", cart);
> > }
> >
> >
> > /**
> > * Database access using JDBC is a straightforward
> > operation; create a
> > * statement, execute it, retrieve the result set,
> > and then iterate
> > through
> > * it. One issue to watch out for with servlets is
> > to free up the
> > * resources once you're done with them.
> > */
> > private void printCatalog(PrintWriter out, String
> > requestURI) {
> >
> > // Product list header
> > out.println("<h3>Product Catalog</h3>");
> > out.println("<form action=\"" + requestURI + "\"
> > method=\"post\">");
> > out.println("<table border=1>");
> >
> >
> out.println("<tr><th>Item</th><th>Price</th><th></th></tr>");
> > try {
> >
> > Statement stmt = dbc.createStatement();
> > stmt.execute("select * from Products");
> > ResultSet rs = stmt.getResultSet();
> >
> > // this is used format the money values
> > NumberFormat nf
> > NumberFormat.getCurrencyInstance();
> >
> > // iterate through the result set; outputing the
> > product
> > // list in a html table
> > while (rs.next()) {
> > out.println("<tr>");
> > out.println("<td><b>" + rs.getString("name") +
> > "</b><br>" +
> > rs.getString("description") + "</td>");
> > out.println("<td align=\"right\">" +
> > nf.format(rs.getDouble("price")) + "</td>");
> > out.println("<td><a href=\"" + requestURI +
> > "?command=add&id=" +
> > String.valueOf(rs.getInt("productID")) +
> > "\">Add to Cart</a></td>");
> > out.println("</tr>");
> > }
> >
> >
> === message truncated ===
>
>
> __________________________________________________
> Do You Yahoo!?
> Yahoo! Calendar - Get organized for the holidays!
> http://calendar.yahoo.com/
>
> ---
> NEED TECHNICAL TIPS, TOOLS, AND INSIGHTS? Is FREE okay?
> Visit EarthWeb for the latest in IT Management, Software Development,
> Web Development, Networking & Communications, and Hardware & Systems.
> Click on http://www.earthweb.com for FREE articles, tutorials,
> and discussions from the experts.
>
|
|
 |