Hi Friends,
Thanks a lot for so quick response.
I am writting the servlet which is requested by jsp to create session.
-----------------------------------------------------------
public class ControllerServlet extends HttpServlet
{
private StudentFacadeHome studentFacadeHome;
public void init(ServletConfig config) throws ServletException
{
super.init(config);
try {
Context ctx = new InitialContext();
studentFacadeHome = (StudentFacadeHome) ctx.lookup("day11/StudentFacade");
} catch (Exception e) { throw new ServletException(e.toString()); }
}
public void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
HttpSession session = request.getSession(true);
String pageName = request.getParameter("pageName");
if ( pageName != null )
if ( pageName.equals("displayProducts" ))
{processDisplayProducts(request, response, session); }
else if ( pageName.equals("catalogPage" ))
{addToCart(request, response, session); }
else if ( pageName.equals("viewCart" ))
{retrieveCartItems(request, response, session); }
//$$$$$$$$$$$$$$$$$$$$$ DISPLAY PRODUCTS $$$$$$$$$$$$$$$$$$
void processDisplayProducts (HttpServletRequest request,
HttpServletResponse response,
HttpSession session ) throws ServletException, IOException
{
StudentFacade studentFacade = null;
try {
studentFacade = (StudentFacade) studentFacadeHome.create();
session.putValue("StudentFacade", studentFacade);
session.setAttribute("courseItemList", studentFacade.getCourseItemList());
forwardToPage(request, response, "/products.jsp");
} catch ( Exception e)
{ System.err.println(throw new ServletException(e.toString());}
}
//$$$$$$$$$$$$$$$$$$$$$ ADD TO SHOPPING CART $$$$$$$$$$$$$$$$$$
void addToCart(HttpServletRequest request,
HttpServletResponse response,
HttpSession session ) throws ServletException,Exception
{
try {
StudentFacade studentFacade = (StudentFacade)session.getValue("StudentFacade");
String itemId = request.getParameter("buy");
if ( itemId != null )
{
String visiId = (String)session.getValue("VISITOR");
int qnty = 1;
if(visiId==null)
{
visiId = generateVisiID();
session.putValue("VISITOR",visiId);
}
studentFacade.addOrder(visiId,itemId,qnty);
}
forwardToPage(request, response, "/products.jsp");
} catch ( Exception e) {throw new ServletException(e.toString());}
}
//$$$$$$$$$$$$$$$$$$$$$ RECEIVE THE SHOPPING CART ITEMS $$$$$$$$$$$$$$$
void retrieveCartItems (HttpServletRequest request,
HttpServletResponse response,
HttpSession session ) throws ServletException, IOException
{
---------------------
--------------------
}
private String generateVisiID()
{ return new Long(System.currentTimeMillis()).toString(); }
void forwardToPage(HttpServletRequest request,
HttpServletResponse response, String page)
throws ServletException, IOException
{
RequestDispatcher disp = this.getServletContext().getRequestDispatcher(page);
disp.forward(request, response);
}
}
-----------------------------------------------------------
Here when user selects the product(in jsp page) and requests the servlet file to add it to cart, it works till user purchases two items from jsp product-dispaly page.Third time when user clicks add to cart in jsp, it gets new session id for same user.
Same thing for session.getValue("StudentFacade") also. IT gets null after purchasing two items.
Hope it may help you to analyse the scenario well.......
[ October 13, 2004: Message edited by: Ramesh Kumar Swarnkar ]