Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you need to capture JSP's output as string it's reasonably straightforward (although far from ideal from the design point of view) from <b>within</b> Servlet Container:<br> 1. Extend javax.servlet.HttpServletResponseWrapper and override getWriter() / getOutputStream() methods to provide your own buffered versions (e.g. StringWriter)<br> 2. Invoke "include()" method of RequestDisparcher, wrapping original response in your own.<br> 3. Grab buffer's content. </p> <p> Now if you need to do the same thing <b>outside</b> Servlet Container, you really need to ask yourself "why?". Perhaps you should be using a template engine (FreeMarker / Velocity / StringTemplate / etc...) instead of JSPs? If your JSPs are well-formed XML files and are not using any java code inserts it may be reasonably trivial to convert them to FreeMarker templates (FreeMarker supports custom JSP tag libraries) which would <b>greatly</b> simplify your task. </p> <p>Nevertheless, if it's an absolute hard requirement your most straightforward options are:<br> 1. Run an external Servlet Container and let it handle JSP interpretation. Your program would submit HTTP requests to it and capture the output.<br> 2. Same as above, but you can run embedded Servlet Container (e.g. Jetty). </p> <p>If your JSPs are available at build-time you can precompile them via Jasper as suggested in other answers. </p> <p>I would <b>strongly</b> advice against trying to implement your own servlet container - you'll be walking into a world of hurt.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. COThanks for your advice. I actually don't want to implement my own servlet container. I don't want to compile and process JSP too, but I don't see any alternative for what I look for. I want to create server responses as a JSON like this: {html1: "<pre>1</pre>", html2: "<script>...</script>"} Every html has to be made with JSP, but the JSON can't be there. I used FreeMarker and I don't see how if it can help. If you know some way for doing what I'm looking for, I would appreciate. Thanks.
      singulars
      1. This table or related slice is empty.
    2. COI'm not quite clear on what you're trying to achieve. Are you returning that JSON response from an application running _outside_ a servlet container? Why? I have a hardest time imagining using JSON for inter-app communication. If you could clarify what you're trying to do a bit more I'd be happy to help. The difference FreeMarker would make is that you can run it completely from within your app without having to fake servlet environment. E.g. every 'html' fragment in your response would be produced by a separate FTL template and you'd just call Template.process(model, writer) on each.
      singulars
    3. CONote that this (currently) won't work on Jetty, due to http://jira.codehaus.org/browse/JETTY-1485 . It works fine on Tomcat. There's a more detailed writeup of this approach here: http://technologicaloddity.com/2011/10/04/render-and-capture-the-output-of-a-jsp-as-a-string/
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload