This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Request attributes within a servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Request attributes within a servlet" Watch "Request attributes within a servlet" New topic
Author

Request attributes within a servlet

Faisal Ahmad
Ranch Hand

Joined: Aug 31, 2006
Posts: 355

I have a servlet and in order to maintain thread-safety, I've removed all the instance variables from it. I have multiple methods in this servlet. The request parameters and their values must be shared among these methods. This servlet wouldn't forward the response to any jsp/servlet. Instead, it generates a xml file which is directly written to the response's output stream. Right now, I am using request attributes to share the information between different methods of the same servlet. Is this a good approach? Are there any other alternative ways that I'm missing? I read, request attributes are used in combination with RequestDispatcher. However, I don't need any RequestDispatcher. Does my approach make sense? Could you please guide me?

Many thanks in advance!
Piotr Nowicki
Ranch Hand

Joined: Jul 13, 2010
Posts: 610

Request attributes are good choice, as they're thread safe.
You can use them without RequestDispatcher. Using RequestDispatcher is not a must requirement but rather commonly used approach.


OCP Java SE 6 Programmer, OCM Java SE 6 Developer, OCE Java EE 6 JSPSD, OCE Java EE 6 EJBD, OCE Java EE 6 JPAD, Spring 3.0 Core Professional.
Faisal Ahmad
Ranch Hand

Joined: Aug 31, 2006
Posts: 355

Thanks Pedro!
I have another question: When do the request attributes cease to exist? How long could they be held by container?

Many thanks in advance!
Piotr Nowicki
Ranch Hand

Joined: Jul 13, 2010
Posts: 610

As far as I remember (somebody fix me if i'm wrong!), the container creates new Request and Response objects before it gets to the Servlet's service() method (just before assigning new thread to this request).
After the response is sent to the client, the thread finishes and Response and Request objects are eligible for GC.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Request attributes within a servlet