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 servlet's init () method
When the application server initializes the servlet instance, it is called for the client to request the service providing service, it will call this servlet's init () method. 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 enhance the performance of the system through the following ways to HTTPSession: • If there is no need, you should close the default settings for HttpSession in the JSP page: If you don't have clearly specified, each JSP page will be default. Create an HttpSession. 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 serialize it, thereby increasing the additional system burden. 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 (