• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Request Attributes

 
Ranch Hand
Posts: 336
Firefox Browser Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 67754
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think, container creates new thread for each request. There is no ambiguity between two requests.
 
Author
Posts: 836
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 836
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 836
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for the detailed answer. Now I am most confident.

Thank you All.
reply
    Bookmark Topic Watch Topic
  • New Topic