Would it be possible to execute a JSP page and capture its output outside of a web application? Mode specifically, in my case there still exists a usual web application, but it loads JSP pages not from its classpath, but from an arbitrary source. It seems like I cannot simply get RequestDispatcher and point it to a JSP file on disk.
Correct me if I'm wrong, but I think you mean you want to capture the HTML... not the JSP. A JSP is processed (into a JAVA file) on a servlet-engine (Web app Server) and after the HTML is formatted and served up to the requestor via a Web server (not the same as a web APP server). You can't get the RequestDispatcher to work on a straight JSP from disk because it hasn't been processed yet by the web app server.
Now, to capture the output of a JSP (in HTML) should be possible, but I've never done that before. There may be some slick APIs that those out there more knowledgable than I can address, but HTTP is typically done on port 80, so I guess one could read/write to port 80 on a TCPIP socket. There's probably some more things to do on top of that, but at least that's some point to start looking into.
Sorry I can't provide more details, but hell... it's all theory to me at this point.
I think you're better off with a templating engine like velocity. This provides a clean infrastructure for dynamic content that's clearly different from the jsp/servlet stuff that you are asking fore.
That said, I've seen applications that copy jsps into their deployed directory in order for the container to pick them up and translate them. Should you do this, please note that this limits your future options:
- you rely upon your application to be "exploded" - e.g. it can't run directly out of a WAR archive (this might limit your deployment options)
- making jsps editable at runtime might open up security holes if you don't disable scriptlets (also if you do disable, but it'll be somewhat harder...). Disabling scriptlets prohibits real Java code in the jsps, you're limited to tag libraries then.
- You'll need a Java compiler available at runtime, which you might not want to have in production systems - e.g. you cannot precompile your jsps before deployment. Also you pay the usual jsp-translation-penalty at runtime in your productive system.
web.xml configuration for disabling scripting:
<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<scripting-invalid>true</scripting-invalid>
</jsp-property-group>
</jsp-config>
I hope this web.xml snippet went through, the preview didn't show it correctly...
Update: Tried to make xml-snippet display correctly.