Overview In this article, Rahul Chaudhary describes the use of PTT Performance-Tuning Techniques to enhance the performance of Servlets and JSP to enhance your J2EE application. The author assumes that the reader has the foundation of Servlets and JSPS knowledge. Author: Rahul Chaudhary Translator: guipei your J2EE applications run slowly it? Can they meet enough pressure? This article will describe how to develop high-performance applications and JSPs and servlets to use performance adjustment technology (PTT Performance-Tuning Techniques). Using these technologies can construct a more fast and robust system to meet more users or more requests. In this article, I will take you practical practice. How to adjust the performance to improve your servlets and JSP pages, ultimately to enhance your J2EE application performance. One of the technologies used in the development process, that is, adapt to the system design or writing code. Others are and configure related technologies. Adjustment Method 1: Using the httpservlet init () method Cache the data application server When the Servlet starts construct, the init () method of calling the servlet before being processed. Only call only once in the life cycle of the servlet. The init () method is used to improve performance during initialization by cache static data or completing the operation of a large number of resources. For example, it is a best practice by using a JDBC connection pool, when calling a javax.sql.datasource interface. Rely on the DataSource with the JNDI (Java Names and Service Interface) tree. If JNDI finds DataSource is performed at each SQL call, it will seriously affect the application service. The system () method of the servlet will be used to get DataSource and cach it to be used later. 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;} ...} Adjustment Method 2: Prohibition of servlet and JSP automatic overload to save development time, provide automatic overload functionality in the development phase servlet / JSP container, so that you are modifying Servlet / JSP does not have to restart the service. However, under the production environment, it is a large number of overhead because there is no necessary reloading operation, so it has brought very much performance impact. At the same time, it is also possible to bring a variety of strange conflicts when the partial load is loaded. Therefore, the automatic load function can be achieved in J2EE's production environment. Translator Note: This is deeply appreciated by me.
First, under a large-scale J2EE project, in a large pressure test, there is an inexplicable error in development mode, and some request tasks fails, which is because the system conflicts caused by the load. Second, in another large-scale J2EE project actual application, suddenly a large number of users make the system that sufficient to meet the user, after the system, database, application server and so on, have not solved the problem. Finally, I have to rule out the person to solve the site, and finally the problem is actually this reason. This incident only happened the day before yesterday. Adjustment Method 3: Controlling HTTPSESSION Many application services require a range of customer requests, which are interdependent between them. Because the HTTP protocol is stateless, the web-based application must use session technology to maintain the connection. In order to implement the application service, the Java Servlet technology provides an API that performs session management using the HTTPSession object, but while using this feature, the HttpSession object must be read and blind, and the server also bear The response system overhead. You can use the following methods to improve performance. By default, do not create httpSessions objects in the JSP page, the JSP page will automatically create httpSessions by default. If HTTPSESSSSSSSSSITIONS is required in your JSP page, in order to save some performance, use the following page instruction to avoid automatically create HttpSessions objects. <% @ Page session = "false"%> Do not store large objects to httpsession: If you store large object data to httpsession, the application server has to handle the entire HttpSession in each request, which will force the use of Java serials. Work, occupy a large number of system resources. The performance of the application service will be reduced due to the serialization of Java. Release HTTPSESSSIONS objects at the end: Use httpsession.invalidate () methods to eliminate sessions when they don't need it. Set the timeout value of the session: servlet has a default timeout value. If you are inside this time, you have neither removed it, and there is no use (for any service request), the servlet service will automatically destroy it. Because the recovery of memory and garbage, the greater the timeout value, the greater the performance of the server. So, the timeout value of the SESSION is minimized as much as possible. Adjustment Method 4: Using Gzip compression compression is an operation of a versed superfine information so that you can use the smallest spatial storage. The use of Gzip (GNU ZIP) compressed content can significantly reduce the time to download the HTML file. The smaller the information content, the faster the transmission. Therefore, if the content is compressed when generating a web application, it can be transferred faster, and it is displayed on the screen of the user. Since it is not a Gzip compression function to each browser, you must simply check if the browser supports.
The following example code shows how to send compressed content: public void doGet (HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {OutputStream out = null // Check the Accepting-Encoding header from the HTTP request // If the header includes gzip. ., choose gZIP // If the header includes compress, choose ZIP // Otherwise choose no compression String encoding = request.getHeader ( "Accept-encoding");..! if (encoding = null && encoding.indexOf ( "gzip" )! = -1) {response.setHeader ("content-encoding", "gzip"); out = new gzipoutputstream (response.getoutputstream ());} else if (Encoding! = Null && encoding.indexof ("compress" )! = -1) {response.setHeader ("content-encoding", "compress"); out = new zipoutputstream (response.getoutputstream ());} else {out = response.getOutputStream ();} .... Adjustment method 5: Do not use the SingLethReadModel SingleThreadModel interface to ensure that servlet accepts only one request at the same time. If the servlet implements this interface, servlet will create an isolated Servelet instance for each new request, which will cause large system overhead. If you need to handle thread security issues, you can use other methods to replace this method. The SingLethReadModel interface in servlet2.4 has been opposed. Adjustment Method 6: Use the thread pool Servlet Engine to create an isolated thread to each request, assign this thread to the service () method, remove this thread after it is executed. By default, the servlet engine creates new threads for each request. This behavior will cause performance issues because it is required to create and eliminate threads. Performance can be improved by using a thread pool. Configure the maximum, minimum, and increased number of thread pools based on the number of concurrent users desired. When the service is started, the servlet engine creates a thread pool using the minimum number of threads. Then the servlet engine assigns the thread to each request, replacing the original creation of the new thread, returns the thread to the thread pool after the processing is complete. With thread pools, performance will have a significant increase. If necessary, depending on the maximum and number of threads, more threads are created, added to the pool for more requests.
Adjustment Method 7: Selecting the correct included mechanism There are two ways in JSP to use the included file: contain instructions (<% @ include file = "TEST.JSP"%>) and include action (