Sunday, September 5, 2010

Enhance the speed of response JSP page seven Cheats trick

Method 1: In the servlet's init () method for caching data

When the application server after initialization servlet instance for the service before the client requests, it calls the servlet's init () method. In a servlet's life cycle, init () method is called only once. By init () method in the cache of some static data or do some only run once, and time-consuming operation, you can greatly improve system performance.

For example, through the init () method to create a JDBC connection pool is a good example, suppose we is jdbc2.0 the DataSource interface to obtain a database connection, in normal circumstances, we need to obtain specific data JNDI source. We can imagine in a specific application, if the request should be executed once for each SQL query JNDI, then system performance will drop sharply. The solution is the following code, which by caching DataSource, so the next time SQL calls can continue to use it:


The following is quoted fragment: 
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 auto-reload (auto-reloading)

Servlet / JSP technology provides a practical, automatically reload technology, which provides developers with a good development environment, when you change the servlet and JSP pages without having to reboot after the application server. However, this technique in a production stage of system resources is a great loss, because it would JSP engine's class loader (classloader) imposes a heavy burden. So turn off auto-reload feature on the system performance is a great help.

Method 3: Do not abuse HttpSession

In many applications, our procedures need to maintain client state, so the page can be linked between. Unfortunately, because HTTP is inherently stateless, thus can not save the client state. Thus the general application server provides a session to save the client state. In the JSP application server, is achieved through the HttpSession of the session as the functional, but in the convenience, it also brought to the system is not a small burden. Because every time you obtain or update the session, the system are time-consuming to its serialization. You can be on the HttpSession of the following approaches to improve system performance.

If not necessary, it should close the JSP page in the default settings on the HttpSession. If you do not explicitly specified, each JSP page will default to create a HttpSession. If you do not need to use JSP in the session, then the JSP page through the following directive to ban it:

The following is quoted fragment: 


Do not store in the HttpSession in a large data such as: If you stored in the HttpSession as large data on it, whenever it read and write, the application server will be serialized on their, thus increasing the system's additional burden . You store the data in HttpSession as larger, then the system performance to fall faster.

When you do not need HttpSession when to release it as soon as possible: When you no longer need to session, you can call HttpSession.invalidate () method to release it. As the session timeout set too short point: In the JSP application server, a default session timeout. When a customer at this time without any action after the words, the system will auto-related session released from memory. Timeout is set too higher, the lower the performance of the system, so the best way is to try to make it keep the value of a lower level.

No comments:

Post a Comment