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)
SERVLET/JSP provides a practical technology, automatic overload technology, which provides developers with a good development environment, when you change the servlet and JSP pages without restarting the application server. However, this technology is a great drain on the resources of the system during the operation of the product, because it will bring a great burden to the JSP engine class loader (ClassLoader). Therefore, turning off the automatic overload feature is a great help to improve system performance.
Method 3: Do not abuse httpsession
In many applications, our programs need to maintain the state of the client so that the pages can be interconnected. Unfortunately, because HTTP is inherently stateless, the state of the client cannot be saved. Therefore, the general application Server provides a session to save the state of the customer. In the JSP application server, it is through the httpsession to realize the function of the session, but at the same time, it also brings a small burden to the system. Because every time you get or update the session, the system needs to serialize it in a time-consuming process. You can improve the performance of the system by following several processing methods for httpsession.
If not necessary, you should close the default settings for HttpSession in the JSP page. If you do not explicitly specify, each JSP page will default to create a httpsession. If you do not need to use session in your JSP, you can disable it by using the following JSP page designator:
<%@ page session= "false"%>
Do not store large data pairs in HttpSession: If you store large data pairs in HttpSession, the application server will serialize it whenever you read and write to it, increasing the extra burden on the system. The larger the data you store in the HttpSession, the faster the performance of the system will fall.
When you don't need to httpsession, release it as quickly as possible: when you no longer need a session, you can release it by calling the Httpsession.invalidate () method. Try to set the timeout for the session short: there is a default timeout for the session in the JSP application server. When the customer has not done anything after this time, the related session is automatically freed from memory. The larger the timeout time, the lower the performance of the system, so the best way is to keep its value at a lower level as much as possible.
Method 4: Compress the page output
Compression is a good way to solve data redundancy, especially in the network bandwidth is not developed today. Some browsers support gzip (GNU Zip) to compress HTML files, which dramatically reduces the time to download HTML files. So, if you compress the HTML page generated by the servlet or JSP page, the user will feel the page is browsing very quickly. Unfortunately, not all browsers support gzip compression, but you can check to see if the client's browser supports it in your program. Here is a snippet of the implementation of this method:
public void doget (HttpServletRequest request, httpservletresponse response)
Throws IOException, Servletexception {
OutputStream out = null;
String encoding = Request.getheader ("accept-encoding");
if (encoding!= null && encoding.indexof ("gzip")!=-1) {
Request.setheader ("content-encoding", "gzip");
out = new Gzipoutputstream (Request.getoutputstream ());
}
else if (encoding!= null && encoding.indexof ("Comdivss")!=-1) {
Request.setheader ("content-encoding", "Comdivss");
out = new Zipoutputstream (Request.getoutputstream ());
}else{
out = Request.getoutputstream ();
}
...
...
}