以下代码测试环境
windows server 2003,myeclipse5.5GA,tomcat6,运行通过
转自
http://hi.baidu.com/bobylou/blog/item/6045562cedb00aed8b13996c.html
代码1:在不跳转下访问目标jsp。就是利用RequestDispatcher.include(ServletRequest request, ServletResponse response)。该方法把RequestDispatcher指向的目标页面写到response中。
代码1
- public static String getJspOutput(String jsppath, HttpServletRequest request, HttpServletResponse response)
- throws Exception
- {
- WrapperResponse wrapperResponse = new WrapperResponse(response);
- request.getRequestDispatcher(jsppath).include(request, wrapperResponse);
- return wrapperResponse.getContent();
- }
此处一个自定义类WrapperResponse封装了HttpServletResponse,具体请看一下代码
代码2:HttpServletResponse的封装类,继承自HttpServletResponseWrapper。其核心是构建一个OutputStream,且让HttpServletResponse获得的jsp内容输入到这个OutputStream。
java 代码2
- import java.io.ByteArrayOutputStream;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.io.UnsupportedEncodingException;
- import javax.servlet.http.HttpServletResponse;
- import javax.servlet.http.HttpServletResponseWrapper;
- public class WrapperResponse extends HttpServletResponseWrapper {
- private MyPrintWriter tmpWriter;
- private ByteArrayOutputStream output;
- public WrapperResponse(HttpServletResponse httpServletResponse) {
- super(httpServletResponse);
- output = new ByteArrayOutputStream();
- tmpWriter = new MyPrintWriter(output);
- }
- public void finalize() throws Throwable {
- super.finalize();
- output.close();
- tmpWriter.close();
- }
- public String getContent() {
- try {
- tmpWriter.flush();
- String s = tmpWriter.getByteArrayOutputStream().toString("UTF-8");
-
-
- return s;
- } catch (UnsupportedEncodingException e) {
- return "UnsupportedEncoding";
- }
- }
-
-
- public PrintWriter getWriter() throws IOException {
- return tmpWriter;
- }
- public void close() throws IOException {
- tmpWriter.close();
- }
-
-
-
- private static class MyPrintWriter extends PrintWriter {
- ByteArrayOutputStream myOutput;
-
- public MyPrintWriter(ByteArrayOutputStream output) {
- super(output);
- myOutput = output;
- }
- public ByteArrayOutputStream getByteArrayOutputStream() {
- return myOutput;
- }
- }
- }
-
转引结束
利用上面的WrapperResponse 类,写了一个filter,实现在jsp展现之前,获取即将展现的jsp的全部内容
获取这个内容的好处不多说(如果不知道就不用看下去了)
下面的代码利用filter,实现对字符串""的过滤,并转换成"邪教"显示
java 代码
- 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 GavinFilter implements Filter
- {
-
- public void destroy()
- {
-
-
- }
-
- public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain chain) throws IOException, ServletException
- {
-
- HttpServletRequest request = (HttpServletRequest)arg0;
- HttpServletResponse response = (HttpServletResponse)arg1;
- WrapperResponse wrapperResponse = new WrapperResponse(response);
- chain.doFilter(request, wrapperResponse);
- String html = wrapperResponse.getContent();
- response.getWriter().print(html.replaceAll("", "邪教"));
-
- }
-
- public void init(FilterConfig arg0) throws ServletException
- {
-
-
- }
-
- public static String getJspOutput(String jsppath, HttpServletRequest request, HttpServletResponse response) throws Exception
- {
- WrapperResponse wrapperResponse = new WrapperResponse(response);
- request.getRequestDispatcher(jsppath).include(request, wrapperResponse);
- return wrapperResponse.getContent();
- }
-
-
- }
完整代码参看附件(myeclipse)