You often complain JSP pages, response speed is slow? When you thought about customer visits increase, your application can withstand WEB increasing visits? This paper on the adjustment of the JSP and servlet some very practical approach, which would enable your servlet and JSP pages faster response, scalability, and more. Increase the number of users and in the circumstances, the system will be showing a smooth load on a trend. In this paper, I will be through a number of practical examples and configuration approach allows your application's performance unexpectedly improved. Among them, some tuning of the technology is in your programming work to achieve. And some other technology and application server configuration related. In this article, we will describe in detail how the adjustment servlet and JSP pages, to improve your application's overall performance. Before reading this article, assume you have basic knowledge servlet and JSP.
Method 1: servlet in the init () method of data cache
When initialization servlet examples of the application server, the client request for the provision of services before, it will call this the servlet init () method. In a servlet life cycle, the init () method is invoked only once. Through the init () method of caching static data or completion of some need only a one-time, time-consuming operation, we can greatly enhance system performance.
For example, through the init () method in the establishment of a JDBC connection pool is an excellent example, assume that we use the jdbc2.0 DataSource interface to the database connection, in normal circumstances, we need to obtain specific JNDI data source. We can imagine in a specific application, if each request to the implementation of a SQL query JNDI, that system performance will be reduced dramatically. Solution is the following code, DataSource through cache, making the next SQL calls can still continue to use it:
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: Ban Heavy automatic servlet and JSP (auto-reloading)
Servlet / JSP provides a practical technology that automatically override technology, which provides developers with a good development environment, when you change after the servlet and JSP pages without having to restart the application server. However, such technology in the product operational phase of the system resources is a great loss, because it would like JSP engine loading (classloader) a tremendous burden. Therefore closed automatically override function on the system performance enhancement is a great help.
Method 3: Do not abuse HttpSession
In many applications, we need to maintain the procedures client state, to between pages can be interconnected. But unfortunately, because HTTP is inherently a state, thus not be able to save the client state. So the general application server has been providing customers session to preserve the state. In JSP application server, through the HttpSession as to achieve the function of the session, but in a convenient, it also brought to the system no small burden. Because whenever you get or update session, the system, it should be the sequence of time-consuming operation. You can use the following types of HttpSession way to improve the performance of the system:
If there is no need, it should be shut down in the HttpSession JSP pages by default: If you do not clearly specified, the default will each JSP pages to create a HttpSession. If you do not use the JSP in the session, then it can be passed the following JSP pages indicator to prohibit it:
<% @ Page session = "false"%>
Not stored in the HttpSession data like: If you stored in the HttpSession in the data as if it whenever and write, the application server will be its sequence, thereby increasing the system's additional burden. You are stored in the HttpSession as greater data, which fell on the performance of the system faster.
When you do not HttpSession, to release it as soon as possible: When you no longer need session, you can call HttpSession.invalidate () method to release it.
As far as possible the overtime session for a shorter time: JSP application server, there is a default session of overtime hours. When a customer at this time have not been any operation, the system will be related to the session automatically released from memory. TIMEOUT located in the greater system performance will be lower and therefore the best way is to make it possible to maintain the value of a lower level.
Method 4: Pages will be compressed output
Redundant data compression solution is a good way, especially in the less developed network bandwidth today. Some browsers support gzip (GNU zip) to the HTML file compression, this method can dramatically reduce the download time HTML document. Therefore, if you will servlet or JSP pages in HTML pages generated by the compression, then users will feel page views speed very quickly. But, unfortunately, not all browsers support gzip compression, but you can be in your program check the browser whether or not to support it. Below is the method of achieving such a code fragment:
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" and "gzip");
Out = new GZIPOutputStream (request.getOutputStream ());
)
Else if (encoding! = Null & encoding.indexOf ( "compress")! = -1)
(
Request.setHeader ( "Content-Encoding" and "compress");
Out = new ZIPOutputStream (request.getOutputStream ());
)
Else
(
Out = request.getOutputStream ();
)
...
...
)
Methods 5: Use thread pool
Default application server for each different client requests to create a thread processing, as well as their assigned service () method, when the service () method call upon the completion with the corresponding thread also will be withdrawn. The thread will be created and withdrawal of certain consuming system resources, such default mode reduces the performance of the system. But fortunately, we can create a thread pool to change this situation. In addition, we have set up for this thread pool thread a minimum and a maximum number of threads. In the application server startup, it will create the minimum number equivalent to the number of threads a thread pool, when customers request, the corresponding removed from the pool from a thread for processing, after the completion of the processing, then re-thread Add to the pool. If enough to thread pool, the system will automatically increase the number of thread pool, but can not exceed the maximum total number of threads. Through the use of the thread pool, when the sharp increase in client requests, the load on the system will show the rise in a smooth curve, thereby enhancing the system's scalability.
Methods 6: Choosing the right page contains mechanisms
In JSP There are two methods can be used to include another page: 1, include the use of indicator (<% @ includee file = "test.jsp"% "). 2, the use of jsp indicator (<jsp: includee page = "test.jsp" flush = "true" / "). I found that in practice, if you use the first method, you can make a higher system performance.
Method 7: the right to determine the life cycle of javabean
The JSP is a powerful place to javabean support. JSP pages through the use of <jsp: useBean> tags, can be directly inserted into a javabean JSP page. It is used as follows:
<Jsp: useBean id = "name" scope = "page | request | session | application" class =
"Package.className" type = "typeName">
</ Jsp: useBean>
That the scope of this bean attributes of the life cycle. For the life cycle of the default page. If you do not have the right to choose the life cycle of bean, it will affect the performance of the system.
For example, if you just want to in a request to use a certain bean, but you however that the life cycle of bean set a session, then when the request after the end of this bean will remain in the memory, unless the session overtime or user closes the browser. This will cost a certain memory, and unnecessary increase in the JVM garbage collector workload. Therefore the correct settings for the bean life cycle, and in the bean as soon as possible after the end of the mission to clear them, will use an improved system performance.
Other useful method
In the string connecting operation as far as possible not to use the "+" operator: java programming, we often use "+" operator to connect several string, but you may have never thought it would actually system performance impact? The string constants, JVM will have some temporary like. You use the "+" more like temporary generated the more this will be to bring some impact on the system performance. The solution is used to replace StringBuffer like "+" operator.
Avoid using System.out.println () method: As System.out.println () is a synchronous call, that is, calling it, the disk I / O operations must wait for the completion of it, so we should try to avoid it call. But we in the debugging process it is an indispensable handy tool, in order to solve this conflict, I suggest that you use the best tool Log4j (http://Jakarta.apache.org), it can easily debug, and not have System.out.println () this way.
PrintWriter ServletOutputStream and the balance: the use of PrintWriter may bring about some minor expenses, because it would all of the initial outputs are converted to the character stream to the output, so if you use it as a page output, the system have to pay a conversion process . And the use of ServletOutputStream output pages as if a problem does not exist, but it is a binary output. Therefore, in practical application, we must weigh the pros and cons of both.
Aggregate
The purpose of this paper is passed on some servlet and JSP tuning technology to greatly improve your application's performance, and therefore improve the entire J2EE application performance. Through these tuning technology, you can find is not some kind of technology platform (such as J2EE and. NET dispute) determine your application's performance, the important thing is that you have to have a platform for more in-depth understanding of this You can basically on their applications to do an optimization! |