Seven Tips for improving JSP page response speed (1)

Source: Internet
Author: User

Method 1: cache data in the servlet init () method

After the application server initializes the servlet instance and provides services for client requests, it will call the init () method of the servlet. In the lifecycle of a servlet, The init () method is called only once. By caching some static data in the init () method or completing some time-consuming operations that only need to be executed once, the system performance can be greatly improved.

For example, setting up a JDBC connection pool in the init () method is the best example. Suppose we use the DataSource interface of jdbc2.0 to obtain the database connection. In general, we need to obtain the specific data source through JNDI. We can imagine that in a specific application, if every SQL request needs to execute a JNDI query, the system performance will drop sharply. The solution is the following code, which caches DataSource so that the next SQL call can continue to use 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 auto-reloading)

Servlet/JSP provides a practical technology, that is, automatic heavy load technology. It provides a good R & D environment for developers. When you change the servlet and JSP pages, you do not have to restart the application server. However, this technology is a huge waste to system resources in the product operation phase, because it will bring a great burden to the JSP Engine Class Loader (classloader. Therefore, disabling the auto-Reload function can greatly improve the system performance.

Method 3: Do not abuse HttpSession

In many applications, our programs need to maintain the client state so that pages can communicate with each other. Unfortunately, because HTTP is inherently stateless, the client state cannot be saved. Therefore, common application servers provide sessions to store the customer's status. In the JSP application server, the session function is implemented through the HttpSession object, but at the same time, it brings a lot of burden to the system. Because every time you obtain or update a session, the system will perform time-consuming serialization operations on it. You can improve the system performance by processing HttpSession in the following ways.

If not, disable the default configuration of HttpSession on the JSP page. If you do not specify it explicitly, an HttpSession is created for each JSP page by default. If you do not need to use session in your JSP, you can use the following JSP page indicator to disable it:

<%@ page session="false"%>

Do not store enlarged data pairs in HttpSession: If you Store enlarged data pairs in HttpSession, the application server will serialize them whenever you read and write them, this increases the additional burden on the system. The larger the data pair you store in HttpSession, the faster the system performance is.

If you do not need an HttpSession, release it as soon as possible: when you no longer need a session, you can release it by calling the HttpSession. invalidate () method. Set the session Timeout time to a shorter value: On the JSP application server, there is a default session timeout time. When the customer does not perform any operation after this time, the system will automatically release the relevant session from the memory. The larger the timeout value, the lower the system performance. Therefore, the best way is to keep the value at a lower level.


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.