I'm generating page content like:
// index.jsp
<%
List<Horse> horses = database.getHorses();
for (Horse it : horses) {
%>
<div><%= it.getName() %></div>
<%
}
%>
is it possible to grab the entire page content at the end of the jsp file, and dump it into a String, like:
String page = this.getPrintWriter().toString();
I'm just curious if this is possible. I might try caching the page as a String, but would need to rewrite my page generation to build everything in one StringBuilder like:
StringBuilder sb = new StringBuilder();
sb.append("<div>"); sb.append(it.getName()); sb.append("</div>");
...
<%= sb.toString() %>
String cached = sb.toString();
Thanks
I would create a HttpServlet which just retrieves page content from a real page (any kind of page, also JSP). Then cache it.
Next time the user makes the same request it would try to get the data from memcache and if present, retrieve only the mem cache content. Of course you have to check parameters, user id, e.t.c. which would change the content on cached page.
This way you could also have a full control of cache size (you can set cache expiration during PUT, limit cached page count e.t.c.)
BTW.. the answer with session data storage won't work!!. Session data is persisted only on single server. As appengine is running on some seriously large cluster (many-many machines), it won't ensure that two same request, from the same client would be served by the same machine - therefore the session data won't be accessible!
Learned this the hard way when i tried to do progress bars ;)
EDIT Seems like sessions are working now with appengine and are shared across all servers (as google states they are using memcache internaly)
Since it's user-specific data, you can't use GAE's memcache for this. It's an application wide cache. I'd just store it in the session scope.
That said, try to avoid using scriptlets as much as possible. The above can perfectly be done in a Servlet class and you can display them using JSTL
c:forEach
as follows:Capturing generated page content can't be done by simply calling
PrintWriter#toString()
. TheStringBuilder
idea is funny, but it's really not worth it. Caching dynamic content has very little benefits for the purposes as you mention.Best what you in this case can do with regard to page caching is just to leverage the webbrowser's default task to cache GET requests.
See also