首页 > 【已解决】Jetty 9.3 无法使用 JSP

【已解决】Jetty 9.3 无法使用 JSP

原来我使用 jetty 9.2.17,一直都很正常;最近想使用标准的 websocket,先将 jetty 升级到 9.3.10。原来 JSP 支持使用 jetty-jsp,升级后改为 jetty 的 apache-jsp (版本与其他 jetty 组件相同,官方换了包名而已,依赖 apache 的 apache-jsp 8.0.33),Servlet 一切正常,但 JSP 都无法使用了,报错:

java.lang.NullPointerException
    at org.apache.jasper.compiler.Validator$ValidateVisitor.<init>(Validator.java:515)
    at org.apache.jasper.compiler.Validator.validateExDirectives(Validator.java:1853)
    at org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:217)
    at org.apache.jasper.compiler.Compiler.compile(Compiler.java:356)
    at org.apache.jasper.compiler.Compiler.compile(Compiler.java:336)
    at org.apache.jasper.compiler.Compiler.compile(Compiler.java:323)
    at org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:585)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:363)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:837)
    at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:583)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143)
    at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:548)
    at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:226)
    at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1180)
    at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:511)
    at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:185)
    at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1112)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:134)
    at org.eclipse.jetty.server.Server.handle(Server.java:524)
    at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:319)
    at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:253)
    at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:273)
    at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:95)
    at org.eclipse.jetty.io.SelectChannelEndPoint$2.run(SelectChannelEndPoint.java:93)
    at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.executeProduceConsume(ExecuteProduceConsume.java:303)
    at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.produceConsume(ExecuteProduceConsume.java:148)
    at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.run(ExecuteProduceConsume.java:136)
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:671)
    at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:589)
    at java.lang.Thread.run(Thread.java:745)

搜了一圈,也看了 jetty 官方的文档,尝试了 web.xml 中设置 jsp servlet,设置其他属性,增加额外的包,均无效。请问有遇到过类似问题的朋友吗?你是如何解决的。


总算解决了这个问题,参考 https://github.com/jetty-proj...

jetty 版本从 9.2.17 升级到 9.3.1x 后,原来 WebAppContext 只需简单设置 ContextPath, ResourceBase 几个就行,现在还需额外设置一些属性,另除了依赖 apache-jsp 包,还需引入 jetty-plus 包以得到 ContainerInitializer。以下是参考例子编写的代码:

    // 构建应用
    WebAppContext webapp  = new WebAppContext();
    webapp.setDescriptor   (conf);
    webapp.setContextPath  (Core.BASE_HREF);
    webapp.setResourceBase (Core.BASE_PATH);
    webapp.setParentLoaderPriority ( true );

    // JSP 相关
    webapp.setTempDirectory(temp);
    webapp.setAttribute("javax.servlet.context.tempdir", temp);
    // 下面是为解决此次问题增加的代码
    webapp.setAttribute("org.eclipse.jetty.containerInitializers" , Arrays.asList (
                       new ContainerInitializer(new JettyJasperInitializer(), null)));
    webapp.setAttribute(InstanceManager.class.getName(), new SimpleInstanceManager());
【热门文章】
【热门文章】