sessionTracking


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: sessionTracking

  1. #1
    Join Date
    Oct 2005
    Posts
    1

    sessionTracking

    I 've written a simple shopping cart which is working fine but what i would like, is to make that program stop selecting the already selected item from the cart. eg if the item 'j2eeBible' is already selected it should not allow the user to once again select 'j2eeBible'
    That is i 've one servlet program for selecting the item using SELECT tag and the other servlet program is to display those item which is selected in the previous servlet page.someone please provide me the code which i am looking for.
    I 've pasted my 2 servlets below:
    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;

    public class ShoppingCart extends HttpServlet {

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

    // Get the current session object, create one if necessary.

    HttpSession session = req.getSession();

    // Cart items are maintained in the session object.
    String[] items = (String[])session.getAttribute("cart.items");

    out.println("<HTML><HEAD><TITLE>SessionTracker</TITLE></HEAD>");
    out.println("<BODY><H1>Session Tracking Demo</H1>");



    out.println("<p>Before you click on the Select,try to select more than one option-by holding down the shift or Ctrl key.Click on the Select button and try again.</p>");
    out.println("<form action=http://localhost:8080/MyFirstProject/servlet/Session method=get>");
    out.println("<select name=mySelect multiple=true>");
    out.println("<option value=J2EE>J2EE</option>");
    out.println("<option value=VB.NET>.net</option>");
    out.println("<option value=Pearl>pearl</option>");
    out.println("</select>");
    out.println("<input type=submit value=Select>");
    out.println("</form>");
    out.println("</BODY></HTML>");
    }
    }

    The following servlet displays the selected items from the previous servlet

    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.util.*;
    public class Session extends HttpServlet
    {
    Vector v=null;
    int vectorCount;
    public void init()
    {
    vectorCount=0;
    }
    public void doPost(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException
    {
    doGet(req, res);
    }
    public void doGet(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException
    {
    res.setContentType("text/html");
    PrintWriter out = res.getWriter();
    HttpSession session = req.getSession();
    String str=req.getParameter("mySelect");
    // Get the current session object, create one if necessary.
    // Cart items are maintained in the session object.
    // and the following line will return null if no objects are stored in it.
    // i.e., the following line is equivalent to v=null, where 'v' is Vector.
    v = (Vector)session.getAttribute("ITEMS");

    out.println("<HTML><HEAD><TITLE>SessionTracker</TITLE></HEAD>");
    out.println("<BODY><H1>Session Tracking Demo</H1>");
    // Print the current cart items.
    out.println("You currently have the following items in your cart:<BR>");
    if (v == null)
    {
    out.println("First Time");
    v=new Vector();
    v.addElement(str);
    //persisting the value between the requests or between the pages.
    session.setAttribute("ITEMS", v);
    }
    else
    {
    out.println("Not First Time");
    out.println();
    out.println();
    //In the following line vector size would become previousEle+currentEle. I.e., becaz of adv of sess persistnce
    //the shifting b/w pages wouldn't be considered or they would considered as the action that take place within a
    //page

    out.println(vectorCount);
    whileL: while(v.size()<vectorCount)
    {
    if (str.equals(v.elementAt(vectorCount)))
    {
    break whileL;
    }
    else
    {

    v.addElement(str);
    session.setAttribute("ITEMS",v);
    //break whileL;
    }
    --vectorCount;
    }
    //session.setAttribute("ITEMS", v);

    }
    out.println("vector size is "+v.size());
    for (int i=0;i<v.size();i++)
    {
    out.println("<h1>"+v.elementAt(i)+"<br>");
    }
    // Ask if they want to add more items or check out.
    out.println("<FORM ACTION=\"" +
    res.encodeURL("/MyFirstProject/servlet/ShoppingCart") + "\" METHOD=GET>");
    out.println("Would you like to<BR>");
    out.println("<INPUT TYPE=SUBMIT VALUE=\" Add More Items \">");
    out.println("<INPUT TYPE=SUBMIT VALUE=\" Check Out \">");
    out.println("</FORM>");
    // Offer a help page. Encode it as necessary.
    out.println("For help, click <A HREF=\"" +
    res.encodeURL("/servlet/Help?topic=ShoppingCartViewer") +
    "\">here</A>");
    out.println("</BODY></HTML>");
    }
    }

  2. #2
    Join Date
    Jul 2005
    Location
    SW MO, USA
    Posts
    299
    >please provide me the code which i am looking for.

    Is this a job offer? What are you willing to pay?

  3. #3
    Join Date
    Oct 2005
    Posts
    4
    Hi,

    You could have used Hashtable instead of a vector to store the items that the user selects for shopping. In that case, the contains method of hashtable returns true if the item is found. Appropriate action can then be carried out.
    This shud solve your problem.

    Sandhya

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