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 com.ups.xmlsdk.servlet;"
if the package the HandyShip servlet file is in is: com.ups.xmlsdk.servlet The the location of the class file must be based in the WEB-INF/classes directory and follow the package name. so: WEB-INF/classes/com/ups/xmlsdk/servlet/HandyShip.class 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. Bill ------------------ author of:
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 com.ups.xmlsdk.servlet; 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 <web-app> <servlet> <servlet-name>HandyShip</servlet-name> <servlet-class>HandyShip</servlet-class> </servlet> <servlet-mapping> <servlet-name>HandyShip</servlet-name> <url-pattern>/HandyShip</url-pattern> </servlet-mapping> </web-app>
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.
Joined: Aug 18, 2001
Bob, Just wanted to thank you for the help. The servelet is running fine now. Thanks again, Preston