Persisting application data across requests
In the previous section, we saw how it is possible to store an object in the request by invoking the HttpRequest.setAttribute()
method, and how later this object can be retrieved by invoking the HttpRequest.getAttribute()
method. This approach only works if the request was forwarded to the servlet invoking the getAttribute()
method. If this is not the case, the getAttribute()
method will return null.
It is possible to persist an object across requests. In addition to attaching an object to the request object, an object can also be attached to the session object or to the servlet context. The difference between these two is that objects attached to the session will not be visible to different users, whereas objects attached to the servlet context are.
Attaching objects to the session and servlet context is very similar to attaching objects to the request. To attach an object to the session, the HttpServletRequest.getSession()
method...