【问题标题】:How to solve a servlet class missing from Tomcat 8 /Library/Tomcat/work/Catalina/localhost directory?如何解决 Tomcat 8 /Library/Tomcat/work/Catalina/localhost 目录中缺少的 servlet 类?
【发布时间】:2015-01-12 20:54:37
【问题描述】:

我创建了一个简单的应用程序,在登陆到 index.jsp 后,用户可以单击一个链接并使用链接到该链接的 servlet。 问题是我看不到 /Library/Tomcat/work/Catalina/localhost 内部没有生成 servlet 类(存在 index.jsp 类)。

以下是相关文件:

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文件可以很容易地看到)

【问题讨论】:

    标签: java maven tomcat tomcat8 maven-tomcat-plugin


    【解决方案1】:

    您没有将 webservlet 的名称与 urlPatterns 混淆吗?试试:

    @WebServlet("/HelloWorld/*")
    

    或者也许:

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

    【讨论】:

    • 非常感谢!我曾尝试使用 urlPatterns 但没有此模式。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-27
    • 2013-06-15
    • 2023-01-13
    • 2018-11-04
    • 1970-01-01
    • 2012-02-09
    相关资源
    最近更新 更多