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


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Extending HttpServlet" Watch "Extending HttpServlet" New topic
Author

Extending HttpServlet

Kerry Wilson
Ranch Hand

Joined: Oct 29, 2003
Posts: 254
I have a development folder that contains all my auxilliary classes. In my application's classes directory I have all the classes that are specific to this single project.

I am extending a class in my /WEB-INF/classes directory tree that extends a class in my development directory that extends HttpServlet the only purpose for the class in the classes directory is to extend the other class since a servlet has to be in the classes directory, it adds no extra functionality.

My problem is this, it seems that the class in the auxilliary folder cannot 'see' the javax.servlet.http package, since I get not able to create HttpServlet instance error when I access it through Tomcat. The classes are already compiled. ( From my local machine with a network drive pointing to the server ) Is there some reason that classes outside the WEB-INF folder would not be able to instantiate even if they are already compiled?


http://www.goodercode.com
SCJP 1.4
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61756
    
  67

the only purpose for the class in the classes directory is to extend the other class since a servlet has to be in the classes directory


Whoa, nelly!

All classes that your app will use need to be in the classpath of the app, whether it be under the classes folder or within a jar, or whatever.

There is never a need to extend a class to "bring it into the classpath" which what it sounds as if you are trying to accomplish.

Is the class that is in your "development directory" (it'd be a lot easier if you told us the names of the classes) in the app classpath?

If not, it needs to be. Then the extending class is completely unnecessary.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Kerry Wilson
Ranch Hand

Joined: Oct 29, 2003
Posts: 254
I thought the container would follow the CLASSPATH environmental variable to locate classes. If not, is there a directory within tomcat that is classes can be put in such that they are available across contexts or is this bad design?
Kerry Wilson
Ranch Hand

Joined: Oct 29, 2003
Posts: 254
Let me set this up a little better:

Dev dir.
c:\classes

Catalina Docbase
c:\classes\webapps\appname

Servlets in c:\classes\webapps\appname\WEB-INF\classes are accessible and can read classes in c:\classes, the problem is when I try to use classes that extend, or otherwise use, javax.servlet.http classes. I have already compiled the classes but I get the following error from Tomcat when I use them in a servlet.

javax.servlet.ServletException: Servlet execution threw an exception

root cause

java.lang.NoClassDefFoundError: javax/servlet/http/HttpServletRequestWrapper

this was trying to access a class in c:/classes directory tree that extends javax.servlet.http.HttpServletRequestWrapper.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61756
    
  67

Tomcat pays no attention whatsoever to the CLASSPATH environment variable.

Classes specific to the web app should be placed under the WEB-INF/classes folder (make sure that all classes belong to a package other than the default).

If the classes are in a jar file, the jar file should be placed in the web app's WEB-INF/lib folder.

Classes to be shared across multiple web app should be jarred up and the jar file placed in $TOMCAT_HOME/shared/lib.

Note that there's a difference between building the web app and deploying the web app. You can build anywhere you want to, but the classes must adhere to the above pattern for Tomcat to construct its run-time classpath.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
Your Tomcat installation has a Class-Loader How-to page that details exactly where Tomcat expects to find class and jar files. (I'm assuming either Tomcat 4 or 5 here )
Bill
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Extending HttpServlet