2011-12-19 102 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