• Post Reply Bookmark Topic Watch Topic
  • New Topic

session tracking  RSS feed

 
ram shyam ravi
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Napa Sreedhar
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check for null in the add() method before inserting in to the hash table
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!