且构网

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

码头:Servlet无法转发到JSP

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

这个问题经常出现,因此我创建了一个在启用JSP的情况下使用Embedded Jetty的示例项目.

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()
  • 需要为JSP初始化程序设置 org.eclipse.jetty.containerInitializers
  • 需要添加 ServletContainerInitializersStarter bean
  • 需要添加 InstanceManager 参考
  • 需要创建一个正确的 javax.servlet.context.tempdir
  • org.apache.jasper.compiler.disablejsr199 设置为 false 以使用标准JavaC编译器
  • 上下文的类加载器不能是系统类加载器.-请参见 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是

The DateServlet is mapped to path spec of /date/ in Main.java

因此,一旦您点击 http://localhost:8080/date/,该请求就会进入servlet,该servlet进而转发至

So once you hit http://localhost:8080/date/ the request will hit the servlet, which in turn forwards to to /test/tag2.jsp