• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Extending HttpServlet

 
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic