This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Request Attributes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Request Attributes" Watch "Request Attributes" New topic
Author

Request Attributes

Milton Ochoa
Ranch Hand

Joined: Oct 23, 2007
Posts: 336

Greetings,

I have a question (my firts question on Web Component)

The HFSJ Book say that the "Request Attributes" are thread-safe.

But... Thinking, i imagine the next situation:

-Supose that ONE Servlet set a attribute on the request object and Dispatch the request to a JSP (JSP1), and before complete the service method, Dispatch again to another JSP or Servlet (<- is that posible?)
-The JSP1 take the request send by the servlet, and the jsp page will procesing that request. but.. before proceseing the request, the another Sevlet (or JSP2) change the attribute, setting a new Attribute (change) to the request, and finally, the JSP1 process the firts request.

My question is: Is that posible? or no ?

Please, explaime in detail, maybe I am thinking something absurt, (but i am a beginner on Servlet and JSP :roll: )

Thank you in advance
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60774
    
  65

another Sevlet (or JSP2) change the attribute
How would that other servlet or JSP obtain a reference to the request in order to change the attribute?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
S Sravs
Ranch Hand

Joined: Feb 25, 2008
Posts: 85
i think you better refer HFSJ 1.4
pg no.202 last Dumb Question and pg no.228

Regards
Sravanthi
Prem Kashyap
Ranch Hand

Joined: Oct 10, 2006
Posts: 52
Once you have dispatched the request to JSP1, the service method ends there.
Once you give RequestDispatcher.forward(req,resp), the control goes to the JSP1. Nothing in the service method afer the forward statement will execute.

From JSP1, you will be able to dispatch it to JSP2.

So in this way request attribute are thread safe, as at one time only one component is reading or writing the request attributes.

Regards
Prem Kashyap
prabakaran perumal
Greenhorn

Joined: Jan 24, 2007
Posts: 27
I think, container creates new thread for each request. There is no ambiguity between two requests.
Charles Lyons
Author
Ranch Hand

Joined: Mar 27, 2003
Posts: 836
Once you give RequestDispatcher.forward(req,resp), the control goes to the JSP1. Nothing in the service method afer the forward statement will execute.
Not quite - the forward method will return once JSP1 has totally finished executing. If any Java method didn't return, the whole call stack would be ruined! Java methods always return, without exception. The difference is that in this case it blocks until the called resource has finished its work. And your second statement is therefore untrue: the rest of the service method will execute, but only once the forward has completed. You are free to put whatever code you want in your servlet after the forward, except that it shouldn't modify the response any further (though it can, if you want some exceptions ).
So in this way request attribute are thread safe, as at one time only one component is reading or writing the request attributes.
Yes - the whole point here is that each request is handled by one thread in the container, and that thread's execution can only be in one Java method at a time (due to the way the call stack is structured) - this means only one component (servlet/JSP) in the thread can be executing at any one instant, so your request scope is thread-safe - it belongs only to the executing thread.


Charles Lyons (SCJP 1.4, April 2003; SCJP 5, Dec 2006; SCWCD 1.4b, April 2004)
Author of OCEJWCD Study Companion for Oracle Exam 1Z0-899 (ISBN 0955160340 / Amazon Amazon UK )
Prem Kashyap
Ranch Hand

Joined: Oct 10, 2006
Posts: 52
Thanks Charles for correcting me.

However what is the behavior when we call jsp:forward from a jsp page 1?. The control goes to the forwarded jsp page 2. Then the rest of the jsp page 1 after the forward does not execute? Is that correct?

Regards

Prem Kashyap
Charles Lyons
Author
Ranch Hand

Joined: Mar 27, 2003
Posts: 836
However what is the behavior when we call jsp:forward from a jsp page 1?. The control goes to the forwarded jsp page 2. Then the rest of the jsp page 1 after the forward does not execute? Is that correct?
Yes, that's right. The JSP translator adds some housekeeping code into the servlet so any content you wrote in the rest of the page is skipped. For example, the Resin 3.1.3 container translates this simple JSP:into the following servlet snippet:I put the relevant bit in bold, so you see using <jsp:forward> causes the JSP's service method to return execution once the forward completes. It uses the if(true) bit to stop the compiler complaining about "unreachable statements" below the return - some containers may just remove all the lines below it altogether!
Charles Lyons
Author
Ranch Hand

Joined: Mar 27, 2003
Posts: 836
I also meant to add that there is very little reason to use <jsp:forward /> I think. It is far better to use the Front Controller pattern or similar to programmatically determine which page to display, then forward using the regular RequestDispatcher API from that servlet. Using <jsp:include /> on the other hand can be useful, for example in templating/layout schemes incorporating lots of different pages (Composite View pattern).
Prem Kashyap
Ranch Hand

Joined: Oct 10, 2006
Posts: 52
Thanks a lot for the wonderful explanation.

Regards

Prem Kashyap
Milton Ochoa
Ranch Hand

Joined: Oct 23, 2007
Posts: 336

Thank you for the detailed answer. Now I am most confident.

Thank you All.
 
 
subject: Request Attributes
 
Similar Threads
How to do Action mapping for this scenario ??
HttpServletRequest question
how to create a Http session in JSP page ?
HFSJ question on session attribute
Are request scope attributes thread-safe?