首页上一页 1 下一页尾页 1 条记录 1/1页
关于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);
}
其中判断是否要生成新的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);
}