• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Running Servlet on Tomcat... Help :)

 
Preston McFarland
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;"

Any ideas would be great.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13058
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Preston McFarland
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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>
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18100
51
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Preston McFarland
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bob,
Just wanted to thank you for the help. The servelet is running fine now.
Thanks again,
Preston
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic