views:

37

answers:

1

Hi there,

I have a JSP in a string variable in a servlet and I would like to compile it as a JSP myself (on the fly, programatically). Right now what I am doing is saving the string to a file and accessing that file (which generates the resulting servlet) - something like:

// and He created JSP from mere html code
final String jsp = convertToJSP(someHTML);
// create a jsp file with the above
final File jspFile = new File(getServletContext().getRealPath("/"), jspFilename);
// write it to a file FIXME: This must be expensive, the user is waiting
// I DONT WANT TO WRITE THIS TO FILE: GENERATE SERVLET IMMEDIATELY FROM CODE
FileUtils.writeStringToFile(jspFile, jsp, "UTF-8");
// access that jsp
resp.sendRedirect(jspFilename);

This is obviously expensive and requires an IO operation. Any ideas/suggestions on how to do this ?

I understand that I'll have to use/tie down to tomcat jasper (jspc) at some point and I am fine with that (Tomcat is my servlet container).

Many Thanks, JP

+2  A: 

You can take a look at the Jasper2 JSP Engine - this is how Tomcat translates JSPs to servlets.

Bozho
Yes I figured as much out - but I couldn't find any Java code which does it. Its all Ant tasks which call JspC, Compiler, ADTCompiler classes - which themselves are a nightmare to follow. I was wondering if anyone has done this sort of thing before, and if therefore some code, more specific pointers could be supplied.
MalteseUnderdog
look through the API for the Jasper compiler - it might be easy to get started.
Bozho