ServletOutputStream与PrintStream有什么区别,如何使用?

doli 2001-11-12 03:26:43
ServletOutputStream与PrintStream有什么区别,如何使用?
...全文
137 点赞 收藏 1
写回复
1 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
skyyoung 2001-11-12
http://www.precisejava.com/javaperf/j2ee/Servlets.htm
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.write(header);

out.flush(); // flush the header

out.write(navbar);

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

// write dynamic data here

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

out.write(footer);

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

synchronized(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 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2001-11-12 03:26
社区公告
暂无公告