• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can a servlet in Tomcat use Java class outside of Tomcat?

 
richa Shrivastava
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am using Apache Jakarta Tomcat (ver. 4.1.29) to accept an HTTPS call in order to call some Java classes. I know Java , but I only know Tomcat a little. Is it possible for a servlet running inside Tomcat to call Java classes that are NOT inside of Tomcat's directory structure? If so, where would I set up the CLASSPATH for that?

Kindly reply if you have the bandwidth.

Thanks,
Rica
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this is possible.

Suppose there is a class that you are refering in ony of your servlets deployed in tomcat and the jar containing that class is registered with the system classloader (means that has been included in the CALSSPATH ) So for making a object of that class you request your context's classloader.As from JDK1.4 java follows a delegating model for classloading.Your classloader delegates the call to the commons classloader , that in turn delegates the call to the commons classloader and that in turn delegates the call to the system classloader and the class finally gets loaded from the system classloader and its residing somewhere outside the tomcat.

This is how I think classloading happens in tiomcat.
please let me know if this works.
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes you can do it by defining it in your systems classpath or place it under jre/lib or.... But it would be the worst thing to do. Better place that class file under your "your-webapp/WEB-INF/classes" or place it under "your-web-app/lib" as jar may be. It would make your application independent to any of the external resources.
[ September 21, 2006: Message edited by: Adeel Ansari ]
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Adeel Ansari.

I agree with what you stated.Just wanted to clarify that whatever I have mentioned is correct or there is something wrong.I have never tried this myself.Just read it from tomcat classloader how to.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tomcat ignores your system classpath.
Servlet containers look for classes in a pre-determined set of directories.

If you want to use classes outside of these directories, you would need to create your own classloader.

I haven't tried (and don't think it would be a good idea) but you may be able to load classes in the JAVA_HOME/lib directory from a webapp running within Tomcat.

Why do you want to do this?
Let us know and we may be able to suggest a better alternative.
[ September 21, 2006: Message edited by: Ben Souther ]
 
richa Shrivastava
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thanks for your fast response. That definitely helped me, but like you suggested I'll pass you the exact situation that I have. The Java "application" in question is a tool that accesses one of the clients legacy systems for some information. I am trying to solve 2 problems:

1. This Java application is shared by the servlet in Tomcat and a completely separate client/server application that does not go through the web server at all.

2. We have different testing environments which could, during development phases, contain different copies of this Java application.

I was trying to figure out the most efficient way to handle these situations without having 2 identical class libraries for each testing environment (one in Tomcat and one outside of it). The client/server application can get to all of the development and testing environments, so I thought it would be great if the servlet could hit the same classes by somehow dynamically setting the CLASSPATH based on an input parameter.

I could probably have the client/server app call the Java classes in the web server, but those directories are already set up. The only thing I was thinking about was trying Runtime.exec() from the servlet but was trying to see if there is a better way to do this.

Thanks again!

[ September 22, 2006: Message edited by: richa Shrivastava ]
[ September 22, 2006: Message edited by: richa Shrivastava ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ant.
Or whatever build tool you like.

Keep one copy of the source for the shared components but have your build script copy it to each project at build time.

This way, you only have to maintain one copy but still have the benefit having the classes in both places. The servlet spec (since 2.3, I think) really pushes the idea of fully self contained webapps, even if it means duplicating files.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!