title: How can I wrap my JSP-generated HTML in a JSONP wrapper?
body: <p>I have a JSP page that renders a block of HTML. In normal usage, I load the contents of the JSP using AJAX (specifically, using <code>jQuery.load()</code>) and insert it into the page. Now, I need to be able to load this block of HTML into a different domain, which means the same-source restrictions won't allow me to use "normal" AJAX.</p>
  
  <p>This block gets included in multiple places, some of which will be on the same domain and some of which will be on alternate domains. I would prefer to have it continue to work the way it works currently unless a specific parameter is passed (in all likelihood, that would be the callback function that is passed for JSONP support).</p>
  
  <p>My conceptual solution (thus far) is to output JSONP, with a single KEY/VALUE pair, having  the complete HTML output as the VALUE.</p>
  
  <p>The PROBLEM: I can't find any way to get the buffered output that is waiting to be sent when the JSP finishes rendering and modify it (in this case, to replace actual new lines with "\n". Without that, I get an <code>Unterminated String Literal</code> error when my JSONP function hits the first new line.</p>
  
  <p>EXAMPLE:</p>
  
  <pre><code>&lt;%@page contentType="text/html"%&gt;
  &lt;%
      String callback = request.getParameter("callback");
  %&gt;
  &lt;% if(callback != null) { %&gt;
      // JSONP Function call, defining Key/Value Pair
      // New lines break because JavaScript strings cannot cross lines
      &lt;%= callback %&gt;({"key":'
  &lt;% } %&gt;
  
  &lt;div id="my_content"&gt;
      ...
  &lt;/div&gt;
  
  &lt;% if(callback != null) { %&gt;
      '}) // End of JSONP Function Call
  &lt;% } %&gt;
  </code></pre>