2013-07-31 116 views
0

我使用的是嵌入在Tomcat Maven項目結構
(喜歡這裏: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
如何訪問的tomcat的servlet在Maven項目結構,嵌入式的tomcat

我試圖把它們放入資源文件夾嘗試這已經是該項目的一部分。

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">... 

}

回答

2

你的應用程序上下文路徑/Archery(在context.xml定義)

你的servlet路徑也是/Archery(的urlPatterns屬性@WebServlet) 它也通過你的web.xml複製

那麼,您的網址應爲localhost:8080/Archery/Archery

第一服務器[:端口],其次是上下文路徑,其次是servlet路徑

無論如何,你會必須修復項目結構才能運作。 如果你是以下Maven約定的網絡資源目錄應該是src/main/webappweb.xml(和context.xml)應WEB-INF目錄下這個目錄下放置。

你已經打包戰爭結束後,只要確保這些文件存在(在WEB-INF)

+0

只是嘗試這條道路,仍然沒有去:( 剛剛發現此http://計算器。com/questions/6535676/webservlet-annotation-with-tomcat7其中說xml需要符合3.0的註釋才能工作。所以它應該忽略我的。但我會嘗試沒有他們。 – jsky

+0

仍將404註釋註釋掉。 – jsky

+0

我需要一個全球性的web.xml嗎? <信息:沒有找到全球web.xml> – jsky

相關問題