aspose file tools
The moose likes JSP and the fly likes problem while keeping scope as session Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » JSP
Reply Bookmark "problem while keeping scope as session" Watch "problem while keeping scope as session" New topic
Author

problem while keeping scope as session

vikasids sharma
Ranch Hand

Joined: Aug 01, 2003
Posts: 157
hi all
I m using MVC architecture where worker servlets corresponding to each jsp dispatches the request to respective jsp pages.
i m including a common jsp in all my jsp pages using <include age..../>.This page uses a CommonView bean by <useBean >tag.
This page has scope as "session" (I have to keep itz scope as session only bcoz its being used by few framed pages).Now all other jsp pages has scope as "request" except few which are framed pages which uses session as scope. The application is working fine as per logic is concerned.
let me know what can be adverse effects on performance using above style of coding and how can i reduce/remove such effects.
thanks
Vikas


Thanks
Vikas Sharma
SCJP(1.4)
Steve Leach
Ranch Hand

Joined: Sep 24, 2003
Posts: 46
I'm not sure what you mean by pages having session or request scope.
If you mean that some pages have "jsp:useBean scope=session" and some have "jsp:useBean scope=request" then you need to be aware that you are using different instances of the bean class. All of the session scope bean accesses refer to the same object. Each request scope access creates a new instance.
If the CommonView bean is thread-safe then you might as well make all accesses session scope. This will save the server from having to create lots of new instances.
If the bean is not thread safe then you need to make all accesses request or page scope. If you don't then you will get problems if a user requests a page twice at the same time; for example refreshing multiple frames, or hitting refresh twice quickly.
Either way, it should be all session scope or all request (or page) scope, not a mixture.
Finally, if the CommonView bean has no session related fields at all then you could consider making it application scope. This will mean there will only be a single instance for all users, but you had better be sure it is fully thread safe.
vikasids sharma
Ranch Hand

Joined: Aug 01, 2003
Posts: 157
1.See i m using commonView bean in common.jsp as
<jsp:UseBean..... scope="session">
2.All other jsp pages while using their corresponding view bean keeping scope as "request"
<jsp:UseBean..... scope="request">
3.Now i m using above common jsp in all jsp pages as
<jsp: include page="/common.jsp"....../>
4.Few jsp pages are framed pages ,while using their corresponding view bean they are keeping scope as "session"(they have to keep scope "session" only as request is not available at framed pages). i m enforcing scope at common jsp as session(step 1) just to make this page avaialble at my few framed jsp pages.
5. For dispatching requests from worker servlets corresponding to each jsp, i m using request.setAttribute("viewObject",viewObject) and forwards the request to corresponding jsp.
6. For dispatching requests from worker servlets corresponding to framed jsp pages, i m using request.getSession().setAttribute("viewObject",viewObject) and forwards the request to corresponding jsp.
In my application i m not using threads.
Let me know what i need to do explicitly in coding as i m scared of any performance issues bcoz of creating session objects in step 6.
Originally posted by Steve Leach:
I'm not sure what you mean by pages having session or request scope.
If you mean that some pages have "jsp:useBean scope=session" and some have "jsp:useBean scope=request" then you need to be aware that you are using different instances of the bean class. All of the session scope bean accesses refer to the same object. Each request scope access creates a new instance.
If the CommonView bean is thread-safe then you might as well make all accesses session scope. This will save the server from having to create lots of new instances.
If the bean is not thread safe then you need to make all accesses request or page scope. If you don't then you will get problems if a user requests a page twice at the same time; for example refreshing multiple frames, or hitting refresh twice quickly.
Either way, it should be all session scope or all request (or page) scope, not a mixture.
Finally, if the CommonView bean has no session related fields at all then you could consider making it application scope. This will mean there will only be a single instance for all users, but you had better be sure it is fully thread safe.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12327
    
    1
In my application i m not using threads.
Let me know what i need to do explicitly in coding as i m scared of any performance issues bcoz of creating session objects in step 6.

"Scared of performance issues...."
If you have not actually measured and profiled, and determined that session creation is a significant resource consumer, there is absolutely no reason to avoid session objects.
Take a look at the Java code that your JSP pages are turned into - they are loaded with object creation - much of which could be removed by tedious hand optimization, but whats the point??
Bill


Java Resources at www.wbrogden.com
Steve Leach
Ranch Hand

Joined: Sep 24, 2003
Posts: 46
OK, I'm starting to understand a bit better now.
The first point is that by developing with JSP your application is multithreaded - you can't help it.
I think we need to clarify what the viewObject does.
Does it contain details that are specific to a particular request, or does it contain values that apply to all requests for a particular user ?
Your step 6 sounds dangerous to me. Lets say you have 2 frames and the user hits the refresh button so the browser requests new copies of both frames. What will happen may well be
1. Servlet handles request 1, creates viewObject and puts it in request scope, then forwards to jsp 1
2. Servlet handles request 2, creates viewObject and replaces the one created in step 1, then forwards to jsp 2
3. Jsp1 runs, and gets the viewObject, but is actually working with the one created in step 2
At this stage, I think performance is probably the least of your worries (unless I am misunderstanding something).
Cheers
Steve
vikasids sharma
Ranch Hand

Joined: Aug 01, 2003
Posts: 157
Steve ,viewObjects are basically objects or instances of beans(classes with getter setter).These are instantiated for every user on worker servlet.
These are used to work with beans or precisely to set values in setter methods of classes.Correponding getters(jsp:getProperty) are used in JSP when we forward viewObject through request.setAttribute("viewObject",viewObject)to respective JSP.
Originally posted by Steve Leach:

I think we need to clarify what the viewObject does.
Does it contain details that are specific to a particular request, or does it contain values that apply to all requests for a particular user ?

Yes , here servlet handles request1 ,creates viewObject but puts in "session" scope rather than "request" when we talk abt frames.No doubt in other cases it puts in "request" scope.

Your step 6 sounds dangerous to me. Lets say you have 2 frames and the user hits the refresh button so the browser requests new copies of both frames. What will happen may well be
1. Servlet handles request 1, creates viewObject and puts it in request scope, then forwards to jsp 1

Say i creates viewObject and puts in by request.getSession().setAttribute("",..).Now as its a multiuser system.So as another user comes , another object will be created for him and again it will be put in session....
Does it makes too many session ojects in application or those are replaced?
Do i need to remove these objects from session and if yes again let me know how can i remove an object from session(may be it is being used by some user)

2. Servlet handles request 2, creates viewObject and replaces the one created in step 1, then forwards to jsp 2
3. Jsp1 runs, and gets the viewObject, but is actually working with the one created in step 2
At this stage, I think performance is probably the least of your worries (unless I am misunderstanding something).
Cheers
Steve

 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: problem while keeping scope as session
 
Similar Threads
How to persist a Stateless Session Bean over several jsp pages?
Object visibility
jsp scope problem
session scopt problem
problem with registration page