从servlet到jsp的嵌入式码头 [英] embedded jetty forward from servlet to jsp
问题描述
我尝试让我的嵌入式码头servlet进行一些处理,然后将控制权传递给JSP,这将生成结果页面.
Im try to get my embedded jetty servlet to do some processing and then pass control over to a JSP which will generate a result page.
该servlet被正确映射和调用,但是找不到JSP.因为我使用嵌入式码头,所以我没有web.xml,也没有战争.也许这意味着码头不知道在哪里寻找我的JSP之类的东西.如果是这样的话,我该如何告诉eclipse/jetty在哪里找到它,或者在我如何称呼前锋时我缺少什么.
The servlet gets mapped and called correctly however it fails to find the JSP. Since Im using embedded jetty I don't have a web.xml nor do I have a war. Maybe this means jetty doesn't know where to look for my JSP or something. If this is the case how can I tell eclipse/jetty where to find this or is there something Im missing with how I am calling the forward.
我正在使用常规的Maven项目,因此必须自己创建WEB-INF文件夹.可能是出了什么问题的线索!?
N.B. Im using a regular maven project so had to create the WEB-INF folder myself. Might be a clue to what's wrong!?
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如下...
My pom is as follows...
<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>
推荐答案
因此,我使用setWar和正确的jar使其工作.使用此代码,可以直接寻址jsp(localhost:8080/result.jsp),更重要的是,可以使用servlet(localhost:8080/hello).forward命令将其转发到jsp.这将使我能够在我的jsp中提供一些动态内容.
So I got this to work using setWar and the correct jars. Using this code it is possible to both address the jsp directly (localhost:8080/result.jsp)and more importantly forward to the jsp using servlets (localhost:8080/hello) .forward command. This will enable me to serve up some dynamic content with my jsp.
代码如下...(注意:Embedded Jetty =>不需要web.xml)
Code as follows... (NB: Embedded Jetty => no web.xml is required)
import java.io.File;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.DefaultHandler;
import org.eclipse.jetty.server.handler.HandlerList;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.webapp.WebAppContext;
public class RunHelloServlet {
public static void main(String[] args) throws Exception {
System.setProperty("DEBUG", "true");
Server server = new Server(8080);
WebAppContext webappcontext = new WebAppContext();
webappcontext.setContextPath("/");
File warPath = new File("C:/dev/workspace/JettyTest", "src/main/webapp");
webappcontext.setWar(warPath.getAbsolutePath());
HandlerList handlers = new HandlerList();
webappcontext.addServlet(new ServletHolder(new HelloServlet()), "/hello");
handlers.setHandlers(new Handler[] { webappcontext, new DefaultHandler() });
server.setHandler(handlers);
server.start();
}
public static class HelloServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public HelloServlet() {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
// logic
try {
request.getRequestDispatcher("/result.jsp").forward(request, response);
}
catch (Throwable e1) {
e1.printStackTrace();
}
}
}
}
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</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>
这篇关于从servlet到jsp的嵌入式码头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!