maven - 如何使用可嵌入的tomcat访问maven项目结构中的tomcat servlet

标签 maven tomcat servlets

我在 Maven 项目结构中使用可嵌入的 Tomcat
(像这里:https://devcenter.heroku.com/articles/create-a-java-web-application-using-embedded-tomcat)
但我没有部署到 Heroku。
我可以在 localhost:8080/
访问 index.jsp(甚至在添加 web.xml 之前) 但是我无法锻炼如何访问我的 servlet(即使在添加 web.xml 之前,也会收到 404)。
localhost:8080/Archery
试过 在 localhost:8080/servlets/servlet.ArcheryShootServlet
试过 尝试在 localhost:8080/servlet/servlet.ArcheryShootServlet
localhost:8080/servlet.ArcheryShootServlet
尝试过 在 localhost:8080/target/Archery
试过 在 localhost:8080/target/ArcheryShootServlet
试过 尝试在 localhost:8080/target/servlet.ArcheryShootServlet

我试过将它们放入已经是项目一部分的资源文件夹中。

Project Structure

我尝试添加一个 webResources 文件夹,并将其添加到
pom 文件配置:

   <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.heroku.sample</groupId>
  <artifactId>embeddedTomcatSample</artifactId>
  <version>1.0-SNAPSHOT</version>
  <name>embeddedTomcatSample Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <properties>
    <tomcat.version>7.0.34</tomcat.version>
  </properties>
  <dependencies>
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-core</artifactId>
        <version>${tomcat.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-logging-juli</artifactId>
        <version>${tomcat.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <version>${tomcat.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat</groupId>
        <artifactId>tomcat-jasper</artifactId>
        <version>${tomcat.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat</groupId>
        <artifactId>tomcat-jasper-el</artifactId>
        <version>${tomcat.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat</groupId>
        <artifactId>tomcat-jsp-api</artifactId>
        <version>${tomcat.version}</version>
    </dependency>
    <dependency>
      <groupId>org.swinglabs</groupId>
      <artifactId>swing-layout</artifactId>
      <version>1.0.3</version>
    </dependency>
  </dependencies>
  <build>
    <finalName>embeddedTomcatSample</finalName>
    <plugins>
    <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>1.1.1</version>
        <configuration>
        <webResources>
                    <resource>
                        <directory>WebContent/WEB-INF</directory>
                        <includes>
                            <include>**/*.properties</include>
                            <include>**/*.xml</include>
                            <include>**/*.css</include>
                            <include>**/*.html</include>
                        </includes>
                    </resource>
                </webResources>
        </configuration>
    </plugin>   
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>appassembler-maven-plugin</artifactId>
            <version>1.1.1</version>
            <configuration>
                <assembleDirectory>target</assembleDirectory>
                <programs>
                    <program>
                        <mainClass>launch.ArcheryServer</mainClass>
                        <name>webapp</name>
                    </program>
                </programs>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>assemble</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
  </build>
</project>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">
    <servlet>
        <servlet-name>ArcheryShootServlet</servlet-name>
        <servlet-class>servlet.ArcheryShootServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>ArcheryShootServlet</servlet-name>
        <url-pattern>/Archery</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>/WEB-INF/index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

context.xml

 <?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/Archery"/>

ArcheryShootServlet.java

    package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(
        name = "Archery", 
        urlPatterns = {"/Archery"}
    )

public class ArcheryShootServlet extends HttpServlet {
protected void processRequest(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
String xmlSent = req.toString();    
System.out.println(xmlSent);
    ServletOutputStream out = resp.getOutputStream();
    PrintWriter test = resp.getWriter();
test.write("hello");
    out.write("hello heroku".getBytes());
out.write(xmlSent.getBytes());

    out.flush();
    out.close();
}

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. doGet and doPost call processRequest">...

最佳答案

您的应用程序上下文路径是/Archery(在context.xml 中定义)

您的 servlet 路径也是 /Archery(@WebServleturlPatterns 属性) 它也被你的 web.xml 复制

所以,你的 URL 应该是 localhost:8080/Archery/Archery

首先是 server[:port],然后是上下文路径,然后是 servlet 路径

无论如何,您必须修复项目结构才能使其正常工作。 如果您遵循 Maven 约定,Web 资源目录应该是 src/main/webapp web.xml(和context.xml)应该放在该目录下的WEB-INF目录下。

在你打包war之后,只要确保这些文件在那里(在WEB-INF下)

关于maven - 如何使用可嵌入的tomcat访问maven项目结构中的tomcat servlet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17967633/

相关文章:

java - 缺少 Artifact com.sun.jersey :jersey-servlet. jar

java - 使用launch4j和maven时不会创建.exe

java - 在 Eclipse 中混合 scala、maven 和 java - 单元测试

Apache + Tomcat + Struts 2 配置

java - 杂散 "html"开始标记

jquery - 我如何从单独的文本框中的自动完成响应中获取 json 键和值。请看图片

java - Arquillian 测试未执行

tomcat - 如何将数据源从 jboss standalone.xml 迁移到 tomcat 9

rest - 设置camunda rest api

Kotlin 类 servlet 引发 NoClassDefFoundError