bianbian coding life

便便代码人生: 关注技术, 翻译文档, 偶尔动动手

[原]利用HttpServletResponseWrapper获取jsp输出内容

Posted by bianbian on 2006-08-29 19:17

本文Tags:

有时候我们需要得到某个jsp的输出内容。比如说在servlet里处理了Module Controller后,需要不同的jsp页面来展示view;又或者得到不同的模板内容(jsp本身可以作为模板语言)并将其缓冲到内存里。
利用 javax.servlet.http.HttpServletResponseWrapper 可以很好地实现我们所需要的功能。下面是使用方法:

  1. ....
  2. public static String getJspOutput(String jsppath, HttpServletRequest request, HttpServletResponse response)
  3.     throws Exception
  4. {
  5.     WrapperResponse wrapperResponse = new WrapperResponse(response);
  6.     request.getRequestDispatcher(jsppath).include(request, wrapperResponse);
  7.     return wrapperResponse.getContent();
  8. }

可以看到,使用起来还是挺方便的。getJspOutput(”/view/header.jsp”, request, response) 就完成了对”/view/header.jsp”的输出内容获取,之后可以进行其他操作。
附WrapperResponse类代码:

  1. import java.io.ByteArrayOutputStream;
  2. import java.io.IOException;
  3. import java.io.PrintWriter;
  4. import java.io.UnsupportedEncodingException;
  5.  
  6. import javax.servlet.ServletOutputStream;
  7. import javax.servlet.http.HttpServletResponse;
  8. import javax.servlet.http.HttpServletResponseWrapper
  9. /**
  10. * @see http://bianbian.sunshow.net/
  11. * @author dannyzhu, bianbian
  12. * @version 1.0
  13. */
  14. public class WrapperResponse extends HttpServletResponseWrapper
  15. {
  16.     private PrintWriter tmpWriter;
  17.     private ByteArrayOutputStream output;
  18.     private ByteArrayServletOutputStream servletOutput;
  19.  
  20.     public WrapperResponse(HttpServletResponse httpServletResponse)
  21.     {
  22.         super(httpServletResponse);
  23.         output = new ByteArrayOutputStream();
  24.         tmpWriter = new PrintWriter(output);
  25.         servletOutput = new ByteArrayServletOutputStream(output);
  26.     }
  27.  
  28.     public void finalize() throws Throwable
  29.     {
  30.         super.finalize();
  31.         servletOutput.close();
  32.         output.close();
  33.         tmpWriter.close();
  34.     }
  35.  
  36.     public String getContent()
  37.     {
  38.         try{
  39.             String s = output.toString("UTF-8");
  40.             reset();
  41.             return s;
  42.         } catch(UnsupportedEncodingException e) {
  43.             return "UnsupportedEncoding";
  44.         }
  45.     }
  46.  
  47.     public PrintWriter getWriter() throws IOException
  48.     {
  49.         // return servletResponse.getWriter();
  50.         return tmpWriter;
  51.     }
  52.  
  53.     public ServletOutputStream getOutputStream() throws IOException
  54.     {
  55.         return servletOutput;
  56.     }
  57.  
  58.     public byte[] toByteArray()
  59.     {
  60.         return output.toByteArray();
  61.     }
  62.  
  63.     public void flushBuffer() throws IOException
  64.     {
  65.         tmpWriter.flush();
  66.         servletOutput.flush();
  67.     }
  68.  
  69.     public void reset()
  70.     {
  71.         output.reset();
  72.     }
  73.  
  74.     public void close() throws IOException
  75.     {
  76.         tmpWriter.close();
  77.     }
  78.  
  79.     private static class ByteArrayServletOutputStream extends ServletOutputStream
  80.     {
  81.  
  82.         ByteArrayOutputStream baos;
  83.  
  84.         public ByteArrayServletOutputStream(ByteArrayOutputStream baos)
  85.         {
  86.             this.baos = baos;
  87.         }
  88.  
  89.         public void write(int i) throws IOException
  90.         {
  91.             baos.write(i);
  92.         }
  93.  
  94.     }
  95. }
标签:

遵守创作共用协议,转载请链接形式注明来自http://bianbian.org 做人要厚道

相关日志

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>

(required)