简体   繁体   English

嵌入式Jetty转发servlet对JSP的响应

[英]Embedded Jetty forward servlet response to JSP

Using Embedded Jetty I am trying to get a very simple servlet to forward to JSP page once the servlet doGet() has executed. 使用Embedded Jetty我试图获得一个非常简单的servlet,一旦servlet doGet()执行,就转发到JSP页面。 However instead of reaching the JSP it simply recursively forwards to the same doGet() which is calling the forward. 然而,它不是到达JSP而是简单地递归转发到调用转发的同一个doGet()。

Im very new to this stuff but its like it either cant find the JSP and instead maps to the only servlet that it can find or else I'm not registering the JSP or something. 我对这个东西很新,但它喜欢它无论是找不到JSP而是映射到它可以找到的唯一的servlet,否则我没有注册JSP或其他东西。 please help. 请帮忙。

My code is as follows... 我的代码如下......

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();
        }

    }
}
}

My JSP located at .\\src\\main\\webapp\\WEB-INF\\result.jsp 我的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>

My pom.xml... 我的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>

It looks like you've mapped HelloServlet so that it handles almost every request. 看起来你已经映射了HelloServlet因此它几乎可以处理每个请求。 When you try to forward the request from HelloServlet to /result.jsp , Jetty matches the path /result.jsp to /* and so forwards the request right back to HelloServlet (which forwards it to /result.jsp and so on recursively). 当您尝试将请求从HelloServlet转发到/result.jsp ,Jetty将路径/result.jsp/*匹配,然后将请求直接转发回HelloServlet (后者将其转发到/result.jsp ,依此类推)。

You should make your servlet mapping more restrictive (eg /hello instead of /* ). 您应该使您的servlet映射更具限制性(例如/hello而不是/* )。

Furthermore, since your .jsp file is in WEB-INF, you should be forwarding to /WEB-INF/result.jsp . 此外,由于您的.jsp文件位于WEB-INF中,因此您应该转发到/WEB-INF/result.jsp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM