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 (
The above is the original, this blog respects the original