Is your J2EE app running very slow? Can they bear the continuous increase in access? This article tells the development of performance optimization techniques for developing high performance, high-elastic JSP pages and servlets. It means that it is to establish as fast and adaptable users and their requests. In this article, I will lead you to learn and have confirmed performance adjustment technology, which will greatly improve your performance of your servlet and JSP pages, which in turn enhances the performance of J2EE. The part of these technologies is used to development stages, such as design and coding stages. Another part of the technology is related to the configuration. Technology 1: Cache Data Server in the httpservlet init () method will then call the servlet's init () method after the servlet instance is created and the servlet is processed. This method is only called once in the life cycle of the servlet. In order to improve performance, slow storage in init () or complete expensive operation to complete during initialization. For example, a best practice is to use a JDBC connection pool that implements the Javax.Sql.DataSource interface. DataSource is obtained from the JNDI tree. Every time you call SQL, you will use JNDI to find DataSource to be very expensive and seriously affecting the performance of the application. Servlet's init () method may be used to acquire cache DataSource and reuse after: 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;} ...} Technology 2: Disable servlet and JSP automatic load function When each modification After servlet / JSP, you will have to restart the server. This feature is considered to be very useful during the development phase due to the reduction in the development time of the automatic load function. However, it is very expensive during operation; servlet / JSP causes poor performance due to unnecessary loads, increasing the loader's burden. Similarly, this will make your application that cannot be worried about the class that has been loaded with a class loader and the class loaded with the current type loader. Therefore, in the operating environment, the automatic load function of Servlet / JSP is turned off in order to obtain better performance. Technology 3: Control HttpSesions Many applications require a series of client requests, so they can be associated with each other. Since the HTTP protocol is stateless, the Web-based application needs to be responsible for maintaining such a status called Session. To support applications that must be maintained, Java Servlet technology provides an API that manages session and allows multiple mechanisms to implement session. The HttpSession object plays Session, but uses it needs cost.
Whenever HttpSession is used and rewritten, it is read by servlet. You can improve performance by using the following technique: l Do not create a default httpsession in the JSP page: By default, the JSP page creates httpsession. If you don't have HTTPSESSSION in the JSP page, in order to save performance overhead, use the following page instructions to use the following page instructions to automatically create httpsession objects: <% @ page session = "false"%> L Do not store large object maps in httpsession: if You use the data as a large object map in httpsession, the application server will have to handle the entire HTTPSession object each time. This will force Java serialization and increase computing overhead. Due to serialization overhead, with the increase in data objects stored in the httpsession object, the system throughput will decline. l After using it, release httpsession: When not using HttpSession, use the httpsession.invalidate () method to make the SESION fail. l Set the timeout value: A servlet engine has a default timeout value. If you don't delete sessions or always use the session to timeout, the servlet engine will remove the session from memory. Due to the overhead of memory and garbage collection, the larger the timeout value of the session, the greater the impact on the elasticity and performance of the system. Try to set the timeout value of the session as low as possible. Technical 4: Using GZIP compression compression is a process that deletes redundant information, describes your information with as little space as possible. The use of Gzip (GNU ZIP) compressed documents can effectively reduce the time to download the HTML file. The smaller your information, the faster it is sent. So if you compress the content generated by your web application, it reaches the user and display the faster the speed on the user screen. Not any browser supports Gzip compression, but checks if a browser supports it and sends Gzip compressed content to a browser. The code segment below illustrates 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.getputStream ());} else if (Encoding! = Null && Encoding.indexof ("compress")! = -1) {response.setHeader "Content-Encoding", "compress"); out = new ZIPOutputStream (response.getOutputStream ());} else {out = response.getOutputStream ();} ... ...} technology 5: Do not use guaranteed servlet SingleThreadModelSingleThreadModel Only one request is processed at a time. If a servlet implements this interface, the servlet engine will create a separate servlet instance for each new request, which will cause a lot of system overhead. If you need to resolve thread security issues, use other ways to replace this interface. SingleThreadModel is no longer advocated in Servlet 2.4. Technology 6: Use the thread pool Servlet Engine to create a separate thread for each request, assign the thread to the service () method, and then delete the thread after the service () method is executed. By default, the servlet engine may create a new thread for each request. This default behavior reduces the performance of the system due to the cost of creating and deleting threads, so this default behavior reduces the performance of the system. We can use thread pools to improve performance. According to the expected quantity, configure a thread pool, set the minimum and maximum values of the number of threads in the thread pool, and the minimum and maximum growth. At first, the Servlet engine created a thread pool equal to the number of minimum threads in the configuration. The servlet engine then assigns a thread in the pool to a request instead of creating a new thread every time, after completing the operation, the servlet engine puts the thread back to the thread pool. With thread pools, performance can be significantly improved. If necessary, more threads can be created depending on the maximum number of threads and growth.