0

我在 Maven 项目结构中使用了可嵌入的 Tomcat
(比如这里:https ://devcenter.heroku.com/articles/create-a-java-web-application-using-embedded-tomcat )
,但我没有部署到 Heroku .
我可以访问 index.jsp(甚至在添加 web.xml 之前),localhost:8080/
但我无法锻炼如何访问我的 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

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

项目结构

我尝试添加一个 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>

上下文.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">...

}

4

1 回答 1

2

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

您的 servlet 路径也是/ArcheryurlPatterns的属性@WebServlet)它也被您的 web.xml 复制

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

第一个 server[:port],然后是上下文路径,然后是 servlet 路径

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

打包好战争后,只要确保这些文件在那里(在WEB-INF下)

于 2013-07-31T10:28:34.690 回答