Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Extending HttpServlet

 
Kerry Wilson
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64688
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kerry Wilson
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64688
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13058
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic