[轉]利用HttpServletResponseWrapper獲取jsp輸出內容

轉載: bianbian coding life


有時候咱們須要獲得某個jsp的輸出內容。好比說在servlet裏處理了Module Controller後,須要不一樣的jsp頁面來展現view;又或者獲得不一樣的模板內容(jsp自己能夠做爲模板語言)並將其緩衝到內存裏。
利用 javax.servlet.http.HttpServletResponseWrapper 能夠很好地實現咱們所須要的功能。下面是使用方法:java

  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類代碼:app

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

轉載:bianbian coding lifejsp

相關文章
相關標籤/搜索