Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Request Attributes

 
Milton Ochoa
Ranch Hand
Posts: 336
Firefox Browser Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64629
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
S Sravs
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think you better refer HFSJ 1.4
pg no.202 last Dumb Question and pg no.228

Regards
Sravanthi
 
Prem Kashyap
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think, container creates new thread for each request. There is no ambiguity between two requests.
 
Charles Lyons
Author
Ranch Hand
Posts: 836
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Prem Kashyap
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 836
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 836
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for the wonderful explanation.

Regards

Prem Kashyap
 
Milton Ochoa
Ranch Hand
Posts: 336
Firefox Browser Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the detailed answer. Now I am most confident.

Thank you All.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic