简体   繁体   English

使用eclipse创建servlet后,Tomcat 7服务器无法启动

[英]Tomcat 7 server failed to start after creating a servlet using eclipse

There error I get when I attempt to start the local Tomcat 7 server in Eclipse: 当我尝试在Eclipse中启动本地Tomcat 7服务器时出现错误:

'Starting Tomcat v7.0 Server at localhost' has encountered a problem. '在localhost启动Tomcat v7.0服务器'遇到了问题。 Server Tomcat v7.0 Server at localhost failed to start. localhost上的服务器Tomcat v7.0服务器无法启动。

These are the steps I took: 这些是我采取的步骤:

  1. Create a new project called "test". 创建一个名为“test”的新项目。
  2. Create a new index.jsp file. 创建一个新的index.jsp文件。
  3. Create a new servlet called "Testservlet". 创建一个名为“Testservlet”的新servlet

    Package name: testpackage 包名:testpackage

  4. Configure a new build path for Web App Libraries. 为Web App库配置新的构建路径。 Add an External JAR from the Tomcat 7 directory I add servlet-api.jar . 从Tomcat 7目录添加外部JAR我添加了servlet-api.jar

This is the path structure in Eclipse: 这是Eclipse中的路径结构:

在此输入图像描述

The contents of the web.xml file: 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>

Contents of the Testservlet.java file: Testservlet.java文件的内容:

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");
    }
}

Next I add the project to the Tomcat server and attempt to start the server. 接下来,我将项目添加到Tomcat服务器并尝试启动服务器。 Which is when I get the error stopping the server from running. 这是当我得到错误停止服务器运行时。

If I remove the servlet related markup from the web.xml file the server starts perfectly fine. 如果我从web.xml文件中删除与servlet相关的标记,则服务器启动完全正常。 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>

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? 我假设我需要在web.xml中使用上面的标记,以便在我导航到test / Testservlet时显示servlet? Especially when I deploy the project to a remote server. 特别是当我将项目部署到远程服务器时。

What am I doing wrong here? 我在这做错了什么?

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 . Servlet API 3.0包含一个名为javax.servlet.annotation的新包,它提供了@WebServlet类的注释,用于注释servlet类,这可以消除web.xml中servlet配置的使用 So, servlet and servlet-mapping are obsolete. 因此, servletservlet-mapping已经过时了。

Also you should not deploy servlet-api.jar with your application. 此外,您不应该将servlet-api.jar与您的应用程序一起部署。 Tomcat 7 already has these classes and it supports many Servlet APIs. Tomcat 7已经有了这些类,它支持许多Servlet API。 It will automatically choose which servlet API to load using your web application deployment descriptor. 它将使用您的Web应用程序部署描述符自动选择要加载的servlet API。

最佳解决方案是在创建servlet时添加服务器运行时1.在创建sevelet名称时添加服务器运行时2.添加指定的tomcat安装位置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM