When the application server servlet instance initialization, the client requests for the provision of services, it will call the servlet’s init () method. In a servlet’s life cycle, init () method is called only once. Through the init () method in a number of static data cache or the completion of the implementation required only once, time-consuming operation, we can greatly improve system performance.
For example, in the init () method to establish a JDBC connection pool is an excellent example, suppose we are using the DataSource interface jdbc2.0 to obtain the database connection, in normal circumstances, we need to obtain specific JNDI data source. We can imagine at a specific application, if every time a request should be the implementation of a SQL query JNDI, then it will be a sharp drop in system performance. Solution is the following code, which through the cache DataSource, made when the next SQL call can continue to use it:
The following is a quoted fragment:
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 heavy automatic (auto-reloading)
Servlet / JSP provides a useful technology, that is, heavy-duty automatic technology, it provides developers with a good development environment, when you change the servlet and JSP page without having to restart the application server. However, the technology products at the operational phase of the system resources is a great loss, because it will JSP engine classloader (classloader) brought about a heavy burden. So turn off automatically overloaded functions enhance the performance of the system is a great help.
Method 3: Do not abuse HttpSession
In many applications, the procedures required to maintain our client’s status in order to contact each other between pages. But unfortunately, because HTTP has the status of non-natural, so you can not save the state of the client. Thus the general application server provides a session to save the status of the client. In the JSP application server, through the HttpSession like to realize the function of session, but at the same time at a convenient, it also brought to the system not a small burden. Because when you obtain or update the session, the system should it time-consuming sequence of operations. HttpSession you through the following types of treatment methods to improve performance of the system.
If there is no need, you should turn off JSP page in the default setting for HttpSession. If you is not specified, then each will be the default JSP page to create a HttpSession. If you do not need to use the JSP in the session, it can be a JSP page through the following indicator to ban it:
The following is a quoted fragment:
<% @ Page session = “false”%>
Not to the HttpSession in a large data storage such as: If you stored in the HttpSession large data like so, when it read and write, the application server will be serialized, thus increasing the system’s extra burden . You stored in the HttpSession as the greater of the data, then the performance of the system the faster the decline.
When you do not need HttpSession when to release it as soon as possible: When you no longer need to session, you can call HttpSession.invalidate () method to release it. Try to be the session timeout set too short point: in JSP application server, there is a default timeout of the session. When the client at the time did not conduct any operation, the system will be related to the session automatically released from memory. Set a timeout greater the performance of the system will be lower, so the best way is to try to maintain the value of making it at a lower level.
4 Ways: The page output compression
Compressed data redundancy to solve a good way to network bandwidth, particularly in less developed today. Some browsers support gzip (GNU zip) to carry out to compress HTML files, this method can dramatically reduce the download time of HTML documents. Therefore, if you will servlet or JSP page to generate the HTML pages are compressed, then the user will feel that the speed of page views will be very quick. Unfortunately, not all browsers support gzip compression, but you can program your browser to check whether the clients support it. The following about this method is the implementation of a code fragment:
The following is a quoted 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”, “gzip”);
out = new GZIPOutputStream (request.getOutputStream ());
)
else if (encoding! = null & & encoding.indexOf ( “comdivss”)! = -1) (
request.setHeader ( “Content-Encoding”, “comdivss”);
out = new ZIPOutputStream (request.getOutputStream ());
) else (
out = request.getOutputStream ();
)
…
…
)
Method 5: Use the Thread Pool
Default application server for each different client requests to create a thread to deal with, and for their assigned service () method, when the service () method call is completed, the corresponding thread also withdrawn. Withdrawn because of the creation and thread would be a certain degree of system resources, which reduces the default mode of performance of the system. But fortunately we can create a thread pool in order to change this situation.
In addition, we have for this thread pool to set up a minimum number of threads and a maximum number of threads. Application server at startup, it creates the minimum number of threads equal quantity of a thread pool, when the client has requested, accordingly removed from the pool from one thread to handle, when the deal is completed, then re-threaded Add to the pool. If the thread pool and not enough, the system will automatically increase the number of thread pool, but the total should not exceed the maximum number of threads. By using the thread pool, when a sharp increase in client requests, the system will be presented by the load increase in smooth curves, so as to enhance the scalability of the system.
Method 6: Choose the right page contains a mechanism
There are two in the JSP method can be used to include another page:
1, include the use of indicator
The following is a quoted fragment:
<% @ Includee file = “test.jsp”%>
2, the use of indicator jsp
The following is a quoted fragment:
<jsp:includee Page=”test.jsp” flush=”true”/>
Found in practice, if you use the first method, you can enable higher system performance.
Method 7: javabean right to determine the life cycle of
A JSP is a great place javabean support. JSP page through the use of jsp: useBean tag, javabean can be directly inserted into a JSP page. Its use as follows:
The following is a quoted fragment:
<Jsp: useBean id = “name” scope = “page | request | session | application”
class = “package.className” type = “typeName”>
</ Jsp: useBean>
One of scope of the bean property pointed out that the life cycle. The life cycle of default for the 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 use in a request for a bean, but you, however, the life cycle of the bean has become a setting session, and that when the end of this request, the bean will be retained in memory, unless the session timeout or user close the browser. This will cost a certain degree of memory, and an increase of unnecessary garbage collector JVM workload. Therefore the right settings for the bean life cycle, and the bean at the end of the mission of cleaning up after them as soon as possible, the use of system performance will improve there is a.
Some other useful
1, in the connection string operations try not to use the “+” operator: at java programming, we usually use the “+” operator to connect to some string, but you may have never thought it could be impact on system performance, right? because of the string is constant, the JVM will produce some temporary like. You use the “+” more like provisional generate many more, this will give some impact on system performance. The solution is to use StringBuffer instead of as “+” operator.
2, avoid the use of System.out.println () method: because of System.out.println () is a synchronous call, that call it, the disk I / O operation must wait for the completion of it, so we want to try to avoid call it. However, in our debugger when it is an indispensable instrument to facilitate, in order to resolve this conflict, I suggest that you use Log4j best instrument (http://Jakarta.apache.org), it can easily debug, rather than will have System.out.println () this method.
3, ServletOutputStream trade-off with the PrintWriter: PrintWriter use may lead to some minor expenses, because it will output all of the original characters are converted to flow to the output, so if you are using it to output as a page, the system have to pay a conversion process . ServletOutputStream the use of output as the page does not exist, then a problem, but it is for the output binary. In practice, therefore want to weigh the pros and cons of both.
Summary
The purpose of this article is through a number of servlet and JSP tuning technology to greatly improve your application’s performance, and thus enhance the performance of the entire J2EE application. Tuning Through these techniques, you can see that is not a technology platform (such as J2EE and. NET dispute) you decide the performance of applications, it is important that the platform you want to have a more thorough understanding of this Fundamentally you can own up to do an optimized application.




1 user commented in " JSP page in response to raise the rate of seven tips trick "
Follow-up comment rss or Leave a TrackbackHi, nice post. I have been pondering this topic,so thanks for posting. I will definitely be coming back to your blog. Keep up the good posts
Leave A Reply