jsp 页面生成静态页面(调包response)

简介: 过滤器package cn.itcast.web.filter;import java.io.File;import java.io.IOException;import javax.servlet.Filter;import javax.servlet.FilterChain;import javax.servlet.FilterConfig;import javax.



过滤器

package cn.itcast.web.filter;

import java.io.File;
import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class StaticFilter implements Filter {
	private FilterConfig config;
	public void destroy() {}
	public void init(FilterConfig fConfig) throws ServletException {
		this.config = fConfig;
	}
	
	public void doFilter(ServletRequest request, 
			ServletResponse response, FilterChain chain) 
					throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse res = (HttpServletResponse) response;
		/*
		 * 1. 第一次访问时,查找请求对应的html页面是否存在,如果存在重定向到html
		 * 2. 如果不存在,放行!把servlet访问数据库后,输出给客户端的数据保存到一个html文件中
		 *   再重定向到html
		 */
		/*
		 * 一、获取category参数!
		 * category有四种可能:
		 * * null --> null.html
		 * * 1 --> 1.html
		 * * 2 --> 2.html
		 * * 3 --> 3.html
		 * 
		 * html页面的保存路径, htmls目录下
		 * 
		 * 判断对应的html文件是否存在,如果存在,直接重定向!
		 */
		String category = request.getParameter("category");
		String htmlPage = category + ".html";//得到对应的文件名称
		String htmlPath = config.getServletContext().getRealPath("/htmls");//得到文件的存放目录
		File destFile = new File(htmlPath, htmlPage);
		
		if(destFile.exists()) {//如果文件存在
			// 重定向到这个文件
			res.sendRedirect(req.getContextPath() + "/htmls/" + htmlPage);
			return;
		}
		
		/*
		 * 二、如果html文件不存在,我们要生成html
		 * 1. 放行,show.jsp会做出很多的输出,我们要让它别再输出给客户端,而是输出到我们指定的一个html文件中
		 * 完成:
		 * * 调包response,让它的getWriter()与一个html文件绑定,那么show.jsp的输出就到了html文件中
		 */
		StaticResponse sr = new StaticResponse(res, destFile.getAbsolutePath());
		chain.doFilter(request, sr);//放行,即生成了html文件
		
		// 这时页面已经存在,重定向到html文件
		res.sendRedirect(req.getContextPath() + "/htmls/" + htmlPage);
	}
}


调包的response

package cn.itcast.web.filter;

import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;

public class StaticResponse extends HttpServletResponseWrapper {
	private PrintWriter pw;
	
	/**
	 * String path:html文件路径!
	 * @param response
	 * @param path
	 * @throws UnsupportedEncodingException 
	 * @throws FileNotFoundException 
	 */
	public StaticResponse(HttpServletResponse response, String path) 
			throws FileNotFoundException, UnsupportedEncodingException {
		super(response);
		
		// 创建一个与html文件路径在一起的流对象
		pw = new PrintWriter(path, "utf-8");
	}

	public PrintWriter getWriter() {
		// 返回一个与html绑定在一起的printWriter对象
		// jsp会使用它进行输出,这样数据都输出到html文件了。
		return pw;
	}
}


本文出自 “点滴积累” 博客,请务必保留此出处http://tianxingzhe.blog.51cto.com/3390077/1728755

目录
相关文章
|
16天前
|
自然语言处理 Java 数据库连接
掌握JSP页面编程:动态生成Web内容
【4月更文挑战第3天】Java Server Pages (JSP) 是一种用于创建动态Web内容的Java技术,它结合HTML并允许在页面中嵌入Java代码。JSP支持代码片段、表达式语言(EL)和JSTL标签库,简化动态内容生成。当服务器接收到请求时,执行JSP中的Java代码并将结果嵌入HTML返回给客户端。示例展示了如何显示当前日期和时间。JSP可与Servlet、JavaBeans、数据库等结合,用于构建功能丰富的交互式Web应用。
掌握JSP页面编程:动态生成Web内容
|
25天前
银行营业网点管理系统——修改的页面(updateBreaches.jsp)
银行营业网点管理系统——修改的页面(updateBreaches.jsp)
14 2
|
28天前
新闻发布项目——注册页面(reg.jsp)
新闻发布项目——注册页面(reg.jsp)
13 1
|
29天前
|
Java 数据库连接 mybatis
springboot访问jsp页面变成直接下载?
springboot访问jsp页面变成直接下载?
29 0
|
30天前
|
前端开发 Java
java实现动态验证码源代码——jsp页面
java实现动态验证码源代码——jsp页面
11 0
|
30天前
|
JavaScript 前端开发 Java
struts+hibernate+oracle+easyui实现lazyout组件的简单案例——Jsp页面
struts+hibernate+oracle+easyui实现lazyout组件的简单案例——Jsp页面
9 0
|
2月前
|
存储 缓存 Java
JSP页面生命周期详解及优化建议
JSP页面生命周期详解及优化建议
|
25天前
|
前端开发 Java
java通过commons-fileupload实现多张图片的上传(jsp页面)
java通过commons-fileupload实现多张图片的上传(jsp页面)
16 2
|
4月前
|
Java 应用服务中间件 容器
JSP页面请求响应过程中的编码解码
JSP页面请求响应过程中的编码解码
58 0
|
5月前
|
Java
jsp页面中显示word/excel文档方法
jsp页面中显示word/excel文档方法