Java Tomcat 无法找到我的 Servlet 并抛出异常,但为什么呢?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/611465/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 16:51:22  来源:igfitidea点击:

Tomcat is unable to find my Servlet and is throwing exceptions, but why?

javatomcat

提问by spligak

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:

我正在尝试进入 Java Web 开发,但似乎遇到了 Tomcat 和一个非常简单的 servlet 的奇怪问题。每次我尝试加载应用程序时,catalina 日志都会喷出这个:

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

很有道理。它似乎无法找到我的 servlet。但是,servlet 似乎在正确的位置。我可以在WEB-INF/classes/MyServlet.class 中清楚地看到它

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

作为参考,这是我目前使用的 web.xml 文件:

<?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?

正如你所看到的,我让事情变得简单......但它不断抛出这个异常。Java 中的基本 Web 应用程序是什么样的,我缺少哪些组件?

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:

为了绝对确定它不是某种人工制品,我启动了 Tomcat 的新副本并再次尝试。这样做后,这开始出现在日志文件中:

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.

我为 MyServlet 类转储了我的 .class 文件,用 -target 1.5 重建它,重新打包 .war 并且一切正常。

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

十分感谢你的帮助!故障排除的好教训永远不会伤害任何人。

采纳答案by Macon Pegram

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.

好吧,鉴于更新的信息,您的问题似乎是您用于类的编译器可能是比运行 Tomcat 的 JDK 版本更新的 JDK 版本。

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.

检查用于启动 Tomcat 的 JDK 版本,然后看看您是否可以做一些事情来协调该版本与用于编译 servlet 的版本之间的版本差异。

That should clear up your issue.

那应该可以解决您的问题。

回答by toolkit

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

会不会有一个旧版本的web.xml潜伏在某个地方,没有<servlet>块?

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

也许find . -type f -name web.xml在tomcat的目录中做一个(或类似的Windows)?

回答by jedierikb

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

如果 MyServlet 类在一个包中,你应该在你的 servlet-name 节点中用 package-dot-class 符号列出它。

回答by Eddie

Are the lines below exactlyfrom your web.xml? Tomcat finds the servlet mapping that maps /myservletto 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.xmlto make sure the case is correct. Double check the web.xmlwhere Tomcat is using it, in the directory mywebapp/WEB-INF/web.xml

下面的行是否完全来自您的web.xml? Tomcat 找到了映射/myservlet到名为“ MyServlet”的 servlet的 servlet 映射,但抱怨说它找不到任何具有该名称的 servlet 定义。案例很重要。您提供的内容看起来正确,但请仔细检查web.xml以确保案例正确。web.xml在目录中仔细检查Tomcat在哪里使用它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.

如果不是这样,请告诉我们。但这些名称区分大小写。

回答by david 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)
  • 您的 servlet 类是否扩展了 HttpServlet?
  • 文件系统权限怎么样 - 有什么可能的问题吗?
  • 也许,也尝试将类移动到命名包(myservlet.MyServlet)

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

一般来说,还要尝试在日志中查找更多异常。