High performance, high resilient JSP and servlet optimization

Source: Internet
Author: User
Tags config sql

Is your Java application running very slowly? Can they afford to keep up with the rising traffic? This article describes the development of high-performance, highly resilient JSP pages and the performance optimization of the servlet technology. The idea is to build as fast as possible and adapt to a growing number of users and their requests. In this article, I'm going to take you through the performance tuning techniques that have been practiced and proven, which will greatly improve the performance of your servlet and JSP pages, and hence the performance of Java EE. Parts of these technologies are used in the development phase, such as the design and coding phases. Another part of the technology is related to configuration.

Technology 1: Caching data in the HttpServlet init () method

The servlet's init () method is invoked by the server after the servlet instance is created and before any requests are processed by the servlet. This method is invoked only once in the life cycle of the servlet. To improve performance, cache static data in init () or complete costly operations to complete during initialization. For example, a best practice is to use a JDBC connection pool that implements the Javax.sql.DataSource interface. DataSource is obtained from the Jndi tree. Using Jndi to find DataSource every time you invoke SQL is a very expensive task and severely affects the performance of your application. The init () method of the servlet can be used to get 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;
     }
    ...
    ...
  }



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.