2011-12-19 101 views
0

使用嵌入式Jetty我試圖獲得一個非常簡單的servlet,以便在執行servlet doGet()後轉發到JSP頁面。然而,不是到達JSP,它只是遞歸地轉發到調用轉發的同一個doGet()。使用嵌入式Jetty嵌入式Jetty向前servlet響應到JSP

我對這個東西很新,但它喜歡它無法找到JSP,而是映射到它可以找到的唯一的servlet,否則我沒有註冊JSP或什麼。請幫忙。

我的代碼如下...

import java.io.IOException; 
import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import org.eclipse.jetty.server.Server; 
import org.eclipse.jetty.servlet.ServletContextHandler; 
import org.eclipse.jetty.servlet.ServletHolder; 

public class RunHelloServlet { 

public static void main(String[] args) throws Exception { 
    Server server = new Server(8080); 

    ServletContextHandler contextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS); 
    contextHandler.setContextPath("/"); 
    server.setHandler(contextHandler); 

    contextHandler.addServlet(new ServletHolder(new HelloServlet()), "/*"); 
    server.start(); 
    server.join(); 
} 

public static class HelloServlet extends HttpServlet { 
    private static final long serialVersionUID = 1L; 

    public HelloServlet() { 
    } 

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { 
     String par1 = request.getParameter("par1"); 
     request.setAttribute("key", par1); 
     try { 
      request.getRequestDispatcher("/result.jsp").forward(request, response); 
     } 
     catch (ServletException e1) { 
      e1.printStackTrace(); 
     } 

    } 
} 
} 

我的JSP位於\ SRC \主\ web應用\ WEB-INF \ result.jsp中

<%@ page language="javascript" contentType="text/html; charset=ISO-8859-1" 
pageEncoding="ISO-8859-1"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title>Insert title here</title> 
</head> 
<body> 

</body> 
</html> 

我的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/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 

    <groupId>com.hp.it.kmcs.search</groupId> 
    <artifactId>JettyTest</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <packaging>jar</packaging> 

    <name>JettyTest</name> 
    <url>http://maven.apache.org</url> 

<properties> 
    <jettyVersion>7.2.0.v20101020</jettyVersion> 
    </properties> 

    <dependencies> 
    <dependency> 
     <groupId>org.eclipse.jetty.aggregate</groupId> 
     <artifactId>jetty-all-server</artifactId> 
     <version>7.6.0.RC1</version> 
     <type>jar</type> 
     <scope>compile</scope> 
    </dependency> 
    </dependencies> 

    <build> 
    <plugins> 
     <plugin> 
     <!-- This plugin is needed for the servlet example --> 
     <groupId>org.mortbay.jetty</groupId> 
     <artifactId>jetty-maven-plugin</artifactId> 
     <version>${jettyVersion}</version> 
     </plugin> 
     <plugin> 
     <groupId>org.codehaus.mojo</groupId> 
     <artifactId>exec-maven-plugin</artifactId> 
     <version>1.1</version> 
     <executions> 
      <execution><goals><goal>java</goal></goals></execution> 
     </executions> 
     <configuration> 
      <mainClass>com.hp.it.kmcs.JettyTest.RunHelloServlet</mainClass> 
     </configuration> 
     </plugin> 
    </plugins> 
    </build> 
</project> 
+0

我看到你有一個pom.xml。我相信這是來自Maven或其他事情嗎?無論如何,如果你有一個web.xml,你也應該發佈它。 – Dave 2011-12-19 16:20:25

+0

沒有我不使用任何過濾器或最少我沒有意識到任何。我沒有任何web.xml,但有一個印象,即碼頭我不一定需要一個 – 2011-12-19 16:25:16

+0

沒有web.xml。我的印象(也許是錯誤的),與嵌入式碼頭,我可能不需要一個。 servlet最初啓動並接受請求,這讓我相信我在這個假設中是正確的。然而,隨着事情變得更加複雜,可能需要? – 2011-12-19 16:30:07

回答

2

看起來你已經規劃HelloServlet,使其處理幾乎每請求。當您嘗試將請求從HelloServlet轉發到/result.jsp時,Jetty匹配路徑/result.jsp/*,因此會將請求轉發回HelloServlet(將其遞歸轉發到/result.jsp等等)。

您應該使您的servlet映射更具限制性(例如/hello而不是/*)。

此外,由於您的.jsp文件位於WEB-INF中,因此您應該轉發至/WEB-INF/result.jsp

+0

該映射似乎工作謝謝。它不再以遞歸方式運行,但它似乎沒有找到jsp文件。瀏覽器說不能找到/WEB-INF/result.jsp。不太清楚爲什麼。 – 2011-12-19 16:39:23

+0

@robsbobs,我從來沒有處理從'main'運行Jetty服務器。正如您對問題的評論所指出的,大多數Java Web應用程序都有一個'web.xml'文件來處理映射,並將其部署到已經運行的Web服務器上。但是,我懷疑你可能需要告訴Jetty在哪裏找到WEB-INF目錄。也許它甚至沒有加載它。 – 2011-12-19 16:42:40

+0

你可能是對的。生病了看着它。這是迴應。 – 2011-12-19 16:51:53