Jetty升级至9.3.0.M2及更高版本后JSP功能失效问题求助
你遇到的核心问题是Jetty 9.3版本开始对JSP相关构件做了重构,原有的jetty-jsp被拆分为独立的apache-jsp和apache-jstl构件,同时需要调整WebAppContext的配置来确保Jetty能正确定位JSTL的TLD文件。下面是具体的解决步骤:
1. 修正Maven依赖配置
Jetty 9.3+不再提供jetty-jsp构件,你需要替换为以下完整的JSP相关依赖,确保所有依赖版本与你的Jetty版本(比如9.3.0.M2)保持一致:
<dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-server</artifactId> <version>9.3.0.M2</version> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-servlet</artifactId> <version>9.3.0.M2</version> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-webapp</artifactId> <version>9.3.0.M2</version> </dependency> <!-- 替换jetty-jsp的核心JSP编译构件 --> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>apache-jsp</artifactId> <version>9.3.0.M2</version> </dependency> <!-- 新增Jetty整合JSTL的适配构件,包含TLD资源 --> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>apache-jstl</artifactId> <version>9.3.0.M2</version> </dependency> <!-- 标准JSTL API依赖 --> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency>
这里关键是添加apache-jstl依赖——它包含了Jetty适配JSTL所需的TLD资源和整合逻辑,之前你只添加了apache-jsp和标准JSTL,缺少这个适配层会导致Jetty找不到TLD文件。
2. 调整WebAppContext的Jar扫描配置
你之前设置的ContainerIncludeJarPattern范围太窄,需要扩展它来包含所有JSP和JSTL相关的Jar文件,修改配置代码如下:
WebAppContext waContext = new WebAppContext(webDir, "/"); waContext.setInitParameter("org.eclipse.jetty.servlet.Default.dirAllowed", "false"); waContext.setSecurityHandler(basicSecurity); // 扩展Jar扫描规则,覆盖jsp、jstl、taglibs相关Jar waContext.setAttribute( "org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern", ".*/[^/]*jsp-api.*\\.jar$|.*/[^/]*jsp.*\\.jar$|.*/[^/]*jstl.*\\.jar$|.*/[^/]*taglibs.*\\.jar$" );
这个配置会让Jetty扫描所有包含jsp、jstl、taglibs的Jar,从而定位到JSTL的TLD文件,避免解析taglib指令时出现空指针异常。
3. 检查JSP页面的Taglib指令
确保你的JSP页面使用符合JSP 2.1+规范的标签库URI,推荐使用:
<%@ taglib prefix="c" uri="http://xmlns.jcp.org/jsp/jstl/core" %> <%@ taglib prefix="fmt" uri="http://xmlns.jcp.org/jsp/jstl/fmt" %>
旧的http://java.sun.com/jsp/jstl/core虽然兼容,但新标准URI更适配现代容器环境。
空指针异常的原因说明
你遇到的NullPointerException是因为Jetty在解析JSP的taglib指令时,无法找到对应的TLD资源路径。这是由于缺少apache-jstl依赖,同时Jar扫描规则没有覆盖相关Jar,导致Jetty无法定位到JSTL的TLD文件,最终在getTldResourcePath方法中抛出空指针。
按照上述步骤配置后,Jetty 9.3.0.M2及更高版本应该能正常编译和渲染JSP页面了。
内容的提问来源于stack exchange,提问作者Stefan

