Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

Share this Page URL

Chapter 14. Writing WebLogic Server Serv... > Best Practices for Implementing Serv... - Pg. 363

Writing WebLogic Server Servlets 363 them. This feature comes in handy in development when there are frequent changes to the code. The developer can configure the interval at which the WebLogic Server automatically checks for reloading by using the Server Reload attribute, which is present on the descriptor tab for each Web application configuration, as shown in Figure 14.4. Best Practices for Implementing Servlets As we've seen, servlets offer a powerful mechanism for integrating Web servers and enterprise- wide business components. Now let's look at some of the best practices that are adopted in enter- prise applications that involve servlets as one of the layers. · Leverage the MVC pattern --Use servlets as controllers, as described in the MVC pattern. Pre- sentation is typically done using a JSP and the servlet is left to invoke the business component and pass on the necessary data for the presentation layer. · Use of SingleThreadModel--SingleThreadModel servlets should be avoided because most of the servlet container implementations have a huge system overhead. Instead use thread synchronization when necessary. · Use HttpSession s --By design, servlets are meant to be stateless component and they use external objects such as HttpSession to maintain state information for a user conversation. Application servers serialize inactive sessions and store them on disk because these objects take a lot of memory and the server has to share the allocated memory efficiently for other resources such as EJBs and JMS resources. Sessions are normally released from memory using the invalidate() function or when they time out. So, users have to take care of im- plementing one of these to make sure that sessions are released when the client is done with the conversation. Also, keep the session object lean and store valuable data in a database instead of storing it as part of session to prevent losing the data in unpredictable server crashes. · Use the Init() method --As explained, the init() method is invoked when the servlet in- stance is created. This is the most convenient method for performing some of the complex logic that must be performed once; for example, JDBC connection pool initialization, EJB home lookup, and so on. The init() method is also the best place to initialize the servlet instance variables because this method is threadsafe by default. · Synchronization in servlets --This should be avoided because large synchronization blocks make a servlet single threaded and thereby decreases the throughput drastically. · Optimization techniques in the service() method --Apart from minimizing the use of synchro- nization blocks, some of the following optimization techniques can be used as well: setting con- tent-length variables for efficient flushing, using ServletOutputStream instead of Print- Writer, and using the print() method for println() and partial data flushing. Along with this, using StringBuffer for string concatenation improves performance. Using caching (when appropriate) and pre-generating large Web sites can help, too. · Using the destroy method --The destroy method should be used to free up resources such as database connections and initial contexts and to close sockets and files. · Modularity improvements --All complex logic (for example, EJBs) should be moved to a separate business layer to keep the logic in the servlet simple. · Shared resources --A servlet being multithreaded should take special care when modifying servlet instance variables because these are shared by multiple resources. Use synchronization and/or transactions. · Connection pooling --Try to avoid obtaining a connection to the database or a backend legacy application (such as a Tuxedo connection) for every request. Use connection pooling wherever possible and initialize using the init method. The developer should take care of releasing connections when the usage is over, such as in the destroy method of the servlet. The data