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 (
Its usage is as follows: