Forums Register Login

session tracking

+Pie Number of slices to send: Send
hi pls go through foll code and help me out in session tracking..
This is a ShoppingCart.java file
package test;
import java.util.*;
public class ShoppingCart
{
Hashtable ht;
public ShoppingCart()
{
ht=new Hashtable();
}
public void add(String item)
{
ht.put("item",item);
System.out.println("from shoppincart.java file");
System.out.println(ht.get("item"));

}
public void remove(String item)
{
ht.remove(item);
}
public void getItem()
{
System.out.println(ht.get("item"));
Enumeration e=ht.keys();
System.out.println(e.hasMoreElements());
while(e.hasMoreElements())
{
String s=(String)e.nextElement();
System.out.println(ht.get(s)+"getitems value" );
}
}
}
Here in this below JSP file iam adding a string ravi
<jsp:useBean id="shoppingcart" scope="session" class="test.ShoppingCart" />
<%
HttpSession cartsession=request.getSession(true);
ShoppingCart cart=new ShoppingCart();
cartsession.putValue(cartsession.getId(),cart);
shoppingcart.add("ravi");
System.out.println("after calling add method");
%>
Here in another JSP iam coding to get the value from session.
try
{
HttpSession cartsession = request.getSession(true);
out.println("before entering");
ShoppingCart cart=(ShoppingCart)(cartsession.getValue(cartsession.getId()));
out.println("after entering");
cart.getItem();
out.println("from viewcart.jsp");
}
catch(Exception e){out.println(e.getMessage()+"\n"+"error");}
Iam getting a null value from this.
Can u pls help me in this..
Rgds
RaviVunnam.
+Pie Number of slices to send: Send
Check for null in the add() method before inserting in to the hash table
Proudly marching to the beat of a different kettle of fish... while reading this tiny ad
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 656 times.
Similar Threads
session prob??
How to store multiple items into a shopping cart?
How To Convert a Servlet File to a JSP File
session tracking
Storing of Data
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 18, 2024 22:50:28.