Wizard API updated!
Tim Boudreau has released a new version of the Swing Wizard library (version 0.997) that fixes the WizardException bug reported in JavaWorld's recent Open Source Java Project profile. The article's examples have been reworked to test out the new, improved WizardException. Thanks, Tim, for this helpful fix!
Open Source Java Projects: The Wizard API

Newsletter sign-up

Sign up for our technology specific newsletters.

Enterprise Java
View all newsletters

Email Address:

Servlet and JSP performance tuning

Improve your enterprise application's performance by tweaking your servlets and JSPs

Do your J2EE applications run slow? Can they sustain rising traffic? This article describes performance-tuning techniques (PTT) for developing high performance and scalable JSP (JavaServer Pages) pages and servlets. That means building applications that are reasonably and consistently fast, and can scale up to the increasing number of users and/or requests. In this article, I walk you through the practical and proven performance-tuning techniques that will boost the performance of your servlets and JSP pages tremendously, thus improving the performance of your J2EE applications. Some of these techniques apply during the development phase, i.e., while you design your application and write the code. And some of these techniques are configuration-related.

PTT 1: Use the HttpServlet init() method for caching data

The server calls the servlet's init() method after the server constructs the servlet instance and before the servlet handles any requests. It is called only once in a servlet's lifetime. init() can be used to improve performance by caching the static data and/or completing the expensive operations that need to be performed only during initialization.

For example, it is a best practice to use JDBC (Java Database Connectivity) connection pooling, which involves the use of the javax.sql.DataSource interface. DataSource is obtained from the JNDI (Java Naming and Directory Interface) tree. Performing the JNDI lookup for DataSource for every SQL call is expensive and severely affects an application's performance. Servlet's init() method should be used to acquire DataSource and cache it for later reuse:

public class ControllerServlet extends HttpServlet
{
   private javax.sql.DataSource testDS = null;
   public void init(ServletConfig config) throws ServletException
   {
      super.init(config);   
      Context ctx  = null;
      try
      { 
         ctx = new InitialContext();
         testDS = (javax.sql.DataSource)ctx.lookup("jdbc/testDS");
      }
      catch(NamingException ne)
      {
         ne.printStackTrace();              
       }
       catch(Exception e)
       {
          e.printStackTrace();
       }
   }
   public javax.sql.DataSource getTestDS()
      {
         return testDS;
      }
   ...
   ...   
}


PTT 2: Disable servlet and JSP auto-reloading

Servlet/JSP auto-reloading proves useful during the development phase because it reduces development time, as you do not have to restart the server after every change in the servlet/JSP. However, it is expensive in the production phase; servlet/JSP auto-reloading gives poor performance because of unnecessary loading and burdening on the classloader. Also, it may put your application in strange conflicts when classes loaded by a certain classloader cannot cooperate with classes loaded by the current classloader. So turn off auto-reloading for servlet/JSP in a production environment to receive better performance.

PTT 3: Control HttpSession

Many applications require a series of client requests so they can associate with one another. Web-based applications are responsible for maintaining such state, called a session, because the HTTP protocol is stateless. To support applications that must maintain state, Java servlet technology provides an API for managing sessions and allows several mechanisms for implementing sessions. Sessions are represented by an HttpSession object, but a cost is involved while using it. An HttpSession must be read by the servlet whenever it is used and rewritten when it is updated. You can improve performance by applying the following techniques:

1 | 2 | 3 |  Next >

Discuss

Start a new discussion or jump into one of the threads below:

Subject Replies Last post
. Session
By Tom Wang
1 05/04/08 07:29 PM
by Anonymous
. Use gzip compression
By Anonymous
4 04/22/08 06:02 AM
by Anonymous
. Good article.
By Chavez
6 04/22/08 06:02 AM
by Anonymous
. Problem with gzip compression code sample
By Anonymous
1 04/22/08 06:02 AM
by Anonymous
. Servlet and JSP performance tuning
By JavaWorldAdministrator
4 04/22/08 06:02 AM
by Anonymous
. Thanks
By Namita
2 04/02/08 09:25 AM
by Anonymous
. Implementation example of thread pool
By Julien Martin
3 02/10/08 10:51 PM
by Anonymous
. + does not create temporary object.
By Pak Young-rok
4 10/16/07 08:58 AM
by Anonymous
. large object graphs inside an HttpSession
By Martin
11 06/25/07 08:15 PM
by Anonymous
. ControllerServlet
By Pancho
2 06/25/07 05:06 PM
by Anonymous
. Do not use SingleThreadModel
By Anonymous
1 06/17/07 04:54 AM
by Anonymous
. Tomcat does Ccompression on it's own
By Anonymous
1 05/19/07 10:12 AM
by Anonymous


Resources