Tomcat无法找到我的Servlet并抛出异常,但为什么呢? [英] Tomcat is unable to find my Servlet and is throwing exceptions, but why?

查看:73
本文介绍了Tomcat无法找到我的Servlet并抛出异常,但为什么呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

有不错的感觉。它似乎找不到我的servlet。但是,servlet似乎在正确的位置。我可以在 WEB-INF / classes / MyServlet.class

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

中清楚地看到它。作为参考,这是web.xml文件我是目前使用:

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>

正如您所看到的,我保持简单......但它会不断抛出此异常。简单的Java应用程序在Java中是什么样的,我缺少哪些组件?

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?

更新

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

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转储我的MyServlet类的.class文件,用-target 1.5重新构建它,重新打包.war,一切都运行得很好。

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.

推荐答案

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

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版本,然后查看是否可以执行某些操作来协调它与您用于编译您的版本之间的版本差异servlet用。

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.

这篇关于Tomcat无法找到我的Servlet并抛出异常,但为什么呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆