好的,这应该是基本的,我之前通过 eclipse 和 weblogic 多次使用过 servlet,没有问题。但是,对于 Tomcat 6,我遇到了 Servlet 映射问题。
web.xml:
<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>
Servlet and JSP Examples.
</description>
<display-name>Servlet and JSP Examples</display-name>
<servlet>
<servlet-name>HelloWorldExample</servlet-name>
<servlet-class>HelloWorldExample</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorldExample</servlet-name>
<url-pattern>/HelloWorldExample</url-pattern>
</servlet-mapping>
</web-app>
我的 web.xml 位于我的 WEB-INF 文件夹中,我的 servlet HelloWorldExample.class 位于 WEB-INF/classes/
它们都位于tomcat/webapps/ch1/
据我所知,一切正常,但是经过数小时的反复试验和多次重启后,我仍然得到:
type Status report
message /ch1/HelloWorldExample
description The requested resource is not available.
http://localhost:8080/ch1/HelloWorldExample
目录结构截图
如果有人能指出我在哪里犯了错误,我将永远感激 :)
最佳答案
你应该 build and export a WAR file并部署到Tomcat上,然后它会自动到正确的地方。另外,请注意 web.xml 的这一部分:
<servlet>
<servlet-name>HelloWorldExample</servlet-name>
<servlet-class>HelloWorldExample</servlet-class>
</servlet>
Servlet-class 需要是完全限定的类名,即:your.package.classname(如果您使用的是默认包,请忽略,这通常是一种不好的做法)。当你处理这个问题时,我建议你安装 Tomcat 7 并利用 Servlet 3.0 的好处,即摆脱 web.xml 配置并使用 servlet 注释。更多信息:
关于java - 未检测到 Tomcat 6 Servlet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14974205/