• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Request attributes within a servlet

 
Faisal Ahmad
Ranch Hand
Posts: 355
Chrome Java Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 611
1
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Faisal Ahmad
Ranch Hand
Posts: 355
Chrome Java Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 611
1
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic