动态页面转化为静态页面

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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值