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;
}
...
...
}