Wednesday, August 12, 2009

How can i reduce the number of writes to Client from Servlet ?

It is always recommended to use StringBuffer instead of String even for Concatenating a group of Strings. Avoid writing every small string to client. Instead store all the strings into a StringBuffer and after sufficient data is available, send this data to Browser.

Here is some sample code,

PrintWriter out =res.getWriter();
StringBuffer sb = new StringBuffer();

//concatenate all text to StringBuffer
sb.append("............. ")

res.setContentLength(sb.length());
out.print(sb);

No comments:

Post a Comment