GWT性能优化
1. 处理长时间的进程:使用IncrementalCommand和Command
参考文档:
http://code.google.com/docreader/#p(google-web-toolkit-doc-1-5)s(google-web-toolkit-doc-1-5)t(DevGuideDeferredCommand)
2. 使用ImageBundle
a) 什么是ImageBundle?
ImageBundle是gwt1.4版本之后中最显著的特性,它使得GWT能将多个图片文件合并成一个图片“条”。这样就可以把它们合并到一个可缓存的HTTP 请求中,而不用通过多个请求来一一取得每个图片文件。使用ImageBundle的另外一个好处就是消除了在图像加载并且调整宽度高度时重新调整布局出现的网页内容“弹动”。
b) 如何使用?
参考文档:
<o:p> </o:p>
3. 在Servlet容器上(例如Tomcat)使用Filter来实现Caching,参考代码如下:
a) Filter示例代码:
public class CacheFilter implements Filter{
private FilterConfig filterConfig;
public void doFilter(ServletRequest request,ServletResponse response,FilterChain filterChain) throws IOException,ServletException{
HttpServletRequest httpRequest = (HttpServletRequest)request;
String requestURI = httpReqest.getRequestURI();
if(requesURI.contains(“.cache.”)){
HttpServletResponse httpResponse = (HttpServletResponse)response;
httpResponse.setHeader(“Cache-Control”,”max-age=31536000”);
}
filterChain.doFilter(request,response);
}
<o:p> </o:p>
public void init(FilterConfig filterConfig) throws ServletException{
this.filterConfig = filterConfig;
}
<o:p> </o:p>
public void destroy(){
this.filterConfig = null;
}
<o:p> </o:p>
}
<o:p> </o:p>
b) 在web.xml文件中配置如下所示:
<filter>
<filter-name>CacheFilter</filter-name>
<filter-class>com.gwtapps.server.CacheFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CacheFilter</filter-nam>
<url-pattern>/*.html</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>CacheFilter</filter-nam>
<url-pattern>/*.png</url-pattern>
</filter-mapping>
<o:p> </o:p>
4. 在Servlet容器上(例如Tomcat)使用Filter来实现Compression,参考代码如下:
a) 在server.xml文件中做如下设置:
<Connector port=”8080” redirectPort=”8443” acceptCount=”20” compression=”on”/>
<o:p> </o:p>
b) Filter示例代码:
public class GZIPFilter implements Filter {
private FilterConfig filterConfig;
<o:p> </o:p>
public void doFilter(ServletRequest request,ServletResponse response,FilterChain filterChain) throws IOException,ServletException{
HttpServletRequest httpRequest = (HttpServletRequest)request;
//skip gzipped files
String requestURI = httpRequest.getRequestURI();
if(!requestURI.endsWith(“.gz”)){
//check for zip support
String acceptEncoding = httpRequest.getHeader(“accept-encoding”);
if(acceptEncoding != null && acceptEncoding.indexOf(“gzip”) != -1){
// forward to .gz file
try{
RequestDispatcher rd=
filterConfig.getServletContext().getRequestDispatcher(requestURI + “.gz”);
rd.forward(request,response);
return;
}
catch(ServletException e){
//continue;
}
}
}
filterChain.doFilter(request,response);
}
<o:p> </o:p>
public void init(FilterConfig filterConfig) throws ServletException{
this.filterConfig = filterConfig;
}
<o:p> </o:p>
public void destroy(){
this.filterConfig = null;
}
}
<o:p> </o:p>
c) 在web.xml文件中配置如下所示:
<filter>
<filter-name>GZIPFilter</filter-name>
<filter-class>com.gwtapps.server.GZIPFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>GZIPFilter</filter-name>
<url-pattern>*.html</ url-pattern >
</filter-mapping>