JSP: How to allow servlet and applet to share jar files?

J

Jim Cochrane

(I'm using Tomcat on Linux, in case it matters.)

I have a JSP application that uses an applet and a servlet for which
several library files are used by both the servlet and the applet.
Instead of duplicating the shared files - e.g., in different jar files
within the webapps directory, I'd like to allow the servlet and applet to
use the same jar file for the shared classes.

I've found a solution that works, but that does not seem particularly
elegant (as well as not being portable to Windows): In the app.jsp file
that loads the applet:

<applet codebase="classes" archive="applib.jar" code="Main_Applet.class"
... >

Since the servlet expects the applib.jar file to reside under WEB-INF, I
have hard-linked the file such that it occurs twice within the web
application directory:

../WEB-INF/lib/applib.jar
../classes/applib.jar

I first tried a symbolic link and the applet failed to load, undoubtedly
because of the security mechanism.

This solution essentially provides what I want - allowing the servlet and
applet to share the same file rather than accessing two different copies of
the same file (saving disk space and guarding against accidentally using
different versions of applib.jar), but, as I said, it's not very elegant.

I originally tried loading the applib.jar file from its original location:

<applet codebase="WEB-INF/lib" archive="applib.jar" code="Main_Applet.class"
... >

but the applet failed to load - because of the security mechanism, I
assume.

So my question is: Is it possible to come up with a portable solution that
allows the servlet and applet to use the same file in the same location
(i.e., with only one link), where portable means it will work on Linux,
Windows, and UNIX platforms?

A further question: Is it possible to come up with an even cleaner
solution, something like e.g., the applet-specific class files are stored
in applet.jar and the applet loads classes from both applet.jar and from
applib.jar (so that applib.jar does not need to contain applet-specific
class files, irrelevant for the servlet), where applib.jar is again shared
by the servlet and the applet?

[I did some googling and looking through applet and JSP docs to try to find
an answer, but with no luck, except for the above inelegant solution. The
solution is probably out there, if there is one, I just didn't find it.]

(I'm posting this question here because it appears to be the most relevant
java group for this topic. It's odd, though that there is no java
newsgroup specific to web development (unless I missed it?).
[comp.lang.java.programmer seems to cover a lot of very disparate topics -
I suspect I'm not the only one who finds this a little irritating and
inefficient.])


Thanks very much!
 
W

William Brogden

(I'm using Tomcat on Linux, in case it matters.)

I have a JSP application that uses an applet and a servlet for which
several library files are used by both the servlet and the applet.
Instead of duplicating the shared files - e.g., in different jar files
within the webapps directory, I'd like to allow the servlet and applet to
use the same jar file for the shared classes.

I've found a solution that works, but that does not seem particularly
elegant (as well as not being portable to Windows): In the app.jsp file
that loads the applet:

<applet codebase="classes" archive="applib.jar" code="Main_Applet.class"
... >

Since the servlet expects the applib.jar file to reside under WEB-INF, I
have hard-linked the file such that it occurs twice within the web
application directory:

./WEB-INF/lib/applib.jar
./classes/applib.jar

I first tried a symbolic link and the applet failed to load, undoubtedly
because of the security mechanism.

This solution essentially provides what I want - allowing the servlet and
applet to share the same file rather than accessing two different copies
of
the same file (saving disk space and guarding against accidentally using
different versions of applib.jar), but, as I said, it's not very elegant.

I originally tried loading the applib.jar file from its original
location:

<applet codebase="WEB-INF/lib" archive="applib.jar"
code="Main_Applet.class"
... >

but the applet failed to load - because of the security mechanism, I
assume.

So my question is: Is it possible to come up with a portable solution
that
allows the servlet and applet to use the same file in the same location
(i.e., with only one link), where portable means it will work on Linux,
Windows, and UNIX platforms?

A further question: Is it possible to come up with an even cleaner
solution, something like e.g., the applet-specific class files are stored
in applet.jar and the applet loads classes from both applet.jar and from
applib.jar (so that applib.jar does not need to contain applet-specific
class files, irrelevant for the servlet), where applib.jar is again
shared
by the servlet and the applet?

[I did some googling and looking through applet and JSP docs to try to
find
an answer, but with no luck, except for the above inelegant solution.
The
solution is probably out there, if there is one, I just didn't find it.]

(I'm posting this question here because it appears to be the most
relevant
java group for this topic. It's odd, though that there is no java
newsgroup specific to web development (unless I missed it?).
[comp.lang.java.programmer seems to cover a lot of very disparate topics
-
I suspect I'm not the only one who finds this a little irritating and
inefficient.])


Thanks very much!

Have the class files served by a custom servlet instead of by the default
servlet. Just like you would have a generated image served by a servlet.

Bill
 
J

Jim Cochrane

Have the class files served by a custom servlet instead of by the default
servlet. Just like you would have a generated image served by a servlet.

Yes, this sounds like an elegant solution (and perhaps the only one if I'm
to go by the other responses I got), though it will take a bit of effort to
implement.

Thanks.
 
N

Nigel Wade

(I'm using Tomcat on Linux, in case it matters.)

I have a JSP application that uses an applet and a servlet for which
several library files are used by both the servlet and the applet.
Instead of duplicating the shared files - e.g., in different jar files
within the webapps directory, I'd like to allow the servlet and applet to
use the same jar file for the shared classes.

I've found a solution that works, but that does not seem particularly
elegant (as well as not being portable to Windows): In the app.jsp file
that loads the applet:

<applet codebase="classes" archive="applib.jar" code="Main_Applet.class"
... >

Since the servlet expects the applib.jar file to reside under WEB-INF, I
have hard-linked the file such that it occurs twice within the web
application directory:

./WEB-INF/lib/applib.jar
./classes/applib.jar

I first tried a symbolic link and the applet failed to load, undoubtedly
because of the security mechanism.

This solution essentially provides what I want - allowing the servlet and
applet to share the same file rather than accessing two different copies of
the same file (saving disk space and guarding against accidentally using
different versions of applib.jar), but, as I said, it's not very elegant.

I originally tried loading the applib.jar file from its original location:

<applet codebase="WEB-INF/lib" archive="applib.jar" code="Main_Applet.class"
... >

but the applet failed to load - because of the security mechanism, I
assume.

So my question is: Is it possible to come up with a portable solution that
allows the servlet and applet to use the same file in the same location
(i.e., with only one link), where portable means it will work on Linux,
Windows, and UNIX platforms?

A further question: Is it possible to come up with an even cleaner
solution, something like e.g., the applet-specific class files are stored
in applet.jar and the applet loads classes from both applet.jar and from
applib.jar (so that applib.jar does not need to contain applet-specific
class files, irrelevant for the servlet), where applib.jar is again shared
by the servlet and the applet?

[I did some googling and looking through applet and JSP docs to try to find
an answer, but with no luck, except for the above inelegant solution. The
solution is probably out there, if there is one, I just didn't find it.]

(I'm posting this question here because it appears to be the most relevant
java group for this topic. It's odd, though that there is no java
newsgroup specific to web development (unless I missed it?).
[comp.lang.java.programmer seems to cover a lot of very disparate topics -
I suspect I'm not the only one who finds this a little irritating and
inefficient.])


Thanks very much!

The solution I use (on Linux, no idea if it's portable to Windows) is to
put both sets of jars (applet and servlet) in the Tomcat shared folder
<CATALINA_HOME>/shared/lib. I then have a symbolic link from the applet
codebase to the shared folder. The applet codebase is
http://www.server.name:8080/java/lib and a symbolic link
<CATALINA_HOME>webapps/root/java points to <CATALINA_HOME>/shared.

It's a hack, but it works. I've not found a better solution.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top