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?
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?
-
2
-
-
-
-
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.
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.
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.
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/...
Cheers,
Naren
-----------------
SCJP
SCDJWS
SCWCD
Cheers,
Naren
(OCEEJBD6, SCWCD5, SCDJWS, SCJP1.4 and Oracle SQL 1Z0-051)
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 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)
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

