Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

Best Practice: Using HttpServlet init method

Harvey Gunther (hgunther@us.ibm.com), Senior Performance Analyst, IBM
Harvey Gunther is a Senior Performance Analyst with the IBM WebSphere Product Development team in Raleigh, North Carolina.

Summary:  This best practice recommends using the HttpServlet init method as the init method is only called once when the servlet is loaded, thereby improving performance.

Date:  08 Aug 2001
Level:  Intermediate

Activity:  34759 views
Comments:  

Introduction

If a servlet has a one-time and expensive operation, incorrect coding techniques can lead to degraded performance. Use the HttpServlet init method to perform one-time expensive operations, such as acquiring thread-safe resource acquisition mechanisms. The overall performance impact significantly reduces when using the HttpServlet init method as the init method is only called once when the servlet is loaded.

This best practice applies to the following product, version, and platform:

  • WebSphere Application Server Base, all platforms, versions 3.0.2.x, 3.5.x, 4.0

Recommendation

Because the Servlet init() method is invoked when the servlet instance is loaded, it is the perfect location to carry out expensive operations that need only be performed during initialization. By definition, the init() method is thread-safe. The results of operations in the HttpServlet.init() method can be cached safely in servlet instance variables, which become read-only in the servlet service method.

The following shows the Servlet init method used for obtaining and caching the JDBC DataSource. One effective use of the Servlet init method is the creation and caching of thread-safe resource acquisition mechanisms, such as JDBC DataSources, EJB Homes, and Web Services SOAP Mapping Registry.

Using the Servlet.init method to acquire JDBC DataSource

      
public class BpAllBadThingsServletV5 extends HttpServlet
{
            // Caching the DataSource - It is obtained in the Servlet.init() method
            private javax.sql.DataSource ds = null;

            // This Happens Once and is Reused
            public void init(ServletConfig config) throws ServletException
            {
                       super.init(config);
                       Context ctx = null;
                       try
                       {
                                 java.util.Hashtable env = new java.util.Hashtable();
                                 env.put(Contex.INITIAL_CONTEXT_FACTORY,
                                  "com.ibm.ejs.ns.jndi.CNInitialContextFactory");

                                 ctx = new InitialContext(env);
                                 ds = (javax.sql.DataSource)ctx.lookup("jdbc/SAMPLE");
                                 ctx.close();
                       }
                       catch(Exception es)
                       {
                                es.printStackTrace();
                       }
            }
}


Alternative

The following shows an incorrect way to acquire the JDBC DataSource. It accomplishes the same task, but the performance in this case is significantly worse, because the DataSource acquisition is done every time the doGet method is invoked.

Wrong way to acquire a data source

public class BpAllBadThingsServletV2a extends HttpServlet
{
           public void doGet(HttpServletRequest request, HttpServletResponse response)
                             throws ServletException, IOException
           {
                      Connection conn = null;
                      ResultSet rs = null;
                      PreparedStatement pStmt = null;
                      javax.sql.DataSource ds = null

                      try
                      {
                                 java.util.Hashtable env = new java.util.Hashtable();
                                 env.put(Context.INITIAL_CONTEXT_FACTORY,
                                   "com.ibm.ejs.ns.jndi.CNInitialContextFactory");

                                 ctx = new InitialContext(env);
                                 ds = (DataSource)ctx.lookup("jdbc/SAMPLE");
                                 ctx.close();

                                 conn = ds.getConnection("db2admin", "db2admin");
                                 pStmt = conn.prepareStatement
                                   ("select * from hgunther.employee");
                                 rs = pStmt.executeQuery
                      }
                      catch(Exception(es)
                      {
                               es.printStackTrace();
                      }
           }
}


Resources

About the author

Harvey Gunther is a Senior Performance Analyst with the IBM WebSphere Product Development team in Raleigh, North Carolina.

Report abuse help

Report abuse

Thank you. This entry has been flagged for moderator attention.


Report abuse help

Report abuse

Report abuse submission failed. Please try again later.


developerWorks: Sign in


Need an IBM ID?
Forgot your IBM ID?


Forgot your password?
Change your password

By clicking Submit, you agree to the developerWorks terms of use.

 


The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

Choose your display name

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

(Must be between 3 – 31 characters.)

By clicking Submit, you agree to the developerWorks terms of use.

 


Rate this article

Comments

Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=WebSphere
ArticleID=14552
ArticleTitle=Best Practice: Using HttpServlet init method
publish-date=08082001
author1-email=hgunther@us.ibm.com
author1-email-cc=

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

For articles in technology zones (such as Java technology, Linux, Open source, XML), Popular tags shows the top tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), Popular tags shows the top tags for just that product zone.

For articles in technology zones (such as Java technology, Linux, Open source, XML), My tags shows your tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), My tags shows your tags for just that product zone.

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Try IBM PureSystems. No charge.

Special offers