for (int i=0; i<100; i ) {
out.println("Data for " i " is " method1() " , " method2() ".<br>");
}
out.close();
用StringBuffer,可发这样:
StringBuffer buf = new StringBuffer();
for (int i=0; i<100; i ) {
buf.append("Data for ").append(i).append(" is ").append(method1());
buf.append(" , ").append(method2()).append(".<br>");
}
response.setContentLength(buf.length());
out.println(buf.toString());
out.close();
我们用StringBuffer实现我个问题减少了对像的创建个数, 所以它比out.println()更有效. 2.利用HTTP Status Codes显示出错信息. 比如我们常用类似下面的处理
public void openFile( String fileName ) {
try {
someOtherMethodToOpenAFile( fileName );
} catch( FileNotFoundException e ) {
out.println( "Sorry... File not found." );
}
}
为了得用status codes,我们可以这样得用HTTP出错信息:
/* 'response' variable is an object of the
HttpServletResponse class.
*/
public void openFile( String fileName ) {
try {
someOtherMethodToOpenAFile( fileName );
} catch( FileNotFoundException e ) {
response.sendError( response.SC_NOT_FOUND );
}
}