嵌入式jetty从servlet转发到jsp
我试图让我的嵌入式jetty servlet进行一些处理,然后将控制权传递给JSP,JSP将生成一个结果页 servlet被正确地映射和调用,但是它找不到JSP。因为我使用的是嵌入式jetty,所以我没有web.xml,也没有战争。也许这意味着jetty不知道在哪里可以找到我的JSP或其他东西。如果是这种情况,我如何告诉eclipse/jetty在哪里可以找到它,或者我如何调用forward缺少什么 注意:我使用的是一个常规的maven项目,所以我必须自己创建WEB-INF文件夹。可能是出了什么问题的线索嵌入式jetty从servlet转发到jsp,jsp,servlets,jetty,forwarding,embedded-jetty,Jsp,Servlets,Jetty,Forwarding,Embedded Jetty,我试图让我的嵌入式jetty servlet进行一些处理,然后将控制权传递给JSP,JSP将生成一个结果页 servlet被正确地映射和调用,但是它找不到JSP。因为我使用的是嵌入式jetty,所以我没有web.xml,也没有战争。也许这意味着jetty不知道在哪里可以找到我的JSP或其他东西。如果是这种情况,我如何告诉eclipse/jetty在哪里可以找到它,或者我如何调用forward缺少什么 注意:我使用的是一个常规的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>
4.0.0
com.hp.it.kmcs.search
JettyTest
0.0.1-快照
罐子
JettyTest
http://maven.apache.org
7.2.0.v20101020
org.eclipse.jetty.aggregate
jetty所有服务器
7.6.0.1
罐子
编译
org.springframework
弹簧芯
3.1.0.1发布
罐子
编译
org.mortbay.jetty
jetty maven插件
${jettyVersion}
org.codehaus.mojo
execmaven插件
1.1
JAVA
com.hp.it.kmcs.JettyTest.RunHelloServlet
您的嵌入式应用程序控制台是否显示如下信息:
INFO:oejw.StandardDescriptorProcessor:NO JSP Support for /servletpath, did not find org.apache.jasper.servlet.JspServlet
默认情况下,嵌入式Jetty没有启用JSP支持。有关详细信息,请参阅。HTH.所以我使用setWar和正确的罐子来实现这一点。使用此代码可以直接寻址jsp(localhost:8080/result.jsp),更重要的是,可以使用servlet(localhost:8080/hello).forward命令转发到jsp。这将使我能够使用jsp提供一些动态内容 代码如下。。。(注意:嵌入式Jetty=>不需要web.xml) 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</groupId>
<artifactId>jetty-server</artifactId>
<version>7.6.0.RC1</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-util</artifactId>
<version>7.6.0.RC1</version>
<type>jar</type>
<classifier>config</classifier>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jsp-2.1-glassfish</artifactId>
<version>2.1.v20100127</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jdt.core.compiler</groupId>
<artifactId>ecj</artifactId>
<version>3.5.1</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>servlet-api-2.5</artifactId>
<version>6.1.14</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-webapp</artifactId>
<version>7.6.0.RC0</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>
4.0.0
com.hp.it.kmcs.search
JettyTest
0.0.1-快照
罐子
JettyTest
http://maven.apache.org
7.2.0.v20101020
org.eclipse.jetty
jetty服务器
7.6.0.1
罐子
编译
org.eclipse.jetty
码头
7.6.0.1
罐子
配置
编译
org.mortbay.jetty
jsp-2.1-glassfish
2.1.v20100127
罐子
编译
org.eclipse.jdt.core.compiler
欧洲法院
3.5.1
罐子
编译
org.mortbay.jetty
servlet-api-2.5
6.1.14
罐子
编译
org.eclipse.jetty
jetty网络应用程序
7.6.0.RC0
罐子
编译
org.mortbay.jetty
jetty maven插件
${jettyVersion}
org.codehaus.mojo
execmaven插件
1.1
JAVA
com.hp.it.kmcs.JettyTest.RunHelloServlet
没有控制台输出不会像这样抱怨。我现在在我的工作区中弄坏了一些东西:)但我会马上给你确切的控制台输出,关于JSP支持的部分至少可以说是令人困惑的。然而,它以这样一句话结束:你们可以使用一个已经使用控制台的jetty聚合罐,上面写着。。。。。。2011-12-22 14:12:17.262:INFO:oejs.Server:jetty-7.6.0.RC1 2011-12-22 14:12:17.370:INFO:oejsh.ContextHandler:started o.e.j.s.ServletContextHandler{/,file:/C:/dev/workspace/JettyTest/}2011-12-22 14:12:17.425:INFO:oejs.AbstractConnector:startedSelectChannelConnector@0.0.0.0:8080开始好的,看来没问题,这就是我遇到的问题。谢谢——这帮我找到了一个相关问题的答案。对我来说,从通用ServletContextHandler切换到WebAppContext解决了这个问题(带有guice servlet注入的嵌入式jetty)。我正在将ResourceBase设置为web应用程序路径。
<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</groupId>
<artifactId>jetty-server</artifactId>
<version>7.6.0.RC1</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-util</artifactId>
<version>7.6.0.RC1</version>
<type>jar</type>
<classifier>config</classifier>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jsp-2.1-glassfish</artifactId>
<version>2.1.v20100127</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jdt.core.compiler</groupId>
<artifactId>ecj</artifactId>
<version>3.5.1</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>servlet-api-2.5</artifactId>
<version>6.1.14</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-webapp</artifactId>
<version>7.6.0.RC0</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>