• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

servlets in jar

 
Bartender
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Wendy L Gibbons
Bartender
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry, i put the jar in:
WEB-INF/lib
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And beyond just not seeming to work, in what way does it actually fail to work?
 
Wendy L Gibbons
Bartender
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

 
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 L Gibbons
Bartender
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 2187
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 L Gibbons
Bartender
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Wendy L Gibbons
Bartender
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i'm not using a war, but ant. here is the useful section of the jar file



 
Jaikiran Pai
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 L Gibbons
Bartender
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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/...
 
Ranch Hand
Posts: 577
Tomcat Server Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Jaikiran Pai
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 2187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 2187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic