Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.defineClassCond(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)
like image 801
Nathan Campos Avatar asked Jan 21 '23 19:01

Nathan Campos


2 Answers

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.

like image 71
BalusC Avatar answered Jan 24 '23 10:01

BalusC


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

like image 39
Mike Tunnicliffe Avatar answered Jan 24 '23 09:01

Mike Tunnicliffe