0

I want to store the result of a JSP in a string.

For example, I want to be able to call a function like:

String result = ProcessJsp("/jspfile.jsp");

Also, this must be rather efficient. Making a url request to the jsp and then storing it would not work because I am running on the Google App Engine, and I'm not sure how slow that would be and also there is a Quota for the number of url fetches you can make.

How could I do this?

Here are my thoughts on how to do this, though I'm not sure if it would work, and I'm hoping there is something simpler:

Do RequestDispatcher("/jspfile.jsp").include(hreq, hresp), but instead of putting the real HttpResponse object in there, you put your own where the getWriter() method returns something that writes to your String or a memory buffer, etc.

Kyle
  • 21,377
  • 37
  • 113
  • 200
  • More or less a dup: http://stackoverflow.com/questions/1152786/looking-for-a-capturing-impl-of-httpservletresponsewrapper – skaffman Mar 22 '10 at 20:52
  • @Thor It would have to go out to a router and come back right? The reason I am wanting to store it in a String is to speed up my web app by caching parts of my jsps. – Kyle Mar 22 '10 at 20:53
  • @skaffman Is making an HttpResponseWrapper the only way to do it? – Kyle Mar 22 '10 at 20:54
  • @Spines: It's the only way that I've found. I'd love to see an alternative, though, because I'm not happy with it. – skaffman Mar 22 '10 at 21:16
  • 2
    @Spines: it is not slow if the URL points to localhost. – BalusC Mar 22 '10 at 21:42
  • @BalusC +1, True, I'm on the google app engine though and I'm not sure how it would work with that, also there is a quota for the number of url fetches you can do. – Kyle Mar 22 '10 at 22:12
  • Appengine.. Request quota.. Ouch.. Your best bet would be really a `HttpServletResponseWrapper`. I've posted similar solution before here: http://stackoverflow.com/questions/1963158/capture-generated-dynamic-content-at-server-side/1963571#1963571 – BalusC Mar 22 '10 at 22:26

1 Answers1

1

In a comment you state that your goal is caching portions of a JSP page. I'll assume that you're using dynamic includes, rather than client-side requests (eg, Ajax).

If the former, your best solution -- rather than write something yourself -- is to follow the instructions for integrating EHCache into your app-server's stack. Or, if you want to write something yourself, follow the same process but create your own caching filter.

If you want to cache content that will be accessed from the client, then I recommend putting a web-server (such as Apache with mod_cache) in front of your app-server.

Anon
  • 345
  • 2
  • 6