Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

session - approach ?

 
Karan Sharma
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I have one question regarding the implementation of session in the jsp's. For using the objects in the session, there are two approaches that can be followed. First approach is that in jsp the code looks like this :
<jsp:usebean id="Constants.FINAL_VARIABLE" scope="session" type="ObjectClass">
Here every jsp has to know about the object to be retrieved from the session. Second approach is where there is a static class, which is called by the jsp, and the static class returns the object from the session based on the parameter passed. Like in the jsp the code will look like this:

SessionHandler.getObjectFromSession("Constants.FINAL_VARIABLE",session);

The point here is that from the jsp a session object is passed a parameter to the method of the static class, which brings the most important thing WILL THIS IMPACT PERFORMANCE ??
To explain this thing better, lets say there is an object1 which is stored in session, the object 1 has a property which is object2, and this object 2 contains a string property. In the first approach to get the property of object2, the code has to be written for retrieving object1 and then object 2 and then the string property, which is fine, in the second implementation only a key is passed to the static class, which does the retrieving of the object1 from the session and then retrieving of object2 from object 1, which is again fine. Now in case the implementation changes in a way that object2 is not going to be a property of object1, instead its going to be a seperate object in the session, then in that case, there will not be any changes to be made in the jsp in the second approach, since the actual implementation is with the static class which will be changed, but in the first approach the jsp needs to be changed.
I am confused as to what approach to follow, basically the only thing that I am doubtful abt is the passing of the session object from the jsp in the second approach. 'PERFORMANCE' is the key word here.
I will really appreciate if the J2EE gurus help me out over here.
Thanks
 
Rufus BugleWeed
Ranch Hand
Posts: 1551
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here every jsp has to know about the object to be retrieved from the session.

From this I wonder are we talking about every JSP a given client uses or if we are talking about an object every client uses. From this I wonder if you want scope of session or scope of application.
Even so I'm still confused.
Sort of looks like you have a dynamic property like the conversion factor from rupee to dollar.
You want to know wether to fetch it from the session and pass it as a parameter versus pass the session and let the client fetch the parameter?
Or you want to know if it's faster to store the propery as a static member in a class versus storing the poperty in the session object?
 
Karan Sharma
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply.
To clear out the scenario, I am talking about every jsp, and the object is in session scope not application scope. For eg, lets say I have to display the userinformation for the logged in user in the jsp, I will populate the UserValueObject in the DAO and then set the UserValueObject in the session for eg session.setAttribute(Constants.USERVALUEOBJECT, userValueObject), and in the jsp for displaying the userinformation, I can follow two approaches -
1)Directly use the jsp:usebean tag and get the properties of the UserValueObject
OR
2) I have one static class, lets say SessionHandler, which the jsp will call by passing in the string and the sessionobject for eg in jsp the code will be like this <%
UserValueObject userValueObject = SessionHandler.getObject(Constants.USERVALUEOBJECT, session). The implementation of getObject of SessionHandler class will be like this return (UserValueObect)session.getAtrribute(Constants.USERVALUEOBJECT);
I hope I clear the two approaches, basically the second approach just hides the jsp from getting the object from the session, but with a big drawback of passing in the session object to the SessionHandler class, what I am confused abt here is that will it be a good idea if I pass session object from a jsp to a SessionHandler class just to get the object from the session, which otherwise I can directly get by using jsp:usebean. Will the second approach hamper PERFORMANCE.
The only advantage in following the second approach is while I am using nested objects, as I explained the firt post, in that case I dont have to change the jsp's, only the implementation in the SessionHandler class will change. To explain it further, lets say the UserValueObject is changed to be a property of GroupValueObject which is added to session, now here, I will have to change the jsp if I follow the first approach, but if I fpllow the second approach i.e.using the SessionHandler class, I dont have to change the jsp, I will change the implementation in the SessionHandler's getObject method, to retrieve the GroupValueObject from session and then retrieve the UserValueObject from GroupValueObject and return it back to the user.
To put it short is it a good idea to pass session object from the jsp to a class, especially in regards to performance, and what do u think is the better approach - the first one or the second one.
I hope I clear out things now.
Regards
 
Rufus BugleWeed
Ranch Hand
Posts: 1551
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While you were working they changed the names. You are speaking of a TransferObject (TO) and a CompositeTransferObject(CTO). If I understand correctly you would have to extract each TO from the CTO and insert in the session as individual attributes to employ the usebean tag.
One more question - there is no stateful session bean involved, true?
 
Karan Sharma
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, lets call it as TO and CTO

Well as u talked abt implementation of jsp:usebean, that brings to another point, does use:bean provide access of nested objects, in that case I can simply give in the property TO.property, like in struts (which uses beanutils) you can use bean:write name="CTO" property="TO.value" which translates to CTO.getTO.getValue. I am not sure jsp:usebean provides this sort of implementation.

Another thing its not necessary that I have to put each TO in session, i can simply insert CTO in the sessin and then retrieve the CTO from the session in the jsp (i guess withouth using jsp:usebean tag) but using session.getAttribute [if the usebean doesnt support nested object access].

Actually the discussion over here is that if I follow the second approach in having a static class which will do the implementation for retrieving the object from the session and returning it back, in that case will the performance be slow, since I will be passing the session object (implicit object available in jsp) as a parameter to the StaticClass method, and as per my understanding parameters are always passed by value in Java, which means it will impact performance. So the point here is it worth to follow the second approach, especially in regards to compromising on performance.
There is no stateful session bean involved.
Thanks once again.
 
Rufus BugleWeed
Ranch Hand
Posts: 1551
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Static Object is going to have more thread collisions too.
 
Karan Sharma
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply. I am missing something here, can you please elaborate on that .
 
Rufus BugleWeed
Ranch Hand
Posts: 1551
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
AFAIK neither a session attribute or a static class is thread safe. This means that you have to worry about concurrent access. Read the synchronized keyword here. If two instances of a JSP ( a servlet of course ) are running at the same time then one has to be careful about the two programs getting their data mixed up. Now the servlets have to queue up to get the lock on the monitor. This could be many servlets queued up waiting for the static method.
I would have to research the threading issues to be certain of this issue.
When the data is in the session, you still have to worry about concurrent access. But far fewer objects will be trying to get into your session.
I don't think either way is going to be that much different in execution time. The session method seems the natural or least complicated way to do it. Since one spends more time and money maintaining an application, doing it the least complicated way, is what the gurus preach.
They say write it the easiest or least complicated way. Put off doing any performance enhancements until very late in the development process. Do not do any unless you need to. Then with the aid of a profiler do performance improvements where you have bottlenecks.
This issue might be better discussed in the servlets forum.
 
Karan Sharma
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for ur reply.
I am bit confused here. Servlets are intrinsically multithreaded. This means a single instance can be accessed by more than one thread. So does this mean that if I am calling a Static Class from the jsp (which will actually be a servlet), then I will have to look into the concurrency issues. Does that mean that if i use
<<StaticClass>>
SessionHandler.getObject(Constants.USERVALUEOBJECT, session)
then I will have to take care of the concurrency issues. What I dont understand here is that the "session" parameter passed to the Staticclass method is particular to one thread so the StaticClass is always going to operate on that, for eg in SessionHandler's getObject implementation of getObject { session.getAttribute(Constants.USERVALUEOBJECT);} then why should there be any thread issues.

Another thing the approach that you recommended is using the jsp:useBean/session.getAttribute OR using a StaticClass for session handling ?
Regards
 
Rufus BugleWeed
Ranch Hand
Posts: 1551
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I like the use bean method. IMO, it stays closest to the servlet way of life.
If you only use local variable in the static method, then that should be thread safe. Static class variables would have to be protected.
 
Bill Wang
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well, I think SessionHanlder will be a better choice in your case, Karan.
Since you pass session object into the static method only to retrieve data, there should not be any synchronization problem. And it much easier to handle changes in the future
Bill
 
Karan Sharma
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah....so it boils down to the same thing, different people having a different opinion, i still wonder whats the best way to figure out which one is more suitable and logical. If I use jsp:usebean then I am sticking to the servlet way of life, also its more acceptable and presentable, but if I go for SessionHanlder, it hides the object composition, which may change in future. ooffff ...
Thanks once again for helping me out !!!
Regards
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic