Friday, March 25, 2005

Storing Request-specific Data Without Using ServletRequest

Storing Request-specific Data Without Using ServletRequest: "Expertise: Advanced
Language: Java
March 22, 2005
Storing Request-specific Data Without Using ServletRequest
There are times when you need to store request-specific attributes (pieces of data that must be available to one specific http-request during the lifetime of that http-request) in order to retrieve them later.

This is easy—if you have access to the ServletRequest instance. You simply use its methods, getAttribute() and setAttribute(), for attribute storage.

But sometimes your code does not provide access to the current request's ServletRequest instance. One way around this is to modify the parameter-list of methods to pass the ServletRequest instance all the way down to the method that needs the request-specific attributes. This would use the ServletRequest's getAttribute() method. However, this refactoring is not always possible.

Another solution is to use the thread's local memory, because each ServletRequest is executed in one thread only (it does not switch threads)."

No comments: