title: JSP: Using a delegate for 'out' ( jspWriter) with jsp includes to change the behaviour of Expressions
body: <p>Say, there is a class <code>MyJSPWriter</code> which extends <code>JspWriter</code> and implemented all the abstract methods.. And <code>print(String )</code> is modified to add some special behavior, so that all the expression that are strings would be treated differently ( May be I could use this for some special encoding or something like that-- this is a simplified example):</p>
  
  <pre><code>package com.myproject.base;
  
  import java.io.IOException;
  import javax.servlet.jsp.JspWriter;
  
  public class MyJSPWriter extends JspWriter{
  
      JspWriter out = null;
  
      public MyJSPWriter(JspWriter out) {
          super(0, true);
          this.out = out;
      }
  
  
      @Override
      public String toString() {
          return out.toString();
      }
  
      @Override
      public void clear() throws IOException {
          out.clear();
      }
  
      @Override
      public void clearBuffer() throws IOException {
          out.clearBuffer();
      }
  
      @Override
      public void close() throws IOException {
          out.close();
      }
  
      @Override
      public void flush() throws IOException {
          out.flush();
      }
  
      @Override
      public int getRemaining() {
          return out.getRemaining();
      }
  
      @Override
      public void newLine() throws IOException {
          out.newLine();
      }
  
      @Override
      public void print(boolean b) throws IOException {
          out.print(b);
      }
  
      @Override
      public void print(char c) throws IOException {
          out.print(c);
      }
  
      @Override
      public void print(int i) throws IOException {
          out.print(i);
      }
  
      @Override
      public void print(long l) throws IOException {
          out.print(l);
      }
  
      @Override
      public void print(float f) throws IOException {
          out.print(f);
      }
  
      @Override
      public void print(double d) throws IOException {
          out.print(d);
      }
  
      @Override
      public void print(char[] s) throws IOException {
          out.print(s);
      }
  
      @Override
      public void print(String s) throws IOException {
          out.print("Processed String: " + s);
      }
  
      @Override
      public void print(Object obj) throws IOException {
          out.print(obj);
      }
  
      @Override
      public void println() throws IOException {
          out.println();
      }
  
      @Override
      public void println(boolean x) throws IOException {
          out.println(x);
      }
  
      @Override
      public void println(char x) throws IOException {
          out.println(x);
      }
  
      @Override
      public void println(int x) throws IOException {
          out.println(x);
      }
  
      @Override
      public void println(long x) throws IOException {
          out.println(x);
      }
  
      @Override
      public void println(float x) throws IOException {
          out.println(x);
      }
  
      @Override
      public void println(double x) throws IOException {
          out.println(x);
      }
  
      @Override
      public void println(char[] x) throws IOException {
             out.println(x);
      }
  
      @Override
      public void println(String x) throws IOException {
             out.println(x);
      }
  
      @Override
      public void println(Object x) throws IOException {
             out.println(x);
      }
  
      @Override
      public void write(char[] cbuf, int off, int len) throws IOException {
          out.write(cbuf, off, len);
      }
  
  
  }
  </code></pre>
  
  <p>And I have jsp ( say <code>Main.jsp</code>) which looks something like this:</p>
  
  <pre><code>&lt;%@page import="com.myproject.base"%&gt;
  &lt;% out = new MyJSPWriter(out); %&gt;
  
  &lt;%= " Hello World" %&gt;
  </code></pre>
  
  <p>So, in my output, it will appear like </p>
  
  <pre><code>Processed String: Hello World
  </code></pre>
  
  <p>Now, if i have some more jsp:includes, and may more inlcudes in each one of them..
  eg:</p>
  
  <p>Main.jsp</p>
  
  <pre><code>&lt;%@page import="com.myproject.base"%&gt;
  &lt;% out = new MyJSPWriter(out); %&gt;
  
  &lt;%= " Hello World" %&gt;
  &lt;jsp:include page="Sub1.jsp"&gt;&lt;/jsp:include&gt;
  &lt;jsp:include page="Sub2.jsp"&gt;&lt;/jsp:include&gt;
  </code></pre>
  
  <p>Sub1.jsp</p>
  
  <pre><code>&lt;%= " Hello World from sub1.jsp" %&gt;
  </code></pre>
  
  <p>Sub2.jsp</p>
  
  <pre><code>&lt;%= " Hello World from sub2.jsp" %&gt;
  &lt;jsp:include page="Sub3.jsp"&gt;&lt;/jsp:include&gt;
  </code></pre>
  
  <p>and so on... </p>
  
  <p>But all the sub jsp's will have <code>out</code> objects of their own... :-(</p>
  
  <p><strong>How can we bring the same behaviour to all included jsps without adding</strong></p>
  
  <pre><code>&lt;% out = new MyJSPWriter(out); %&gt;
  </code></pre>
  
  <p><strong>in each one of these files ( because i'm trying to use this in a legacy application) ?</strong></p>
  
  <p>Is there any other way i can approach this? </p>
  
  <p><strong>Additional Information:</strong>
  When we look at the generated .java file of the jsp, 
  this is how part of the code look like </p>
  
  <pre><code>public void _jspService(HttpServletRequest request, HttpServletResponse response)
          throws java.io.IOException, ServletException {
  
      PageContext pageContext = null;
      HttpSession session = null;
      ServletContext application = null;
      ServletConfig config = null;
      JspWriter out = null;
      Object page = this;
      JspWriter _jspx_out = null;
      PageContext _jspx_page_context = null;
  
  
      try {
        response.setContentType("text/html");
        pageContext = _jspxFactory.getPageContext(this, request, response,
                  null, true, 8192, true);
        _jspx_page_context = pageContext;
        application = pageContext.getServletContext();
        config = pageContext.getServletConfig();
        session = pageContext.getSession();
        out = pageContext.getOut();
        _jspx_out = out;
        out = new MyJSPWriter(out);
        // and so on writing content ....
  </code></pre>