JSP in JAR / JAR in WAR - What is the Path to the JSP?

Has anyone bundled a JSP into a JAR and then deployed that JAR in a WAR file?

I'd like to access that jsp in the usual way:

"/relative/path/to/jsp" and compile it at runtime

What would that path look like when a JSP is bundled into a JAR in the:

"/WEB-INF/lib/jar-name-1.0.jar"

Is it possible to map a URL to the jar's internal directory?

- - -

One solution that does occur to me is precompile the jsp and add a "<servlet-mapping>" in the "web.xml" to the generated servlet. However, this would involve many mappings, if I have many JSPs. A number of he conveniences of JSPs are lost. Is there a better technique?

[671 byte] By [john.m.pagea] at [2007-11-26 17:12:36]
# 1

You could use Ant JSPC task to pre-compile the JSPs

http://tomcat.apache.org/tomcat-5.0-doc/jasper/docs/api/org/apache/jasper/JspC.html

The process of precompiling JSPs automatically adds the JSP class file mappings to your web.xml

It creates a huge web.xml depending on the number of JSP files you have.

First it creates Java files corresponding to each JSP then you need to compile the Java files to Class files.

And then with the usual Ant distribution target you can package them into a WAR file.

Is ther any particular reason why you want to create a Jar file with the JSPs? I guess even if you create a JAR file ;with the JSP class files your application should work because, behind the scenes JSPs are like servlets.

appy77a at 2007-7-8 23:40:29 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Re: "Is there any particular reason why you want to create a Jar file with the JSPs?"

Yes, let me back up a bit. I have a web application module, which I would like to reuse in multiple Web Applications. This module has Servlets and JSPs(like a full-blown web application). However, it is not appropriate to include specific deployment information (such as the stuff that generally appears in a Web.xml), because I don't know what the database parameters(for example) will be in a specific deployment.

Say for example, you had a log-in module that handles user logins. The pieces of the puzzle included a Servlet and a JSP that you wanted to reuse across a bunch of web applications you developed. You can't package then up in a WAR because you want to be able to reuse the module in different deployment environments. So how do you package it? It has to be a JAR.

Thanks for the tip on the ANT task. But I don't want to precompile.. A little off-topic but definitely interesting. I'm using MAVEN2 and trying to adhere to the philosophy of "One project, One artifact". Hence the desire to package the module up rather than recopying it each time....

Message was edited by:

john.m.page

john.m.pagea at 2007-7-8 23:40:29 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

In all the Java projects I've worked on so far we had multiple environments:

- Development

- QA

- Staging

- Production

and just like your set-up, the database parameters for each environment was different from the other.

I think to handle the differences in parameters we stored database information in property files, - text files with name=value pairs.

By storing the system parameters in a property file the JSP code and Java code was kept independent. We never found a need to package JSPs into JAR files, they got directly packaged into a WAR when they were ready for production.

I'm not familiar with Maven as I haven't used it, but perhaps it may also have something similar to the JSPC task (if you ever need to use that task).

appy77a at 2007-7-8 23:40:29 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

I've been experimenting with a test applications and have gotten some interesting results:

If I package up a JAR called "jsp-in-jar-1.0.jar" with a JSP called "test.jsp", Then I package the JAR in a second WAR called "jsp-in-jar-in-war-1.0.war", I can access it off the classpath:

URL url = MyServlet.class.getResource( "test.jsp" );

The result of calling url.toString()

is:

/jar:file:/home/dev/servers/jetty/jetty-5.1.11/work/Jetty__8080__jsp-in-jar-in-war-1_0/webapp/WEB-INF/lib/jsp-in-jar-1.0.jar!/test.jsp

Interesting exclamation point(!) after the jar name.

Now If I can convince the container to compile it....

john.m.pagea at 2007-7-8 23:40:29 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5
I have the same need to package up JSPs in a separate jar file.Did you find a solution ?PS: the exclamation point (!) is to show that the file "test.jsp" is picked up inside an archive file ...Message was edited by: ezzahra
ezzahraa at 2007-7-8 23:40:29 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6
Hi!I have the same need too. Have you guys found any solution, plz?Thanks, Stan.
stahya at 2007-7-8 23:40:29 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7
I'm using Maven2 as well and have the exact same problem.Did anyone find a good solution to it?Thanks!
pompiusesa at 2007-7-8 23:40:29 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...