File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Running Servlet on Tomcat... Help :) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Running Servlet on Tomcat... Help :)" Watch "Running Servlet on Tomcat... Help :)" New topic

Running Servlet on Tomcat... Help :)

Preston McFarland

Joined: Aug 18, 2001
Posts: 3
I have a servlet that won't run on Tomcat. At one point I did have the servlet running, but in an attempt to clean things up (I had copies of jar and class files everyhwere) it won't run again.
I can compile the servlet just fine in Forte. However, when I run it I get the following error:
java.lang.NoClassDefFoundError: webapps/servlet/Web-inf/classes/HandyShip (wrong name: com/ups/xmlsdk/servlet/HandyShip)
the "webapps/servlet/Web-inf/classes/HandyShip" represents the path to the servlet it self
com/us/xmlskd/servlet is the name of the package in servlet Handyship "package;"

Any ideas would be great.
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13037
if the package the HandyShip servlet file is in is:
The the location of the class file must be based in the WEB-INF/classes directory and follow the package name. so:
Do you have the servlet defined in the web/xml file for the application?
Study the Tomcat examples especially the way servlet names are related to class names and the location of the class files.
author of:
Preston McFarland

Joined: Aug 18, 2001
Posts: 3
William thanks for the response.
I was under the impression that all one need to do was place the class file in the "app name"/web-inf/classes folder and tomcat would auto register it. Then when accessing the page you would use http://localhost:8080/"app name"/"servlet name".
If I understand you correctly, becuase the servlet contains the following package line package; I must create that entire directory structure.
1. Now will Tomcat still automatically regesiter the servelet?
2. What does the URL then look like?
3. How does that affect my web.xml file?
Current web-xml
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17417

The XML does 2 things for you.
1. It allows you to abstract the URL request so that the user request name (URL) doesn't have to be the same as the servlet name.
2. It abstracts the servlet name so that you can change the class name for the servlet without having to change the URL mappings. One example might be if you wanted to swap in a test version of the class.
The difference might be more plain if I correct your code there:

There are 2 places I think where you burned yourself. First, when you put something in a package, the package name must be reflected in the directory structure: classes/com/ups/xmlsdk/servlet/HandyShip.class
Secondly, you have to include the "true" name (that is, including the package name) in the servlet-class element.
I think you can now see why the <servlet> declaration has what might superficially seem to be a redundancy - if you didn't package the servlet, the two names would be the same.
For historical reasons, it's often possible to "auto-register" servlets (unpackaged ones, at least), because the web.xml didn't exist originally and thus a default mapping had to be done. However, this loophole needs to close eventually - it's a security risk.

An IDE is no substitute for an Intelligent Developer.
Preston McFarland

Joined: Aug 18, 2001
Posts: 3
Just wanted to thank you for the help. The servelet is running fine now.
Thanks again,
I agree. Here's the link:
subject: Running Servlet on Tomcat... Help :)
jQuery in Action, 3rd edition