关于Java Web 编程宝典第15章例15.5的index.html更新时间问题
发表在JavaWeb图书答疑 2012-03-06
是否精华
版块置顶:
尊敬的作者们您好,关于Java Web 编程宝典第15章例15.5的HtmlFileFilter类别里的doFilter方法
其中判断是否要生成新的index.html的判断式,如下
if(debug||htmlDate==null||htmlDate.getDate()!=new Date().getDate())
我想请教一下老师,这样子会不会造成「每个毫秒mill-seconds」都会生成新的index.html?
但是我不太确定,我稍微改写了书里的源码,如下,请各位老师们帮我看看这样是不是正确的?

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {

boolean debug=true;
HttpServletRequest httpRequest = (HttpServletRequest)request;
HttpServletResponse httpResponse = (HttpServletResponse)response;
String jspUrl = httpRequest.getRealPath("index.jsp");
System.out.println("jspUrl="+jspUrl);
File jspFile = new File(jspUrl);

File htmlFile = new File(jspFile.getParentFile(),"index.html");

ResponseWrapper responseWrapper = new ResponseWrapper(httpResponse);
chain.doFilter(request, responseWrapper);
responseWrapper.getWriter().flush();

Date htmlDate = null;

if(htmlFile.exists()){
htmlDate = new Date(htmlFile.lastModified());
}else{
htmlFile.createNewFile();
}

boolean needRefresh = false;
Long last_modified = 0L;
Long now = 0L;
if(htmlDate!=null){
last_modified = htmlDate.getTime();
now = new Date().getTime();
needRefresh = (now - last_modified > 10000); //超过10秒钟才更新
System.out.println("last_modified="+last_modified);
System.out.println("now="+now);
if(needRefresh) System.out.println("needRefresh = true");
}
if(htmlDate==null||needRefresh){ //第一次生成 或是 index.html鲜度超过10秒
//if(debug||htmlDate==null||htmlDate.getDate()!=new Date().getDate()){
FileOutputStream fileStream = new FileOutputStream(htmlFile);
DataOutputStream dataStream = new DataOutputStream(fileStream);
DateFormat format = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL);
dataStream.writeChars("");
dataStream.writeUTF("生成时间:"+format.format(new Date()));
dataStream.writeUTF(responseWrapper.getContent());
dataStream.close();
fileStream.close(); //另外我发现书中忘记加上这一行,这似乎会导致无法正确写入资料到index.html
}
//页面已生成,将request转发到index.html
request.getRequestDispatcher("index.html").forward(request, response);
}
分享到:
精彩评论 1
无语_mrkj
学分:3155 LV11
2012-03-09
沙发
if(debug||htmlDate==null||htmlDate.getDate()!=new Date().getDate())
这条语句不会造成「每个毫秒mill-seconds」都会生成新的index.html,因为这个过滤器是在只有请求或者转发时才起作用的。
首页上一页 1 下一页尾页 1 条记录 1/1页
手机同步功能介绍
友情提示:以下图书配套资源能够实现手机同步功能
明日微信公众号
明日之星 明日之星编程特训营
客服热线(每日9:00-17:00)
400 675 1066
mingrisoft@mingrisoft.com
吉林省明日科技有限公司Copyright ©2007-2022,mingrisoft.com, All Rights Reserved长春市北湖科技开发区盛北大街3333号长春北湖科技园项目一期A10号楼四、五层
吉ICP备10002740号-2吉公网安备22010202000132经营性网站备案信息 营业执照