This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes EJB and other Java EE Technologies and the fly likes Session Variable problem in EJB Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Session Variable problem in EJB" Watch "Session Variable problem in EJB" New topic
Author

Session Variable problem in EJB

Ramesh Kumar Swarnkar
Ranch Hand

Joined: Sep 15, 2003
Posts: 84
Hi Friends,
I am getting problem when I run my J2EE application on JBOss 3.0.7.
The scenario is :-
While a user(client from jsp) requests Servlet to view the products-page, a session for ClientFacade (EJB file) is created in that servlet, so that for the next request to ClientFacade, it just uses the session variable. Here session variable nevigates between servlet and jsp.This session variable retains the value only for two times from jsp to servlet, and gets null.

I create seesion variable for each visitor to store its value of shopping cart.But here also the same problem, this session created value does not exist in session variable for more that two hits to servlet.

I found myself stucked in this point.
I would appreciate my friends for this help me .
Thanks in advance !!
[ October 13, 2004: Message edited by: Ramesh Kumar Swarnkar ]
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8904

This session variable retains the value only for two times from jsp to servlet, and gets null.


Is the session getting timed out somewhere? :roll:


Groovy
somkiat puisungnoen
Ranch Hand

Joined: Jul 04, 2003
Posts: 1312
I create seesion variable for each visitor to store its value of shopping cart.But here also the same problem, this session created value does not exist in session variable for more that two hits to servlet.


What is your condition for create session variable ?


In each user have different session's attribute or not ?


SCJA,SCJP,SCWCD,SCBCD,SCEA I
Java Developer, Thailand
Ramesh Kumar Swarnkar
Ranch Hand

Joined: Sep 15, 2003
Posts: 84
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 ]
somkiat puisungnoen
Ranch Hand

Joined: Jul 04, 2003
Posts: 1312
Can you get null value from another session's attribute ??

IF you can get null, i think , session will expired.
Ramesh Kumar Swarnkar
Ranch Hand

Joined: Sep 15, 2003
Posts: 84
Thanks Somkiat Puisungnoen,
Well I have posted here my Code.
Where to look for session elongation ?
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8904

You can specify the timeout value in web.xml
http://e-docs.bea.com/wls/docs61/webapp/web_xml.html#1017356

Avoid using session.putValue and getValue as they are deprecated.
somkiat puisungnoen
Ranch Hand

Joined: Jul 04, 2003
Posts: 1312
Why you use session.setAtribute() and session.putValue() ?

My suggestion, please change session.putValue() to session.setAttribute().
Ramesh Kumar Swarnkar
Ranch Hand

Joined: Sep 15, 2003
Posts: 84
Hi Somkiat puisungnoen & Pradeep Bhat

....After changing the putValue and getValue for Session into setAttribute and getAttribute, I am able to add three items to shoppong cart.If I continue in the same browser from beggining, then I can add four itmes to cart......!!
I dont why it is happenig..!!
Puuzled.
somkiat puisungnoen
Ranch Hand

Joined: Jul 04, 2003
Posts: 1312
putValue
public void putValue(java.lang.String name,
java.lang.Object value)Deprecated. As of Version 2.2, this method is replaced by setAttribute(java.lang.String, java.lang.Object)
Parameters:
name - the name to which the object is bound; cannot be null
value - the object to be bound; cannot be null
Throws:
IllegalStateException - if this method is called on an invalidated session
somkiat puisungnoen
Ranch Hand

Joined: Jul 04, 2003
Posts: 1312
getValue
public java.lang.Object getValue(java.lang.String name)Deprecated. As of Version 2.2, this method is replaced by getAttribute(java.lang.String).
Parameters:
name - a string specifying the name of the object
Returns:
the object with the specified name
Throws:
IllegalStateException - if this method is called on an invalidated session
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8904

Originally posted by Ramesh Kumar Swarnkar:
Hi Somkiat puisungnoen & Pradeep Bhat

....After changing the putValue and getValue for Session into setAttribute and getAttribute, I am able to add three items to shoppong cart.If I continue in the same browser from beggining, then I can add four itmes to cart......!!
I dont why it is happenig..!!
Puuzled.


From the begining? Do you mean you are using the Back button and going to the first page ?
somkiat puisungnoen
Ranch Hand

Joined: Jul 04, 2003
Posts: 1312
Originally posted by Ramesh Kumar Swarnkar:
Hi Somkiat puisungnoen & Pradeep Bhat

....After changing the putValue and getValue for Session into setAttribute and getAttribute, I am able to add three items to shoppong cart.If I continue in the same browser from beggining, then I can add four itmes to cart......!!
I dont why it is happenig..!!
Puuzled.


In your system should be created signon process for each user.

I think, your app use same session's attribute to keep shopping cart.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Session Variable problem in EJB
 
Similar Threads
How to access EJB from Different Machine
Session expire problem with HttpSessionListener
critical ejb design problem
How to access EJB from Different Machine
Control scheme for the client GUI