如何解决 Tomcat 8 /Library/Tomcat/work/Catalina/localhost 目录中缺少的 servlet class?

How to solve a servlet class missing from Tomcat 8 /Library/Tomcat/work/Catalina/localhost directory?

我创建了一个简单的应用程序,在登陆 index.jsp 后,用户可以单击 link 并使用 linked 到此 link 的 servlet。 问题是我没有看到在 /Library/Tomcat/work/Catalina/localhost 中生成任何 servlet class(存在 index.jsp class)。

相关文件如下:

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>com.skiabox.webapps</groupId>
  <artifactId>TheWebApp1</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>TheWebApp1 Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
      <scope>provided</scope>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.2</version>
        <configuration>
          <url>http://localhost:8080/manager/text</url>
          <server>TomcatServer</server>
        </configuration>
      </plugin>
    </plugins>
    <finalName>TheWebApp1</finalName>
  </build>
</project>

index.jsp :

<html>
    <head>
        <title>My Home Page</title>
    </head>
<body>
<h2>Hello World!</h2>

Hello from index.jsp!
Check your HelloWorld servlet <a href="HelloWorld">here</a>
</body>
</html>

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 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_3_0.xsd"
         version="3.0">
  <display-name>Archetype Created Web Application</display-name>
</web-app>

HelloWorld.java :

package gui;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by Administrator on 12/01/15.
 */
@WebServlet(name = "/HelloWorld/*")
public class HelloWorld extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();

        out.println("<html>");
        out.println("<b>Hello World</b>");
        out.println("</html>");
    }
}

我正在使用 tomcat maven 插件来编译和部署项目(您可以从 pom.xml 文件中轻松看到)

你有没有把 webservlet 的名字和 urlPatterns 搞混了?尝试:

@WebServlet("/HelloWorld/*")

或者也许:

@WebServlet(name = "HelloWorld", urlPatterns = "/HelloWorld/*")