我在/src/main/java/examples/web/SimpleServlet.java
中编码了以下servlet:
package examples.web;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class SimpleServlet extends HttpServlet {
public void goGet(HttpServletRequest req, HttpServletResponse res) throws IOException {
PrintWriter writer = res.getWriter();
writer.println("SimpleServlet Executed!");
writer.flush();
writer.close();
}
}
并且在
src/main/webapp/WEB-INF/web.xml
中定义了以下web.xml:<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>SimpleServlet</servlet-name>
<servlet-class>examples.web.SimpleServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SimpleServlet</servlet-name>
<url-pattern>/simpleservlet</url-pattern>
</servlet-mapping>
</web-app>
并且在
/pom.xml
中定义了以下pom.xml:<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>examples.web</groupId>
<artifactId>simple-webapp</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>simple-webapp Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>simple-webapp</finalName>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
当我运行
mvn jetty:run
时,我得到的输出表明一切正常。当我加载localhost:8080/simple-webapp
时,会得到src/main/webapp/index.jsp
的服务,因此我知道Jetty至少在部分工作。现在是一个问题:尽管我可以进入
index.jsp
,但是我却无法进入/simple-webapp/SimpleServlet
-我只是从Jetty收到404错误。由于在web.xml
中进行了配置,因此我期望从SimpleServlet Executed!
中看到SimpleServlet
。我究竟做错了什么?这是
mvn jetty:run
的相关输出:[INFO] Configuring Jetty for project: simple-webapp Maven Webapp
[INFO] Webapp source directory = C:\Users\Ken\Workspace\Eclipse\Maven\simple-webapp\src\main\webapp
[INFO] Reload Mechanic: automatic
[INFO] Classes = C:\Users\Ken\Workspace\Eclipse\Maven\simple-webapp\target\classes
2011-11-23 16:59:56.220:INFO::Logging to STDERR via org.mortbay.log.StdErrLog
[INFO] Context path = /simple-webapp
[INFO] Tmp directory = determined at runtime
[INFO] Web defaults = org/mortbay/jetty/webapp/webdefault.xml
[INFO] Web overrides = none
[INFO] web.xml file = C:\Users\Ken\Workspace\Eclipse\Maven\simple-webapp\src\main\webapp\WEB-INF\web.xml
[INFO] Webapp directory = C:\Users\Ken\Workspace\Eclipse\Maven\simple-webapp\src\main\webapp
最佳答案
URL区分大小写。定义的URL模式与您请求的值不匹配:
<url-pattern>/simpleservlet</url-pattern>
尝试http://localhost:8080/simple-webapp/simpleservlet(小写servlet名称)。