File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Tomcat and the fly likes servlets in jar Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Products » Tomcat
Bookmark "servlets in jar" Watch "servlets in jar" New topic
Author

servlets in jar

Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1107

I have a common project with lots of servlets. I would love to jar this common project and use this is many other projects.
I have tried just making a normal jar, placing it in web-inf/jars and placing the servlet in the web.xml (in the normal way) but that doesn't seem to work.

Is this possible? and if so how do i do it?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60755
    
  65

It's WEB-INF, not web-inf.

And where did you come up with WEB-INF/jars? Jars should be placed in WEB-INF/lib.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1107

sorry, i put the jar in:
WEB-INF/lib
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

And beyond just not seeming to work, in what way does it actually fail to work?
Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1107


I am using tomcat and the exception i am getting is:

Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
Java-based web applications consist of much more that the code written by a programmer, e.g. Servlet container code. Moreover, these applications , i.e. servlets, need to be "properly" deployed in order to function within a web server. Placing the servlet code in a JAR file and then putting the file in the lib directory of a web application is not sufficient and will not work.
Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1107

Jimmy Clark wrote:Java-based web applications consist of much more that the code written by a programmer, e.g. Servlet container code. Moreover, these applications , i.e. servlets, need to be "properly" deployed in order to function within a web server. Placing the servlet code in a JAR file and then putting the file in the lib directory of a web application is not sufficient and will not work.


exactly the point of my question, how can i reuse my servlets?
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
First, you should analyze the "fuctionality" that is coded in servlet form. It some cases, the "functionality" should never have been place in a "servlet" class. This practice creates uneccessary dependencies on a Java web server and potential obstacles to reusability. In most cases, "functionality" should be coded in simple POJO form and accessed from servlet classes. Here the "functionality" can live in a simple JAR file and can be easliy reused.

As an alternative, enter "web services." Common functionality then is accessed via web service from ANY application including Java servlets.

The key is to identify exactly what you what to reuse and see how you can do this efficiently.

And the last step (caveman approach) is to simply leave your servlet applications deployed in a web server and access them via URL from other applications.

Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1107

Jimmy Clark wrote:First, you should analyze the "fuctionality" that is coded in servlet form. It some cases, the "functionality" should never have been place in a "servlet" class. This practice creates uneccessary dependencies on a Java web server and potential obstacles to reusability. In most cases, "functionality" should be coded in simple POJO form and accessed from servlet classes. Here the "functionality" can live in a simple JAR file and can be easliy reused.

As an alternative, enter "web services." Common functionality then is accessed via web service from ANY application including Java servlets.

The key is to identify exactly what you what to reuse and see how you can do this efficiently.

And the last step (caveman approach) is to simply leave your servlet applications deployed in a web server and access them via URL from other applications.



There is very little functionality in the actual servlet, just getting the parameters stuffing them into a DTO (if required) and calling other functionality and handling the return values. But it seems silly having to copy even that little code (moto never cut and paste, reuse). Your solutions, all sound a little more work than the effort i was hoping to save with reuse.

Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9924
    
158

Wendy Gibbons wrote:I have a common project with lots of servlets. I would love to jar this common project and use this is many other projects.
I have tried just making a normal jar, placing it in web-inf/jars and placing the servlet in the web.xml (in the normal way) but that doesn't seem to work.

Is this possible? and if so how do i do it?


The Servlet spec clearly allows packaging a servlet in a jar within *.war/WEB-INF/lib/. Here's the relevant portion from the spec:

The contents of the WEB-INF directory are:
- The /WEB-INF/web.xml deployment descriptor.
- The /WEB-INF/classes/ directory for servlet and utility classes. The classes in this directory must be available to the application class loader.
- The /WEB-INF/lib/*.jar area for Java ARchive files. These files contain servlets, beans, and other utility classes useful to the Web application. The Web application class loader must be able to load classes from any of these archive files.

The Web application class loader must load classes from the WEB-INF/ classes directory first, and then from library JARs in the WEB-INF/lib directory


So it's either some configuration issue that you are running into or it's a genuine ClassNotFoundException - which means that the class(es) isn't really in the jar.

Which exact version of Tomcat is this? Please post the output of

1)

where myapp.war is the war you are deploying
2)


where myjar.jar is the jar (within the WEB-INF/lib) that you think the SearchServlet is in.

[My Blog] [JavaRanch Journal]
Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1107

i'm not using a war, but ant. here is the useful section of the jar file



Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9924
    
158

Which exact version of Tomcat is this?

I think you missed this, from my previous reply

Also have you changed any classpath settings or anything else in Tomcat? Is this same jar file or some other jar containing these same classes available in any other jars within Tomcat?




Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1107

sorry tomcat 5.5.28 (to match the live environment)

and about classpaths: I didn't set tomcat up on this pc, so anything i should be checking for?

I am not sure i understood the question about the jar file, but this is the jarfile i have created with the servlets in i wish to use. This is the only jar file with them in.

I have found a work around that ant is unjaring the servlets directory of the jar to WEB-INF/classes/...
Naren Chivukula
Ranch Hand

Joined: Feb 03, 2004
Posts: 576

Certainly, this problem seems interesting. If you place a class in the form of jar in WEB-INF/lib or as a class itself within WEB-INF/classes. It shouldn't make any difference. The class "must" be picked up in Tomcat server runtime. Unless, if you have the class in question existing somewhere within the server twice or more, which shouldn't cause any problem as long as all of these classes are of same version.

Cheers,
Naren
-----------------
SCJP
SCDJWS
SCWCD


Cheers,
Naren
(OCEEJBD6, SCWCD5, SCDJWS, SCJP1.4 and Oracle SQL 1Z0-051)
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9924
    
158

Wendy Gibbons wrote:

and about classpaths: I didn't set tomcat up on this pc, so anything i should be checking for?

See if anything has been changed in the startup scripts of Tomcat or the catalina.sh (especially the CLASSPATH value). Alternately, you could also try downloading a clean installation of Tomcat and trying out a quick example by packaging the servlets in the WEB-INF/lib/*.jar (locally, I did get it working).

By the way, I'll move this to our Tomcat forum since this looks very much specific to Tomcat installation.

Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
The servlet classes need to be "deployed". Simply putting servlet classes in a JAR and sticking the JAR file in the library directory will not make them accessible or executable from other web applications, i.e. servlets.
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9924
    
158

Jimmy Clark wrote:The servlet classes need to be "deployed". Simply putting servlet classes in a JAR and sticking the JAR file in the library directory will not make them accessible or executable from other web applications, i.e. servlets.


Wendy, in his first post, does say that he is "deploying" them and not just placing those jars in the WEB-INF/lib:

and placing the servlet in the web.xml (in the normal way)
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
I counted at least 24 servlet classes....lol

If you are talking abouting "redeploying" this bunch of servlet code in every web application that will access the code in the servlets... sure this can be done.


Way too many servlet classes in my opinion. However if you are counting lines of code (LOC) and charging by this metric....then go crazy
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: servlets in jar
 
Similar Threads
xbean.jar classpath loader
accessing jar files accross servlets
Jar Hell Resolution
Classpath Setting
Thin Driver not working