not able to get the correct value from the seesion


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: not able to get the correct value from the seesion

  1. #1
    satish Guest

    not able to get the correct value from the seesion



    pls check my code...

    i am getting error with variables curprice and also with total...


    i want to put the costs of all the products in the session objects and finally
    calculate the totalcost and dispaly it..
    pls help me

    satish



    import java.io.*;
    import java.sql.*;
    import javax.servlet.*;
    import javax.servlet.http.*;




    public class cart extends HttpServlet
    {



    Connection con = null;
    PreparedStatement pst = null;
    ResultSet rs =
    null;





    public void init(ServletConfig sc) throws ServletException

    {

    try
    {

    Class.forName("COM.ibm.db2.jdbc.app.DB2Driver");

    con = DriverManager.getConnection("jdbc:db2:sample",
    "satish", "london");

    super.init(sc);

    }

    catch(Exception e) {e.getMessage() ; }

    }







    public void doPost(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException
    {




    res.setContentType("text/html");
    PrintWriter out = res.getWriter();

    HttpSession session = req.getSession(true);

    Object[] items = (Object []) session.getValue("cart.items");


    out.println("<html> ");
    out.println("<body bgcolor=899699>");
    out.println("<H3>you currently have the following
    items in your cart:</H3><br>");
    out.println("<form> ");

    out.println("<table>");

    if(items!=null)
    {

    for (int i = 0 ; i < items.length
    ; i++ )
    {

    try
    {

    String str1 =
    (String) items[ i ] ;

    pst = con.prepareStatement("select
    * from ITEMTAB where itmname = ? " );


    pst.setString(1,str1)
    ;
    rs = pst.executeQuery(
    );




    while(rs.next())
    {
    out.println("<tr>
    ");

    out.println("<td> ");



    out.println( "The Price of " + " " + str1 + " is "
    + " " + rs.getString("price"));



    out.println("</td> ");
    out.println("</tr>
    ");


    out.println("</table>
    ");

    out.println("<br>");


    String
    curprice = rs.getString("price") ;

    session.putValue("total.cost",
    curprice+curprice );

    }


    String totprice = (Object)
    session.getValue("total.cost");

    Object total = (Object) curprice
    + totprice ;



    session.putValue("total.cost",
    total );






    }

    catch(Exception e) { e.getMessage()
    ; }

    }



    out.println("<table border
    = 1 cellpadding = 2 cellspacing = 3>");
    out.println("<tr>
    ");
    out.println("<td>
    ");




    out.println("The Total Cost of all the Products in the Cart is : " +
    total );

    out.println("</td>
    ");
    out.println("</tr>
    ");
    out.println("</table>
    ");






    out.println("</form>
    ");



    }



    else
    {
    out.println("There are no items in the session
    value for cart.items" );

    }


    out.println("</body>");

    out.println("</html> ");

    }


    }





  2. #2
    Nikhil Guest

    Re: not able to get the correct value from the seesion


    Can you try this....

    import java.io.*;
    import java.sql.*;
    import javax.servlet.*;
    import javax.servlet.http.*;


    public class cart extends HttpServlet
    {



    Connection con = null;
    PreparedStatement pst = null;
    ResultSet rs = null;


    public void init(ServletConfig sc) throws ServletException

    {

    try
    {

    Class.forName("COM.ibm.db2.jdbc.app.DB2Driver");

    con = DriverManager.getConnection("jdbc:db2:sample","satish",
    "london");

    super.init(sc);

    }

    catch(Exception e) {e.getMessage() ; }

    }



    public void doPost(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException
    {
    String strtotalprice = new String();
    res.setContentType("text/html");
    PrintWriter out = res.getWriter();

    HttpSession session = req.getSession(true);

    Object[] items = (Object []) session.getValue("cart.items");


    out.println("<html> ");
    out.println("<body bgcolor=899699>");
    out.println("<H3>you currently have the following items in your cart:</H3><br>");
    out.println("<form> ");

    out.println("<table>");

    if(items!=null)
    {
    String strcurprice = new String();
    for (int i = 0 ; i < items.length; i++ )
    {

    try
    {

    String str1 = (String) items[ i ] ;

    pst = con.prepareStatement("select price from ITEMTAB where
    itmname = ? " );


    pst.setString(1,str1);
    rs = pst.executeQuery();

    while(rs.next())
    {
    strcurprice = rs.getString("price");
    out.println("<tr>");

    out.println("<td> ");

    out.println( "The Price of " + " " + str1 + " is
    " + " " + strcurprice );

    out.println("</td> ");
    out.println("</tr>");


    out.println("</table>");

    out.println("<br>");


    String strsessiontotalval = (String)session.getValue("total.cost");
    if (strsessiontotalval == null)
    strsessiontotalval = "0.0";

    session.putValue("total.cost", (new Float(Float.parseFloat(strsessiontotalval)
    + Float.parseFloat(strcurprice)).toString()) );

    } // end while



    } // end try block

    catch(Exception e) { e.getMessage(); }

    } // end for loop



    out.println("<table border = 1 cellpadding = 2 cellspacing = 3>");
    out.println("<tr>");
    out.println("<td>");



    String totval = (String)session.getValue("total.cost");

    out.println("The Total Cost of all the Products in the Cart is : " +
    totval );

    out.println("</td>");
    out.println("</tr>");
    out.println("</table>");






    out.println("</form>");



    }
    else
    {
    out.println("There are no items in the session value for cart.items"
    );

    }


    out.println("</body>");

    out.println("</html> ");

    }


    }


  3. #3
    SATISH Guest

    Re: not able to get the correct value from the seesion


    Dear Nikhil,

    thanks for ur response.

    i treid with ur code the pgm got compiled but then when i tried run that
    it gave the following error..pls check it.

    satish

    -----------------------------------------------------------------

    500 Internal Server Error
    The servlet named invoker at the requested URL

    http://localhost:8080/servlet/cart
    reported this exception: java.lang.Float: method parseFloat(Ljava/lang/StringF
    not found. Please report this to the administrator of the web server.

    java.lang.NoSuchMethodError: java.lang.Float: method parseFloat(Ljava/lang/StringF
    not found at cart.doPost(Compiled Code) at javax.servlet.http.HttpServlet.service(HttpServlet.java:747)
    at javax.servlet.http.HttpServlet.service(Compiled Code) at com.sun.server.ServletState.callService(ServletState.java:226)
    at com.sun.server.ServletManager.callServletService(Compiled Code) at com.sun.server.http.servlet.InvokerServlet.service(InvokerServlet.java:137)
    at javax.servlet.http.HttpServlet.service(Compiled Code) at com.sun.server.ServletState.callService(ServletState.java:226)
    at com.sun.server.ServletManager.callServletService(Compiled Code) at com.sun.server.ProcessingState.invokeTargetServlet(ProcessingState.java:423)
    at com.sun.server.http.HttpProcessingState.execute(HttpProcessingState.java:79)
    at com.sun.server.http.stages.Runner.process(Runner.java:79) at com.sun.server.ProcessingSupport.process(Compiled
    Code) at com.sun.server.Service.process(Service.java:204) at com.sun.server.http.HttpServiceHandler.handleRequest(HttpServiceHandler.java:374)
    at com.sun.server.http.HttpServiceHandler.handleRequest(Compiled Code) at
    com.sun.server.HandlerThread.run(Compiled Code)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center