programming4us
programming4us
WEBSITE

Java EE 6 : Servlet Development and Deployment - Persisting application data across requests

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
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 this object can be retrieved later by invoking the HttpRequest.getAttribute() method. This approach only works if the request was forwarded to the servlet by 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 by 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 must be invoked. This method returns an instance of javax.servlet.http.HttpSession. We then call the HttpSession.setAttribute() method to attach the object to the session. The following code fragment illustrates the process:

protected void doPost(HttpServletRequest request, HttpServletResponse response)
{
.
.
.
Foo foo = new Foo(); //theoretical object
HttpSession session = request.getSession();
session.setAttribute("foo", foo);
.
.
.
}


					  

We can then retrieve the object from the session by calling the HttpSession.getAttribute() method:

protected void doPost(HttpServletRequest request, HttpServletResponse response)
{
HttpSession session = request.getSession();
Foo foo = (Foo)session.getAttribute("foo");
}


					  

Notice how the return value of session.getAttribute() needs to be casted to the appropriate type. This is necessary as the return value of this method is java.lang.Object.

The procedure to attach and retrieve objects to and from the servlet context is very similar. The servlet needs to call the getServletContext() method (defined in the class called GenericServlet, which is the parent class of HttpServlet, which in turn is the parent class of our servlets). This method returns an instance of javax.servlet.ServletContext, which defines a setAttribute() and a getAttribute() method. These methods work the same way as their HttpServletRequest and HttpSessionResponse counterparts.

The procedure to attach an object to the servlet context is illustrated in the following code snippet:

protected void doPost(HttpServletRequest request, HttpServletResponse response)
{
//The getServletContext() method is defined higher in
//the inheritance hierarchy.
ServletContext servletContext = getServletContext();
Foo foo = new Foo();
servletContext.setAttribute("foo", foo);
.
.
.
}


					  

This code attaches the foo object to the servlet context. This object will be available to any servlet in our application and will be the same across sessions. It can be retrieved by calling the ServletContext.getAttribute() method, as illustrated next:

protected void doPost(HttpServletRequest request, HttpServletResponse response)
{
ServletContext servletContext = getServletContext();
Foo foo = (Foo)servletContext.getAttribute("foo");
.
.
.
}


					  
This code obtains the foo object from the request context. A cast is again needed as the ServletContext.getAttribute() method, like its counterparts, returns an instance of java.lang.Object.

Objects attached to the servlet context are said to have a scope of application. Similarly, objects attached to the session are said to have a scope of session, and objects attached to the request are said to have a scope of request.

Other  
 
Top 10
Free Mobile And Desktop Apps For Accessing Restricted Websites
MASERATI QUATTROPORTE; DIESEL : Lure of Italian limos
TOYOTA CAMRY 2; 2.5 : Camry now more comely
KIA SORENTO 2.2CRDi : Fuel-sipping slugger
How To Setup, Password Protect & Encrypt Wireless Internet Connection
Emulate And Run iPad Apps On Windows, Mac OS X & Linux With iPadian
Backup & Restore Game Progress From Any Game With SaveGameProgress
Generate A Facebook Timeline Cover Using A Free App
New App for Women ‘Remix’ Offers Fashion Advice & Style Tips
SG50 Ferrari F12berlinetta : Prancing Horse for Lion City's 50th
- Messages forwarded by Outlook rule go nowhere
- Create and Deploy Windows 7 Image
- How do I check to see if my exchange 2003 is an open relay? (not using a open relay tester tool online, but on the console)
- Creating and using an unencrypted cookie in ASP.NET
- Directories
- Poor Performance on Sharepoint 2010 Server
- SBS 2008 ~ The e-mail alias already exists...
- Public to Private IP - DNS Changes
- Send Email from Winform application
- How to create a .mdb file from ms sql server database.......
programming4us programming4us
programming4us
 
 
programming4us