views:

396

answers:

5

I would like to compile and interpret JSP in Java, out of Tomcat or any other servlet container. I think I may use the Jasper libraries coming with Tomcat, but I can't find any example of how to do it on the Web.

The minimum I need is a function that returns an String (HTML resulting text) from a compiled JSP and a HttpServletRequest, or something similar.

Thanks.

A: 

Have a look at the source code of ant task that comes with tomcat for pre-compiling jsp's. This should help you.

http://tomcat.apache.org/tomcat-5.5-doc/jasper-howto.html

I'd also strongly question why you want to do this...

Pablojim
Thanks for your answer. I searched a little and this code uses Jasper's JspC class for compiling. Maybe it'll be useful. But actually the real challenge is to interpret the JSP.I need to return an HTML text made of different interpreted JSPs. These JSPs are decided in execution time, so I can't use <jsp:include> or Tiles, I think. Maybe there is a simpler way of achieving that. I would appreciate any help for simplifying my point of view.
Sinuhé
You should add this information to the question above. But It's still a bit unclear: do you actually have code running in a servlet container after all? How are those JSPs "decided"? Describe your actual scenario and requirements, stop trying to abstract it.
Michael Borgwardt
+1  A: 

interpret JSP in Java, out of Tomcat or any other servlet container.

That's simply not possible. Whatever you end up doing is going to be a reasonably complete implementation of a servlet container by the time it works on non-trivial JSPs (what about sessions? Application attributes? Taglibs?) anyway, so why not use one and save yourself the work of implementing it?

What is it that you actually want to achieve?

Michael Borgwardt
You're right. I answered Pablojim, exposing what I want to achieve.
Sinuhé
+2  A: 

This was asked here the other day:

http://stackoverflow.com/questions/1075827/execute-jsp-directly-from-java/

karim79
In fact I wrote that question. But I can't answered now as John Pollancre, because I was not a registered user until today.
Sinuhé
A: 

Uncle Bob's got some blog posts on how to use jasper outside of tomcat.

His conclusion:

The technique described here can be used to test virtually any static web page, or portion thereof outside of a container, and without a webserver running. It is relatively simple to set up; and then very easy to extend. With it, you can spin around the edit/compile/test loop very quickly, and can easily follow the rules of Test Driven Development.

Stephen Denne
Interesting link. Thanks!
Sinuhé
+1  A: 

If you're just looking for a text processor (not necessarily JSPs) , you can also have a look at Apache Velocity

Pierre
Velocity is a terrible templating engine - eg exceptions thrown while executing inline statements. It does nothing better than JSP.
mP
...except the ability to be easily rendered from Java, which is what this question is about.
itsadok