【发布时间】:2011-12-21 17:01:46
【问题描述】:
我尝试让我的嵌入式码头 servlet 进行一些处理,然后将控制权交给将生成结果页面的 JSP。
servlet 被正确映射和调用,但是找不到 JSP。由于我使用嵌入式码头,我没有 web.xml,也没有战争。也许这意味着码头不知道在哪里寻找我的 JSP 或其他东西。如果是这种情况,我如何告诉 eclipse/jetty 在哪里可以找到它,或者我在呼叫前锋时是否缺少某些东西。
注意我使用的是常规的 Maven 项目,所以必须自己创建 WEB-INF 文件夹。可能是出了什么问题的线索!?
import java.io.IOException;
import javax.servlet.RequestDispatcher;
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;
import org.springframework.core.io.ClassPathResource;
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()), "/hello");
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);
// logic
try {
RequestDispatcher r = request.getRequestDispatcher("/result.jsp");
request.getRequestDispatcher("/WEB-INF/result.jsp").forward(request, response);
}
catch (ServletException e1) {
e1.printStackTrace();
}
}
}
}
我的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/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>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.1.0.RELEASE</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>
【问题讨论】:
标签: jsp servlets jetty forwarding embedded-jetty