Seven tricks to enhance JSP applications

xiaoxiao2021-03-05  19

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 can continue to take advantage of the next SQL call by cache DataSource.

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: Prohibition of Servlet and JSP Auto-Reloading

Servlet / JSP provides a practical technology, an automatic overloading technology that provides developers with a good development environment, when you change the Servlet and JSP pages, without having 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). Therefore, it is a great help to turn off automatic overload function on system performance.

Method 3: Do not abuse httpsession

In many applications, our program needs to keep the client's status so that the page can be interconnected. But unfortunately, because HTTP has natural stateless, it cannot save the state of the client. So the general application servers provide session to save the status of the customer. In the JSP application server, it is the functionality of the session through the HttpSession object, but it also brings a small burden to the system while convenient. Because whenever you get or update the session, the system is to take a time-consuming operation. You can upgrade your system performance by several processing methods of HttpSession:

· If there is no need, you should close the default settings for HttpSession in the JSP page: If you don't make it clearly specified, each JSP page creates an HttpSession default. If you don't need to use Session in your JSP, you can prohibit it by the following JSP page indicator:

<% @ Page session = "false"%> Do not survive in httpsession: If you save an enlarged data object in httpsession, whenever you read and write it, the application server will It is serialized to increase the additional burden of the system. The larger the data stored in the httpsession, the faster the performance of the system will drop.

· When you don't need httpsession, release it as soon as possible: When you no longer need session, you can release it by calling the httpsession.invalidate () method.

• Try to set the session timeout: In the JSP application server, there is a default session timeout. When the customer does not perform any operations after this time, the system will automatically release the relevant session from memory. The larger the timeout, the lower the performance of the system, so the best way is to try to keep its value in a low level.

Method 4: Compressing the page output is a good way to solve data redundancy, especially today that is not developed in network bandwidth. 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. Here is a code snippet for 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.getputStream ());} else if (Encoding! = Null && encoding.indexof ("compress")! = -1) {Request.SetHeader ("Content-Encoding", "compress"); out = new zipoutputstream (Request.getOutputStream ());} else {OUT = Request.getOutputStream ();} ...} Method 5: Use the thread pool application server default to create a thread for each different client, and assign a service () method for them. When the service () method is completed, the corresponding thread is also Cancel. 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 indicates the life cycle of this bean. 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! Welcome to reprint in the case of retaining http://www.javajia.com!

The above is the original, this blog respects the original

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

New Post(0)