- 浏览: 306137 次
- 性别:
- 来自: 长沙
文章分类
最新评论
-
完善自我:
支持一下!
揭秘IT人才特点:中美印日四国程序员比较 -
悲剧了:
好文,看玩thinking in java的提到的异常处理,看 ...
高效的Java异常处理框架(转) -
yin_bp:
开源框架bbossgroups页支持组件异步方法调用哦,详情请 ...
Spring 3中异步方法调用 -
flyjava:
sun的悲哀
Apache怒了,威胁说要离开JCP
DWR provides a convenient mechanism to execute server side java classes from javascript running in the browser. We use it extensively while developing Dekoh applications. Recently I noticed that when we use DWR, there are many requests to java-script files from the browser. It appeared that some DWR scripts are not being cached by the browser. I fired up
firefox live http headers
and noticed that inded some scripts were being downloaded with every page. We use DWR version 1.1.4. The headers for the repeat downloads looked like: Notice that in the response headers, there is no Last-Modified header. This is the reason why the browser is reloading the script on every page load. This script is a DWR interface script. When you expose java methods using DWR (through a create tag in the dwr-*.xml), DWR creates an interface javascript. This file implements javascript methods which invoke the remote java methods (using DWREngine._execute). The interface script does not change unless the methods exposed in the dwr-*.xml are changed and the application is restarted. Hence the script should have been cacheable. These scripts are generated by DefaultInterfaceProcessor in DWR. To enable caching, we need to change the InterfaceProcessor to add the Last-Modified header in the response and read the If-Modified-Since header in the request. DWR provides a nice mechanism to plugin custom implementation of processors. So instead of chaging the DefaultInterfaceProcessor (which is shipped with DWR), I subclassed the default implementation to support these new headers and plugged it in to our runtime. The code for custom interface processor looks like: To plugin this new Interface Processor, we need to add init parameter for DWRServlet in web.xml. The snippet from web.xml would look like: After adding the custom inerface processor following are the headers for the dwr interface request. Notice that now for the first request, DWR is sending the Last-Modfied header. When we make the second request, the browser sends an “If-Modified-Since” header and DWR now sends a 304 NOT_MODIFIED response. Thus, by plugging in a custom interface processor, we can enable caching of interface java-scripts in DWR.GET /dekohportal/dwr/interface/locationchooser.js HTTP/1.1
Host: localhost
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1) Gecko/20061010 Firefox/2.0
Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Language: en,as;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Cache-Control: max-age=0
HTTP/1.x 200 OK
Server: Pramati Server/5.0SP3 [Servlet/2.4 JSP/2.0]
Date: Tue, 24 Jul 2007 05:29:20 GMT
Transfer-Encoding: chunked
Connection: Keep-Alive
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import uk.ltd.getahead.dwr.impl.DefaultInterfaceProcessor;
import uk.ltd.getahead.dwr.impl.HtmlConstants;
/**
* This class is the InterfaceProcessor for DWRHandler which
* will add the functionality to add las modified header and
* check if modified since header.
* @author <a href="mailto:venkat@pramati.com" mce_href="mailto:venkat@pramati.com">Venkat Gunnu</a>
* @since Jul 19, 2007
*/
public class HttpCachingInterfaceProcessor
extends DefaultInterfaceProcessor
{
//Store the application startup time. This will be the time we will set
//as the Last-Modified time for all the interface scripts
private final long lastUpdatedTime = (System.currentTimeMillis() / 1000) * 1000;
public void handle(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
long ifModifiedSince = req.getDateHeader(HtmlConstants.HEADER_IF_MODIFIED);
if (ifModifiedSince < lastUpdatedTime) {
//If the browser does not have the script in the cache or the cached copy is stale
//set the Last-Modified date header and send the new script file
//Note: If the browser does not have the script in its cache ifModifiedSince will be -1
resp.setDateHeader(HtmlConstants.HEADER_LAST_MODIFIED, lastUpdatedTime);
super.handle(req, resp);
} else {
//If the browser has current version of the file, dont send the script. Just say it has not changed
resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
}
}
}
<servlet><servlet-name>dwr-invoker</servlet-name>
<servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
<init-param>
<param-name>interface</param-name>
<param-value>util.dwr.HttpCachingInterfaceProcessor</param-value>
</init-param>
...
</servlet>
GET /dekohportal/dwr/interface/locationchooser.js HTTP/1.1
Host: localhost
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1) Gecko/20061010 Firefox/2.0
Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Language: en,as;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-aliveHTTP/1.x 200 OK
Server: Pramati Server/5.0SP3 [Servlet/2.4 JSP/2.0]
Date: Tue, 24 Jul 2007 05:49:43 GMT
Last-Modified: Tue, 24 Jul 2007 05:49:33 GMT
Transfer-Encoding: chunked
Connection: Keep-Alive
GET /dekohportal/dwr/interface/locationchooser.js HTTP/1.1
Host: localhost
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1) Gecko/20061010 Firefox/2.0
Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Language: en,as;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
If-Modified-Since: Tue, 24 Jul 2007 05:49:33 GMT
HTTP/1.x 304 NOT_MODIFIED
Server: Pramati Server/5.0SP3 [Servlet/2.4 JSP/2.0]
Date: Tue, 24 Jul 2007 05:50:42 GMT
Content-Length: 0
Connection: Keep-Alive
原文:http://blogs.dekoh.com/dev/2007/07/24/caching-dwr-interface-java-script-files/
发表评论
-
Web编程是函数式编程
2010-11-30 13:44 1034任何一位在两个领域里 ... -
如何开发Web应用程序
2010-11-30 13:41 1070这是一个经常被问到的 ... -
设计Web应用程序时要注意可伸缩性
2010-11-26 09:19 885Max Indelicato是一位软件 ... -
Web 2.0应用客户端性能问题十大根源
2010-11-25 20:19 999Web 2.0应用的推广为用户带来了全新的体验,同时也让开 ... -
HTML压缩(JSP的GZIP实现)
2010-11-24 22:31 4903HTTP 压缩可以大大提高浏览网站的速度,它的 ... -
浏览器加载和渲染html的顺序
2010-11-22 09:45 25141.浏览器加载和渲染html的顺序 1、IE下载的顺序是从上到 ... -
在服务端合并和压缩JavaScript和CSS文件
2010-11-22 09:16 1097Web性能优化最佳实践中最重要的一条是减少HTTP请求 ... -
用 YUI Compressor 压缩和混淆 JS 和 CSS
2010-11-22 09:05 2306一、简介: 目前开发Web应用Javas ... -
HTTP状态一览
2010-11-17 22:43 744在网站建设的实际应用中,容易出现很多小小的失误,就像m ... -
两款HTTP流量分析工具的比较:HTTP Watch,Fiddler
2010-11-17 17:26 0做Web开发或者Web分析经常需要查看Http通讯的过程, ... -
了解CSS的查找匹配原理,让CSS更简洁、高效
2010-11-17 16:49 0用了这么多年的CSS,现在才明白CSS的真正匹配原理,不知 ... -
高性能WEB开发 - flush让页面分块,逐步呈现
2010-11-17 16:47 0在处理比较耗时的请求的时候,我们总希望先让用户先 ... -
WEB高性能开发 - 疯狂的HTML压缩
2010-11-17 16:46 0前言: ... -
该如何加载google-analytics(或其他第三方)的JS
2010-11-17 16:44 0很多网站为了获取用户访问网站的统计信息,使用了go ... -
高性能WEB开发 - 页面呈现、重绘、回流。
2010-11-17 15:57 0页面呈现流程 在讨论页面重绘、回流之前。需要 ... -
高性能WEB开发 - JS、CSS的合并、压缩、缓存管理
2010-11-17 15:54 0本篇文章主要讨论下目前JS,CSS 合并、压缩、缓存 ... -
高性能WEB开发- 减少请求,响应的数据量
2010-11-17 15:49 0上一篇中我们说 ... -
高性能WEB开发 - 为什么要减少请求数,如何减少请求数!
2010-11-17 15:42 0http请求头的数据量 我们先分析下 ... -
高性能web开发 - 如何加载JS,JS应该放在什么位置?
2010-11-17 15:39 0外部JS的阻塞下载 所有浏览器在下载JS的时候, ... -
Web缓存教程
2010-11-17 15:08 1427原文(英文)地址: http://www.mnot.n ...
相关推荐
dwr和简单的文件上传 说明都在附件里面的 index.jsp里面
这几天无聊开发了一个程序,需要AJAX要求也不高就使用了DWR,但是程序始终报错,因为以前用过DWR,所以最后才发现是DTD文件路径已经被改变了,这里留下一个以防程序又找不到DTD
dwr用到的2个js文件
dwr3.0的web.xml的配置文件,该文件是从dwr.jar文件中拷贝出来的
springboot整合dwr实现js调用java方法,页面支持freemarker和jsp
DWR3.0 全部文件 包括文档 很实用哦
利用DWR实现文件上传进度条 利用DWR实现文件上传进度条 利用DWR实现文件上传进度条
dwr ajax dwr项目说对应的dwr.xml文件内容
Strues2-Dwr 带进度条文件上传 已测可用.
此包包含DWR所需的三个JS文件,一个JAR包以及XML文件的配置说明
DWR配置文件详解 DWR配置 DWR配置文件
dwr操作文件上传下载
用数据库缓存池和dwr实现的jsp程序数据库缓存池和dwr实现的jsp程序
dwr配置文件详解 dwr.xml配置文件说明,非常详细的配置手册
dwr.xml文件的配置和入门使用,配置dwr.xml,在jsp中使用
如果您尝试用Extjs的fileuploadfield 调用dwr3的文件上传功能,您会发现,第一次可以调用,但第二次就会出错(不刷新页面的情况下),什么原因,如何解决呢,本文会告诉您
DWR + Servlet 实现文件上传功能 进度条
从java中传递null值到javascript是没有任何危险性的,所以DWR将这个作为默认的converter,所以你自己不用再把这个converter添加到配置文件的部分中去. 基本类型的converter转换int,boolean,double等.当然还包括对应的...
利用这个框架可以让AJAX变得很简单,通过DWR可以在客户端通过JavaScript直接调用服务器的Java方法并返回值给JavaScript,整个过程就好像通过本地客户端调用一样,DWR通过Java类动态生成JavaScript代码。
Spring 集成 Dwr 文件上传和文件下载