Jetty:Servlet无法转发到JSP [英] Jetty: Servlet can't forward to JSP
问题描述
我有以下问题:我正在尝试将请求从servlet转发到jsp页面,但是我得到了404.当我直接访问/sites/home.jsp时,它会加载jsp。
I have the following problem: I'm trying to forward the request from the servlet to the jsp page, but I get an 404. When I´m accessing /sites/home.jsp directly, it loads the jsp.
WebAppContext sites = new WebAppContext("src/com/example/blub/server/sites", "/sites");
ServletContextHandler weblet = new ServletContextHandler(ServletContextHandler.SESSIONS);
weblet.setContextPath("/");
weblet.addServlet(new ServletHolder(new Weblet()), "/home");
HandlerList handlers = new HandlerList();
handlers.setHandlers(new Handler[] {sites, weblet});
server.setHandler(handlers);
。
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.getRequestDispatcher("/sites/home.jsp").forward(req, resp);
}
推荐答案
这个问题经常出现,所以我创建了一个使用嵌入式Jetty并启用JSP的示例项目。
This question comes up often enough, so I created an example project of using Embedded Jetty with JSP enabled.
https://github.com/jetty-project/embedded-jetty-jsp/
加载此项投射到你最喜欢的IDE。
Load up this project into your favorite IDE.
运行 org.eclipse.jetty.demo.Main
类,然后使用你的浏览器并打开 http:// localhost:8080 /
Run the org.eclipse.jetty.demo.Main
class and then use your browser and open http://localhost:8080/
快速浏览
src / main / java / org / eclipse / jetty / demo / Main.java
包含创建/配置/启动嵌入式服务器的部分。
src/main/java/org/eclipse/jetty/demo/Main.java
contains the part that creates / configures / and starts the embedded server.
特别注意:
- JspServlet必须命名为jsp - 参见
jspServletHolder()
-
org.eclipse.jetty.containerInitializers
需要为JSP初始化程序设置 - 需要添加
ServletContainerInitializersStarter
bean - 需要添加
InstanceManager
引用 - 正确的
javax.servlet.context.tempdir
需要创建 - 将
org.apache.jasper.compiler.disablejsr199
设置为false
使用标准JavaC编译器 - Context的类加载器不能是系统类加载器。 - 参见
getUrlClassLoader()
- DefaultServlet必须命名为default - 参见
defaultServletHolder()
- The JspServlet must be named "jsp" - see
jspServletHolder()
- The
org.eclipse.jetty.containerInitializers
needs to be setup for the JSP initializers - The
ServletContainerInitializersStarter
bean needs to be added - The
InstanceManager
reference needs to be added - A proper
javax.servlet.context.tempdir
needs to be created - Set
org.apache.jasper.compiler.disablejsr199
tofalse
to use the standard JavaC compiler - The Classloader for the Context must not be a System Classloader. - see
getUrlClassLoader()
- The DefaultServlet must be named "default" - see
defaultServletHolder()
src / main / java / com / acme / DateServlet.java
是如何从Servlet转发到JSP的示例。
src/main/java/com/acme/DateServlet.java
is an example of how to forward to a JSP from a Servlet.
DateServlet是映射到<$ c的路径规范Main.java中的$ c> / date /
The DateServlet is mapped to path spec of /date/
in Main.java
所以一旦你点击 http:// localhost:8080 / date /
请求将命中servlet,而servlet又转发到 /test/tag2.jsp
So once you hit http://localhost:8080/date/
the request will hit the servlet, which in turn forwards to to /test/tag2.jsp
这篇关于Jetty:Servlet无法转发到JSP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!