开发者

Tomcat 5.5 Don't Find My Servlet

I've compiled the source into the class Files, then putted at the folder:

Tomcat 5.5\WEB-INF\ROOT\classes\Files.class

And added this to the web.xml file:

<servlet>
    <servlet-name>Files</servlet-name>
    <servlet-class>Files</servlet-class>
</servlet>

But when I tried to access the URL http://localhost:8080/Files, I got this error from Tomcat: Tomcat 5.5 404 Error http://img251.imageshack.us/img251/5042/tomcat404.png


Update: after adding <servlet-mapping> I'm now getting the following error:

exception

javax.servlet.ServletException: Error allocating a servlet instance
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:837)
org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:640)
org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1287)
java.lang.Thread.run(Unknown Source)

root cause

java.lang.NoClassDefFoundError: IllegalName: /Files
java.lang.ClassLoader.preDefineClass(Unknown Source)
java.lang.ClassLoader.defineClassC开发者_Go百科ond(Unknown Source)
java.lang.ClassLoader.defineClass(Unknown Source)
java.security.SecureClassLoader.defineClass(Unknown Source)
org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:1960)
org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:931)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1405)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1284)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:837)
org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:640)
org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1287)
java.lang.Thread.run(Unknown Source)


root cause
java.lang.NoClassDefFoundError: IllegalName: /Files

This means that the given class definition cannot be found because it has an illegal name /Files. This in turn means that you've changed the <servlet-class> to /Files. This is wrong. You're basically instructing the servletcontainer to declare and instantiate the servlet as follows:

/Files Files = new /Files();

This won't already compile. The complete mapping should look like:

<servlet>
    <servlet-name>instanceName</servlet-name>
    <servlet-class>com.example.ServletClass</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>instanceName</servlet-name>
    <url-pattern>/urlPattern</url-pattern>
</servlet-mapping>

Which is to be interpreted in raw Java code as follows:

com.example.ServletClass instanceName = new com.example.ServletClass();

The <servlet-class> should denote the full qualified classname, including any package. The <servlet-name> should denote the unique instance name. The <url-pattern> should denote the URL pattern for which the servletcontainer should invoke this servlet.


You also need to define a

<servlet-mapping>
  <servlet-name>Files</servlet-name>
  <url-pattern>/Files</url-pattern>
</servlet-mapping>

To match the url pattern to the servlet


You also need the servlet-mapping:

<servlet-mapping>
  <servlet-name>Files</servlet-name>
  <url-pattern>/Files</url-pattern>
</servlet-mapping>


Also, in you classes folder under WEB-INF, make sure you make a folder whose name is the same as the package name of the classes and put all the classes in that folder. In web.xml, use

<servlet> <servlet-name>File</servlet-name> <servlet-class>package.File</servlet-class> </servlet>

to reference you servlet in the classes folder

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜