Seven tricks to enhance JSP applications

xiaoxiao2021-03-05  47

This article tells some of the very practical methods for adjusting JSP and Servlet, which allows your servlet and JSP pages to respond faster, and the scalability is stronger. Are you often complained by customers a JSP page response speed very slow? Do you think that when the number of customer visits increases, can your web application becomes an increasing number of visits? This article tells some of the very practical methods for adjusting JSP and Servlet, which allows your servlet and JSP pages to be faster and more scalability. And when the number of users is increased, the system load will show a long trend. In this article, I will make the performance of your application have sparely improved through some practical examples and configuration methods. Among them, some tuning technologies are implemented in your programming work. Other techniques are related to the configuration of the application server. In this article, we will describe how to improve your application's overall performance by adjusting the Servlet and JSP pages. Before reading this article, assume you have basic servlets and JSP knowledge. Method 1: Cache data in the server INIT () method When the application server initializes the servlet instance, it will call this servlet's init () method before requested the service. In a life cycle of a servlet, the init () method will only be called once. System performance can greatly improve system performance by cache some static data or completes some, time-consuming operations. For example, by establishing a JDBC connection pool in the init () method, we are assumed to obtain database connections with JDBC2.0's DataSource interface. In normal case, we need to achieve specific data through JNDI. source. We can imagine that in a specific application, if each SQL request is executed, the system performance will drop sharply. The solution is the following code, which by caching the DataSource, so that it can continue to use the next time SQL calls: 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 4: Compressing the page output is to resolve data redundancy A good way, especially today's network bandwidth is not developed today. Some browsers support GZIP (GNU ZIP) to compress the HTML file, which can dramatically reduce the download time of the HTML file. Therefore, if you compress the HTML page generated by the Servlet or JSP page, the user will feel that the page browsing speed will be very fast. But unfortunately, not all browsers support Gzip compression, but you can check if the customer's browser supports it through your program.

Below is a code snippet on 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 ("compress")! = -1) {Request.setHeader ("content-encoding", "compress"); out = new zipoutputstream ());} else {out = request. GetOutputStream ();} ...} method 5: Using the thread pool application server defaults to create a thread for each different client, and assign a service () method for them, when service () After the method call is completed, the corresponding thread is also undo. This default mode reduces the system's performance due to creating and revoking threads. But fortunately, we can change this situation by creating a thread pool. Also, we have to set a minimum thread number and a maximum number of threads for this thread pool. When the application server is started, it creates a number equal to a thread pool equal to the minimum thread number. When the customer has a request, the corresponding thread is taken from the pool, and after the processing is complete, then re-put the thread into the pool. in. If the thread in the pool is not enough, the system will automatically increase the number of threads in the pool, but the total amount cannot exceed the maximum number of threads. By using a thread pool, when the client requests have increased sharply, the system's load will present a smooth rising curve, thereby increasing the scalability of the system. Method 6: Selecting the correct page containing mechanism There are two ways in JSP to include another page: 1. Using the include indicator (<% @ include file = "Test.jsp"%>). 2, use the JSP indicator (). In practice, I found that if the first method is used, the system performance can be made higher. Method 7: Correctly determines a powerful place for JavaBean's lifecycle JSP is support for JavaBean. By using the tab in the JSP page, you can insert JavaBean directly into a JSP page.

Its usage is as follows: where the scope property is pointed out This bean life cycle. The default life cycle is Page. If you don't have the correct choice of beans's life cycle, it will affect the performance of the system. For example, if you just want to use a bean in a request, you set this bean lifecycle to sessions, which will still keep in memory when the request is over, unless the session Turn off the browser for timeout or users. This will consume a certain amount of memory, and there is no measurement of the JVM garbage collector. Therefore, the correct life cycle is set for the bean and clean them as soon as the BEAN's mission is completed, and there is an improvement in the system performance. Other useful methods • Try not to use " " operator in a string connection operation: In Java programming, we often use the " " operator to connect several strings, but you may never think of it. It actually affects system performance? Since the string is constant, the JVM will generate some temporary objects. The more " " you are using, the more temporary objects generated, which will also have some impact on system performance. The solution is to replace the " " operator with the StringBuffer object. · Avoid using system.out.println () method: Since system.out.println () is a synchronous call, that is, the disk I / O operation must wait for it, so we must try to avoid it. Call. But when we debug procedures, it is an indispensable tool. In order to solve this contradiction, I suggest you use the log4j tool (http://jakarta.apache.org), which can be easily debugged, not A method such as system.out.println () will be generated. · ServletOutputStream and PrintWriter Weigh: Use PrintWrit may bring some small overhead because it converts all the original output to a character stream, so if you use it as a page output, the system should bear a conversion process. There is no problem with the use of servletoutputstream as the page output, but it is output in binary. Therefore, we must weigh the pros and cons of both in practical applications. Summary This paper is to greatly improve your performance through some tuning techniques of servlet and JSP, and thus enhance the performance of the entire J2EE application. Through these tuning technologies, you can find that it is not a technical platform (such as J2EE and .NET dispute) determines your application performance, it is important to have a more in-depth understanding of this platform, so Can you make an optimization from your application! ...} Method 2: Prohibition of Servlet and JSP Auto-reloading Servlet / JSP provides a practical technology, an automatic overloading technology, which provides developers with a good development environment, when you change The Servlet and JSP pages are not required to restart the application server. However, this technique is a great loss on the system's resources in the product operation phase, as it will bring great burden to the JSP engine's class loader (ClassLoader).

转载请注明原文地址:https://www.9cbs.com/read-31776.html

New Post(0)