doli 2001-11-12 03:26:43
193 1 打赏 收藏 举报
1 条回复
skyyoung 2001-11-12
Optimization techniques in service() method

When you write a service() method for your Servlet, you can improve performance by using following techniques.

1. Use StringBuffer rather than using + operator when you concatenate multiple strings

2. Use print() method instead of println() method

3. Use ServletOutputStream instead of PrintWriter

4. Initialize the PrintWriter with proper size

5. Flush the data partly

6. Minimize the amount of code in the synchronized block

7. Set the content length

1. Use StringBuffer for concatenation rather than using + operator when you concatenate multiple strings. See Concatenating Strings for detailed information.

2. println() method internally calls print() method and there is no need for new line separation when generating html pages. So, small overhead of calling one more method is reduced if you use print() method directly.

3. There is a small overhead involved in PrintWriter because it is meant for character output stream and it encodes data to bytes, rather you can directly use ServletOutputStream whenever you want to send binary data.

4. The better way of creating PrintWriter is

ByteArrayOutputStream byteArray = new ByteArrayOutputStream(12288);

PrintWriter out = new PrintWriter(byteArray);

This increases the size of buffer in the PrintWriter.

5. If you want to pass the large data to the client from your servlet, user may need to wait till the ServletOutputStream or PrintWriter flushes the data. This happens generally whenever you have number of gifs per page and you want to pass to the client. The better approach is to flush the data partly using flush() method rather than flushing whole data at a time. You can initially flush header, then navigation bar, then body content and finally footer so that the user need not wait for whole data and he sees the header data immediately and so on with navigation bar, body content and footer.

ServletOutputStream out = res.getOutputStream();


out.flush(); // flush the header


out.flush(); // flush the navigation bar

// write dynamic data here

out.flush(); // flush the dynamic data


out.flush(); // finally flush the footer

6. Minimize the amount of code in the synchronized block that is used in the service method whenever you want to the data to be thread safe.

for example, you may write like this


code line1;

code line2;

code line3;

code line4;


but here you may want only to synchronize the data in the code line2 rather all the lines so the better way is

code line1;

synchronized(this){code line2;}

code line3;

code line4;

This reduces the overhead of locking all the code lines.

7. Whenever the client, such as a browser requests a page it establishes socket connection internally with the web server to get the requested content. Client gets the page data not with single connection but with multiple connections depending on the size of the data. if the data is more, such as with multiple gifs then it needs to establish the connection multiple times to get the data. The number of connections established will depend up on default content length of the header, and size of the content to be traversed from web server to the client. By increasing content length, you can reduce traffic and increase the performance. Here is the sample what you can do

response.setContentLength(int contentSize);

This method sets the length of the content that the server can send to client by using Content-Length header.

  • 打赏
  • 举报
Web 开发



Java Web 开发
  • Web 开发社区
2001-11-12 03:26