【问题标题】:Embedded Jetty forward servlet response to JSP嵌入式 Jetty 转发 servlet 对 JSP 的响应
【发布时间】:2011-12-19 16:08:25
【问题描述】:

使用 Embedded Jetty 我试图在 servlet doGet() 执行后获得一个非常简单的 servlet 以转发到 JSP 页面。然而,它并没有到达 JSP,而是简单地递归地转发到调用转发的同一个 doGet()。

我对这个东西很陌生,但它要么找不到 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\main\webapp\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>

【问题讨论】:

  • 我看到你有一个 pom.xml。我相信那是来自 Maven 还是对吧?无论如何,如果你有一个 web.xml,你也应该发布它。
  • 不,我没有使用任何过滤器,或者至少我不知道任何过滤器。我没有任何 web.xml,但我的印象是有了码头,我不一定需要一个
  • 没有 web.xml。我的印象是(可能是错误的)嵌入式码头我可能不需要一个。 servlet 最初启动并接受请求,这使我相信我的假设是正确的。但是,随着事情变得越来越复杂,可能需要它?

标签: java servlets jetty forwarding embedded-jetty


【解决方案1】:

看起来您已经映射了HelloServlet,因此它几乎可以处理每个请求。当您尝试将请求从 HelloServlet 转发到 /result.jsp 时,Jetty 将路径 /result.jsp 匹配到 /* 并因此将请求转发回 HelloServlet(将其转发到 /result.jsp 以此类推)。

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

此外,由于您的 .jsp 文件在 WEB-INF 中,您应该转发到 /WEB-INF/result.jsp

【讨论】:

  • 该映射似乎有效,谢谢。它不再递归运行,但似乎没有找到 jsp 文件。浏览器说找不到 /WEB-INF/result.jsp。不太清楚为什么。
  • @robsbobs,我从来没有处理过从 main 运行 Jetty 服务器的问题。正如您问题的 cmets 中所述,大多数 Java Web 应用程序都有一个 web.xml 文件来处理映射并部署到已经运行的 Web 服务器。但是,我怀疑您可能需要告诉 Jetty 在哪里可以找到 WEB-INF 目录。也许它甚至没有加载它。
  • 你可能是对的。生病调查它。这就是响应。
猜你喜欢
  • 2016-05-04
  • 2011-05-09
  • 2012-08-05
  • 2015-06-24
  • 2011-09-21
  • 2015-02-25
  • 1970-01-01
  • 1970-01-01
  • 2021-04-03
相关资源
最近更新 更多