Method One: Caching data in the Init () method of the Servlet
After the application server initializes the servlet instance, it invokes the Init () method of the servlet before servicing the client request. In the lifecycle of a servlet, the init () method is invoked only once. System performance can be greatly improved by caching some static data in the Init () method or by completing some time-consuming operations that need to be performed only once.
For example, creating a JDBC Connection pool in the init () method is a good example, assuming that we are using the jdbc2.0 DataSource interface to get a database connection, and that, in general, we need to get a specific data source through Jndi. We can imagine that in a specific application, if a jndi query is executed every time a SQL request is performed, the performance of the system will drop dramatically. The workaround is the following code, which is cached datasource, making it possible for the next SQL call to continue to take advantage of it:
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;
}
...
...
}
Method 2: Disable servlet and JSP automatic overloading (auto-reloading)