tags:

views:

2146

answers:

5

I'm trying to get into Java web development but seem to be running into a strange issue with Tomcat and an extremely simple servlet. The catalina log is spewing this every time I try and load the app:

Caused by: java.lang.IllegalArgumentException: Servlet mapping specifies an unknown servlet name MyServlet
    at org.apache.catalina.core.StandardContext.addServletMapping(StandardContext.java:2393)
    at org.apache.catalina.core.StandardContext.addServletMapping(StandardContext.java:2373)
    ... 40 more
Mar 4, 2009 10:37:58 AM org.apache.catalina.startup.ContextConfig applicationWebConfig
SEVERE: Parse error in application web.xml file at jndi:/localhost/mywebapp/WEB-INF/web.xml
java.lang.IllegalArgumentException: Servlet mapping specifies an unknown servlet name MyServlet

Makes decent sense. It can't seem to find my servlet. However, the servlet seems to be in the right place. I can plainly see it at WEB-INF/classes/MyServlet.class

For reference, this is the web.xml file I'm currently using:

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> 
    <description>My first web app in Java.</description>
    <display-name>My Web App</display-name>

    <servlet>
     <servlet-name>MyServlet</servlet-name>
     <servlet-class>MyServlet</servlet-class>
    </servlet>

    <servlet-mapping>
     <servlet-name>MyServlet</servlet-name>
     <url-pattern>/myservlet</url-pattern>
    </servlet-mapping>
</web-app>

As you can see, I'm keeping things simple... but it continually throws this exception. What does a bare-bones web app look like in Java, which components am I missing?

Update

To make absolutely certain it wasn't an artifact of some kind, I started up a fresh copy of Tomcat and tried again. Upon doing so, this started appearing in the log files:

SEVERE: Error deploying web application archive mywebapp.war
java.lang.UnsupportedClassVersionError: Bad version number in .class file (unable to load class MyServlet)

I dumped my .class file for the MyServlet class, rebuilt it with -target 1.5, repackaged the .war and everything worked perfectly.

Thank you so much for the help! A good lesson in troubleshooting never hurt anybody.

A: 

Could there be a older version of web.xml lurking around somewhere, without the <servlet> block?

Perhaps do a find . -type f -name web.xml (or similar for windows) in tomcat's directories?

toolkit
A: 

If the MyServlet class is in a package, you should list it with package-dot-class notation in your servlet-name nodes.

jedierikb
I certainly plan to move to a dot notation, I was just miffed by my inability to get such a simple app running that I avoided changing too much before I resolved this issue. Thanks for the input!
spligak
A: 

Are the lines below exactly from your web.xml? Tomcat finds the servlet mapping that maps /myservlet to the servlet named "MyServlet" but complains that it cannot find any servlet definition with that name. Case matters. What you provided looks correct, but double check your web.xml to make sure the case is correct. Double check the web.xml where Tomcat is using it, in the directory mywebapp/WEB-INF/web.xml

<servlet>
    <servlet-name>MyServlet</servlet-name>   <-- Check this name
    <servlet-class>MyServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>MyServlet</servlet-name>  <-- Compare against this name
    <url-pattern>/myservlet</url-pattern>
</servlet-mapping>

If that's not it, let us know. But these names are case sensitive.

Eddie
A: 

Few hints:

  • Does your servlet class extend HttpServlet?
  • How about filesystem rights - any possible problem there?
  • Perhaps, try also to move the class to a named package (myservlet.MyServlet)

In general, also try to look for more exceptions in the logs.

david a.
Turns out there was a very important exception I was missing. Check my update above.
spligak
+4  A: 

Well, given the updated information, it appears that your problem is that the compiler you used for your class is potentially a newer version of the JDK than the one running Tomcat.

Check the JDK version being used to start Tomcat, and then see if you can do something to reconcile the version differences between that and the one you're using to compile your servlet with.

That should clear up your issue.

Macon Pegram
That's what it turned out to be. I believe it was 1.6 building and 1.5 running Tomcat. I fixed that issue and everything seems to be building and running fine. Thanks!
spligak