aspose file tools*
The moose likes Servlets and the fly likes Best practices for storing servlet library code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Best practices for storing servlet library code" Watch "Best practices for storing servlet library code" New topic
Author

Best practices for storing servlet library code

Rob Bauer
Greenhorn

Joined: Sep 04, 2003
Posts: 2
I'm learning Java by trial by fire. I've just completed my first site (on Tomcat running under W2k Pro) and its based off the dispatcher->controller->template recipe. The first site has several classes I would like to use in site 2 (the dispatcher itself, the database query tools, my templating class, etc). Then there's several classes that I feel I'll need in both sites, but will need to be modified slightly (I have a displaynav class which manages building a site's nav menus - well, right now just site 1's nav menus).
My first thought process was to copy the WEB-INF/classes/* data to site two and rename the packages and directory name (to match the new package name). I wasn't a fan of this idea because now there's two copies of each class floating around. I haven't been able to get any new classes for site 2 to use any of the copied over classes (error was something like cannot reference class - class was being specified with the full package name for site 2).
So my 2nd thought was to put the classes that should be available to all the sites on this server in directory and put that directory in the CLASSPATH. I haven't been able to get this to work.
Is it possible to put some of these classes in one spot and be able to access them from sites that are on the same server, but in different directories? From the looks of it, it appears Java is good at doing this, but I'm not sure how I need to tweak the web.xml, server.xml, and/or the CLASSPATH. Is it possible to have 1 dispatcher saved in one directory and have multiple sites call it?
If anyone has links to any online documentation on this, it would be very helpful. If anyone has a better approach to doing this, I'd be excited to hear it. Thanks!
Rob Bauer
Greenhorn

Joined: Sep 04, 2003
Posts: 2
Well, the source of my original problem was with the CLASSPATH.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61413
    
  67

It's best not to muck with the classpath.
Any common classes that I have (that is, classes that I use in more than one webapp), get put into a jar file which I place in the servlet container's common area. That way they are automatically added to the classpath and shared among all the web apps.
Where to put such shared jars depends upon which servlet container you are using. For Tomcat4 that's $CATALINA_HOME/shared/lib.

hth,
bear


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Expanding on Bear's comment, Application Servers (of which Web containers are one part) are designed to serve multiple applications at the same time. If they could all 'see' the system classpath, they'd be limitted in the code they could run.
For example, if you had an older version of the xerces JAR on your classpath and all applications in the server could use this, you couldn't run applications that relied on a newer version of the xerces code without forcing all applications in the server to upgrade, and some of them may not be compatible with the newer version.
It's a bit of a pickle.
The solution is to limit applications running on the server to the basic Java Classes and those in the common extension. All applications running on the server can share these. It should also be mentioned that the code for the application server itself can see the system classpath, it just hides the other stuff from the applications running under it.
Then we set up two more levels of class visibility (there are actually more, but this is the basic explaination). Classes can be set up as common to all applications on the app server by putting it in the app server common directory. This is the solution Bear gave. Drop a JAR in $CATALINA_HOME/shared/lib and all applications can use it, or drop classes in the $CATALINA_HOME/shared/classes directory.
The other option is to explicitly give each applciation the ability to use the code by dropping a copy in the application web-inf/lib or /classes directory.
The place I'll disagree with Bear is that I never use the application server common directory to share code with all applications. My choice is to explicitly add each piece of code to each application. There is little difference as long as you're careful, but my way takes more time but is less likely to cause unexpected problems.
Dave
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Best practices for storing servlet library code