2

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.

skaffman
  • 398,947
  • 96
  • 818
  • 769
sinuhepop
  • 20,010
  • 17
  • 72
  • 107

4 Answers4

2

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

Pierre
  • 34,472
  • 31
  • 113
  • 192
  • Velocity is a terrible templating engine - eg exceptions thrown while executing inline statements. It does nothing better than JSP. – mP. Jul 09 '09 at 11:01
  • 1
    ...except the ability to be easily rendered from Java, which is what this question is about. – itsadok Jul 09 '09 at 12:01
1

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
  • 342,105
  • 78
  • 482
  • 720
0

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
  • 8,542
  • 8
  • 45
  • 69
  • 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 or Tiles, I think. Maybe there is a simpler way of achieving that. I would appreciate any help for simplifying my point of view. – sinuhepop Jul 09 '09 at 11:17
  • 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 Jul 09 '09 at 12:02
0

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
  • 36,219
  • 10
  • 45
  • 60