In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you how to achieve Servlet and JSP performance optimization, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to understand it!
Does your J2EE application run very slowly? Can they withstand the increasing number of visitors? This paper describes the performance optimization techniques for developing high-performance and flexible Servlet and JSP pages. It means to build as fast as possible and adapt to the growing number of users and their requests. In this article, I will walk you through practical and proven performance tuning techniques that will greatly improve the performance of your servlet and JSP pages, and thus the performance of J2EE. Some of these technologies are used in the development phase, such as the design and coding phase. Another part of the technology is related to configuration.
Technique 1: cache data in the HttpServlet init () method
The server calls the init () method of servlet after the servlet instance is created and before servlet processes any requests. This method is called only once in the life cycle of the servlet. To improve performance, cache static data in init () or perform expensive operations to be done during initialization. For example, a * practice is to use JDBC connection pooling that implements the javax.sql.DataSource interface. DataSource is obtained from the JNDI tree. Every time SQL is called, it is very expensive to use JNDI to find DataSource, and it seriously affects the performance of the application. The init () method of Servlet can be used to get the DataSource and cache it for later reuse:
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 automount of servlet and JSP
Every time you change the Servlet/JSP, you will have to restart the server. Because the automount feature reduces development time, it is considered to be very useful during the development phase. However, it is very expensive at run time; servlet/JSP has poor performance due to unnecessary loading, which increases the burden on the classloader. Similarly, this will cause your application to have strange conflicts because the classes already loaded by some class loader cannot cooperate with the classes loaded by the current class loader. Therefore, in order to get better performance in the running environment, the automount function of servlet/JSP is turned off.
Technology 3: control HttpSession
Many applications require a series of client requests, so they can be related to each other. Because the HTTP protocol is stateless, Web-based applications are responsible for maintaining such a state called session. In order to support applications that must maintain state, Java servlet technology provides API that manages session and allows multiple mechanisms to implement session. The HttpSession object acts as session, but there is a cost to use it. Whenever HttpSession is used and rewritten, it is read by servlet. You can improve performance by using the following techniques:
Do not create a default HttpSession in a JSP page: by default, a JSP page creates a HttpSession. If you do not use HttpSession in JSP pages, to save performance overhead, use the following page instructions to avoid automatically creating HttpSession objects:
< %@ page session="false"%>Don't store large object graphs in HttpSession: if you store data in HttpSession as one large object graph, the application server will have to process the entire HttpSession object at a time. This will force Java serialization and increase computational overhead. Due to the overhead of serialization, the throughput of the system will decrease as the data objects stored in the HttpSession object grow.
Release HttpSession after use: use the HttpSession.invalidate () method to invalidate sesion when HttpSession is no longer in use.
Set timeout value: a servlet engine has a default timeout value. If you don't delete session or keep using session until it times out, the servlet engine will remove session from memory. Due to the overhead of memory and garbage collection, the greater the timeout value of session, the greater its impact on system resilience and performance. Try to set the timeout value of session as low as possible.
Technology 4: using gzip Compression
Compression is the way to delete redundant information and describe your information in as little space as possible. Compressing documents with gzip (GNU zip) can effectively reduce the time it takes to download HTML files. The smaller the amount of information you have, the faster they will be sent. Therefore, if you compress the content generated by your web application, the faster it reaches the user and appears on the user's screen. Not all browsers support gzip compression, but it's easy to check if a browser supports it and send gzip compressed content to the browser. The following code snippet 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 ();}.}
Technology 5: do not use SingleThreadModel
SingleThreadModel guarantees that servlet processes only one request at a time. If an servlet implements this interface, the servlet engine will create a separate servlet instance for each new request, which will incur a lot of overhead. If you need to solve thread safety problems, please use another way to replace this interface. SingleThreadModel is no longer advocated in Servlet 2.4.
Technology 6: using thread pools
The servlet engine creates a separate thread for each request, assigns the thread to the service () method, and then deletes the thread after the service () method finishes execution. By default, the servlet engine may create a new thread for each request. Because the cost of creating and deleting threads is expensive, this default behavior degrades system performance. We can use thread pools to improve performance. According to the expected number of concurrent users, configure a thread pool and set the minimum and * * values for the number of threads in the thread pool and the minimum and * * values for growth. Initially, the servlet engine creates a thread pool with the number of threads equal to the minimum number of threads in the configuration. The servlet engine then assigns a thread in the pool to a request instead of creating a new thread each time, and after completing the operation, the servlet engine puts the thread back into the thread pool. Using thread pools, performance can be significantly improved. If necessary, more threads can be created based on the number of threads and the number of growth.
Technology 7: choose the right inclusion mechanism
In a JSP page, there are two ways to include files: including instructions (
< %@ include file="test.JSP" %>) and include actions (
< JSP:include page="test.JSP" flush="true" />). Include instructions include the contents of a specified file during the compilation phase; for example, when a page is compiled into a servlet. Include actions refer to the inclusion of file contents during the request phase; for example, when a user requests a page. Including instructions is faster than including. Therefore, unless the included files change frequently, the use of include instructions will achieve better performance.
Technique 8: use the appropriate scope in the useBean action
One of the major ways to use JSP pages is to work with JavaBean components. JavaBean usage
< JSP:useBean>Tags can be embedded in JSP pages. The syntax is as follows:
< jsp:useBean id="name" scope="page|request|session|application" class= "package.className" type="typeName"> < /jsp:useBean>The scope property describes the visible range of the bean. The default value of the scope property is page. You should choose the correct scope according to the needs of your application, otherwise it will affect the performance of the application.
For example, if you need an object dedicated to some request, but you set the scope to session, that object will remain in memory after the request ends. It will remain in memory unless you explicitly remove it from memory, invalidate session, or time out session. If you do not select the correct scope properties, the overhead of memory and garbage collection will affect performance. So set the appropriate scope for objects and delete them as soon as they are used up.
These are all the contents of the article "how Servlet and JSP achieve performance optimization". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.