问题描述
尝试在Eclipse中启动本地Tomcat 7服务器时出现错误:
There error I get when I attempt to start the local Tomcat 7 server in Eclipse:
这些是我采取的步骤:
- 创建一个名为"test"的新项目.
- 创建一个新的index.jsp文件.
-
创建一个名为"Testservlet"的新
servlet
.
包名称:testpackage
Package name: testpackage
这是Eclipse中的路径结构:
This is the path structure in Eclipse:
web.xml
文件的内容:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>test</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Testservlet</servlet-name>
<servlet-class>testpackage.Testservlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Testservlet</servlet-name>
<url-pattern>/Testservlet</url-pattern>
</servlet-mapping>
</web-app>
Testservlet.java 文件的内容:
Contents of the Testservlet.java
file:
package testpackage;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(description = "a test servlet", urlPatterns = { "/Testservlet" })
public class Testservlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
response.setContentType("text/html");
out.println("This is a test servlet");
}
}
接下来,我将项目添加到Tomcat服务器并尝试启动服务器.这是我在停止服务器运行时遇到错误的情况.
Next I add the project to the Tomcat server and attempt to start the server. Which is when I get the error stopping the server from running.
如果我从web.xml文件中删除了与servlet相关的标记,则服务器可以正常启动.这是我删除的标记:
If I remove the servlet related markup from the web.xml file the server starts perfectly fine. This is the markup I remove:
<servlet>
<servlet-name>Testservlet</servlet-name>
<servlet-class>testpackage.Testservlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Testservlet</servlet-name>
<url-pattern>/Testservlet</url-pattern>
</servlet-mapping>
我假设我需要在web.xml中使用上面的标记,以便当我导航到test/Testservlet时才能显示servlet?尤其是当我将项目部署到远程服务器时.
I assume that I need the above markup in the web.xml in order for the servlet to display when I navigate to test/Testservlet though? Especially when I deploy the project to a remote server.
我在做什么错了?
推荐答案
Servlet API 3.0包括一个名为 javax.servlet.annotation
的新程序包,它提供了 @WebServlet
之类的注释.用于注释servlet类,这可以消除对 web.xml
中servlet配置的使用.因此, servlet
和 servlet-mapping
已过时.
Servlet API 3.0 included a new package called javax.servlet.annotation
which provides annotations like @WebServlet
to be used to annotate a servlet class and this can eliminate the use of servlet configuration in web.xml
. So, servlet
and servlet-mapping
are obsolete.
此外,您不应该在应用程序中部署 servlet-api.jar
.Tomcat 7已经具有这些类,并且支持许多Servlet API.它将使用您的Web应用程序部署描述符自动选择要加载的servlet API.
Also you should not deploy servlet-api.jar
with your application. Tomcat 7 already has these classes and it supports many Servlet APIs. It will automatically choose which servlet API to load using your web application deployment descriptor.
这篇关于使用Eclipse创建Servlet后,Tomcat 7服务器无法启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!