Web hosting plans - 360 CHAPTER 14 SOLVING COMMON JAVA EE

360 CHAPTER 14 SOLVING COMMON JAVA EE PERFORMANCE PROBLEMS object. The HttpServletRequest object serves as a communication mechanism for various components in your dynamic Web tier, but as soon as the request is complete and the socket connected to the user is closed, the servlet container frees all variables stored in the HttpServletRequest. These variables exist for the lifetime of a single request. HTTP is a stateless protocol, meaning that a client makes a request of the server, the server responds to the request, the communication is terminated, and the conversation is complete. Because we appreciate being able to log on to a Web page, add items to a shopping cart, and then check out, Web servers have devised a mechanism to define an extended conversation that spans multiple requests the session. Attributes and parameters can be stored on a per user basis inside an HttpSession object, and then accessed by any servlet or JSP in the application when that user accesses them. In this way, the login page can locate your information and add it to the HttpSession, so that the shopping cart can add items to it and the check out page can access your credit card number to bill you. For a stateless protocol, the client always initiates the communication with the server, requiring the server to know how long the maximum break in communications can be before it considers the conversation over and discards the user s data. This length of time is referred to as the session time-out, and it is configurable inside the application server. Unless objects are explicitly removed from the session or the session is programmatically invalidated, objects will stay in the session for at least the duration of the time-out, measured from the last time the user accessed the Web server. While the session manages objects on a per-user basis, the ServletContext object manages objects on an application basis. The ServletContext is sometimes referred to as application scope, because through a servlet s ServletContext or a JSP s application object, you are able to maintain and share objects with all other servlets and JSPs for all users in the same application. The ServletContext is a prime location to place application configuration information and to cache application-wide data, such as database JNDI lookup results. If data is not stored in one of these four predefined areas: page scope, request scope, session scope, or application scope, objects may be stored in the following objects: Static variables Long-lived class variables Static variables are maintained in the JVM on a per-class basis and do not require a class instance to be alive in the heap for the static variable to exist. All class instances share the same static variable values, so changing a static variable in one class instance affects all other instances of the same class type. Therefore, if the application places an object into a static variable for a class and nullifies that variable, the static object is not reclaimed by the JVM. These static objects are prime locations for leaking memory! Finally, objects can be added to internal data structures or member variables inside long- lived classes such as servlets. When a servlet is created and loaded into memory, it has only one instance in memory, and multiple threads are configured to access that servlet instance. If it loads configuration information in its init() method, stores it in class variables, and reads that information while servicing requests, then all instances are assured of seeing the same information. One common problem that I have seen is the use of servlet class variables to store data such as page caches. These caches, in and of themselves, are good to have, but probably the worst place to manage them is from inside a servlet. If you are considering using a cache, then you are best served by integrating a third-party cache, like Tangosol s Coherence, into your application framework for that specific purpose.
Searching for affordable and proven webhost to host and run your servlet applications? Go to Linux Web Hosting services and you will find it.

Leave a Reply