gpt4 book ai didi

java - 是否可以在 Tomcat 8 中禁用对 JSP 2.3 引用静态字段和方法的支持

转载 作者:搜寻专家 更新时间:2023-11-01 02:05:30 25 4
gpt4 key购买 nike

是否可以关闭作为统一表达式语言 3.0 的一部分添加的 Tomcat 8 中对引用静态字段和方法的支持。

我们的应用程序中有 ~4K JSP,其中有许多 ${undefined}(未指定范围)表达式,迁移到 Tomcat 8 会导致性能显着下降,因为这些表达式的计算是合法的“空”值。我们不再将 JSP 技术用于较新的页面,但遗留技术不会很快消失。

有问题的代码在 javax.servlet.el.ScopedAttributeELResolver 类中,它试图解析来自 ImportHandler 的表达式,该表达式进行许多 Class.forName 查找,这主要是由于 ClassNotFoundException 而失败。

@Override
public Object getValue(ELContext context, Object base, Object property) {
if (context == null) {
throw new NullPointerException();
}

Object result = null;

if (base == null) {
context.setPropertyResolved(base, property);
if (property != null) {
String key = property.toString();
PageContext page = (PageContext) context
.getContext(JspContext.class);
result = page.findAttribute(key);

if (result == null) {
// This might be the name of an imported class
ImportHandler importHandler = context.getImportHandler();
if (importHandler != null) {
Class<?> clazz = importHandler.resolveClass(key);
if (clazz != null) {
result = new ELClass(clazz);
}
if (result == null) {
// This might be the name of an imported static field
clazz = importHandler.resolveStatic(key);
if (clazz != null) {
try {
result = clazz.getField(key).get(null);
} catch (IllegalArgumentException | IllegalAccessException |
NoSuchFieldException | SecurityException e) {
// Most (all?) of these should have been
// prevented by the checks when the import
// was defined.
}
}
}
}
}
}
}

return result;
}

更新

针对 Tomcat 8 打开了一个错误 - performance problems when using scopeless optional attributes ,这是关闭的,因为不会修复。我认为他们可能会添加一些属性来禁用性能消耗代码,但现在他们不会,因为:

  • 这将是特定于 Tomcat 的
  • 它必须是一个系统属性,因为这是一个规范类
  • 它不能应用于每个应用程序 - 它会影响在该实例上运行的所有应用程序

请指教谢谢

最佳答案

禁用新行为的一种方法是利用 Tomcat 的类加载机制。公共(public)类加载器包含额外的类,这些类对 Tomcat 内部类和所有 Web 应用程序都是可见的。此类加载器搜索的位置由 $CATALINA_BASE/conf/catalina.properties 中的 common.loader 属性定义。默认设置将按照列出的顺序搜索以下位置:

  1. 在 $CATALINA_BASE/lib 中解压类和资源
  2. $CATALINA_BASE/lib 中的 JAR 文件
  3. 在 $CATALINA_HOME/lib 中解压类和资源
  4. $CATALINA_HOME/lib 中的 JAR 文件

我用一个类创建了一个新的 jar:javax.servlet.jsp.el.ScopedAttributeELResolver,除了 getValue 执行静态解析的方法(tomcat 代码在 Apache 2 许可下,因此补丁是合法的)。 jar 放在 $CATALINA_BASE/lib 文件夹下。

新方法:

    @Override
public Object getValue(ELContext context, Object base, Object property) {
if (context == null) {
throw new NullPointerException();
}

Object result = null;

if (base == null) {
context.setPropertyResolved(base, property);
if (property != null) {
String key = property.toString();
PageContext page = (PageContext) context
.getContext(JspContext.class);
result = page.findAttribute(key);

// if (result == null) {
// // This might be the name of an imported class
// ImportHandler importHandler = context.getImportHandler();
// if (importHandler != null) {
// Class<?> clazz = importHandler.resolveClass(key);
// if (clazz != null) {
// result = new ELClass(clazz);
// }
// if (result == null) {
// // This might be the name of an imported static field
// clazz = importHandler.resolveStatic(key);
// if (clazz != null) {
// try {
// result = clazz.getField(key).get(null);
// } catch (IllegalArgumentException | IllegalAccessException |
// NoSuchFieldException | SecurityException e) {
// // Most (all?) of these should have been
// // prevented by the checks when the import
// // was defined.
// }
// }
// }
// }
// }
}
}

return result;
}

这个类将被加载而不是原来的,绕过性能问题。

优点:

  • 通过简单的 jar 删除轻松回滚

缺点:

  • 每次 Tomcat 升级都需要维护

关于java - 是否可以在 Tomcat 8 中禁用对 JSP 2.3 引用静态字段和方法的支持,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35475980/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com