Saving HttpServletRequest object in session

javacube used Ask the Experts™
I'm having a problem with HttpServletRequest object which I save as a session attribute in order to retrieve its properties later.

HttpServletRequest originalRequest = request;

HttpServletRequest originalRequest = (HttpServletRequest)session.getAttribute("originalRequest");

The problem is that originalRequest.getRequestURI() is null, although an HttpSessionAttributeListener shows that the attribute was saved correctly (attributeAdded: originalRequest.getRequestURI() is correct), and that it had not been replaced or removed.

My question is:
How to prevent changes to the request object saved as a session attribute and how to access this request object from another servlet?

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
It is a major design flaw to save request for later use.
cite from servlet specification (page 37):

SRV.4.10 Lifetime of the Request Object
Each request object is valid only within the scpoe of a servlet’s service method, or within the scope of a filter’s doFilter method. Containers commonly recycle request objects in order to avoid the performance overhead of request object creation. The developer must be aware that maintaining references to request objects outside the scope described above may lead to non-deterministic behavior.

to download spec:

Solution: extract the required information from request object and save that object as session attribute. Of course, you can use collection or your own composite object if you have a lot of detail to handle
let me know if you have any further question

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial