且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Jetty:Servlet无法转发到JSP

更新时间:2023-12-04 14:49:46

这个问题经常出现,所以我创建了一个使用嵌入式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 to false 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是映射到 / 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