servlet的一些优化措施
-
通过init方法来缓存一些静态数据,诸如数据库连接,预初始化数据,前台加载。
-
用print()方法取代pringln()方法。自己想想就知道了。
-
用ServletOutputStream取代PringWriter。自己想想就知道了。
-
更好的创建PrintWriter对象的设计: ByteArrayOutputStream byteArray = new ByteArrayOutputStream(1024); PrintWriter out = new PrintWriter(byteArray);
-
采用部分输出的方式向客户端输出数据。以减少时间开销,获得连续加载。 ServletOutputStream out = res.getOutputStream(); //输出头信息数据 out.write(header); out.flush(); //输出图片信息 out.write(gif); out.flush(); //输出动态数据 out.write(data); out.flush(); //输出尾信息 out.write(footer); out.flush();
-
尽量缩小同步代码区间。 synchronized(this){ code 1; code 2; code 3; code 4; code 5 }
code 1; code 2; synchronized(this){ code 3; } code 4; code 5
-
设置合适的响应长度 response.setContentLength(size);
-
合理缓存静态数据与动态数据。 session.set/getAttribute application.set/getAttribute
-
尽量不要用SingleThredModel 使用线程池