动态页面转化为静态页面

来源:互联网 发布:lol全网络大区 编辑:程序博客网 时间:2024/05/22 03:01

1 通过filter进行对response的替换完成,

2 让jsp输出在指定的文件下的静态页面,

3 最后又重定向到当前项目路径下的,静态页面

向对方的浏览器传输html文件的相关重要代码如下:

有关filter的代码:

private FilterConfig config=null;
public void destroy() {
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
chain.doFilter(request, response);
HttpServletRequest req=(HttpServletRequest) request;//进行类型转化
HttpServletResponse res=(HttpServletResponse) response;//进行类型转化
String category=req.getParameter("category");//得到分类的类是第几个
String classp=category+".html";//完成拼接
String projectPath=config.getServletContext().getRealPath("/htmls");//得到当前的项目包下的htmls的真实路径
File html=new File(projectPath,classp);//封装成一个文件对象
if(html.exists()){//判断htnl是否存在
res.sendRedirect(req.getContextPath()+"/htmls/"+classp);//重定向到制定的html下
return ;
}
//偷换respond对象
responceA responceA=new responceA(res, html.getAbsolutePath());//偷换对象
chain.doFilter(req, responceA);//放行
//重定向到当前项目路径下的资源文件
res.sendRedirect(req.getContextPath()+"/htmls/"+classp);


}
public void init(FilterConfig fConfig) throws ServletException {
this.config=fConfig;//将Filterconfig对象保存到本类之中
}

有关替换类的代码:

private PrintWriter writer=null;


public responceA(HttpServletResponse response,String path) {
super(response);
try {
writer=new PrintWriter(path,"utf-8");//把输出的路径改成了当前磁盘下的路径
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}


}
public PrintWriter getWriter() throws IOException {
 
return writer;//返回磁盘下的输出流对象,jsp会向该对象进行输出
}



0 0
原创粉丝点击