gpt4 book ai didi

java - servlet 中的 xpath 查询给出异常

转载 作者:行者123 更新时间:2023-12-01 15:19:35 26 4
gpt4 key购买 nike

我在 servlet 的 init() 方法中初始化了一个 Document 对象,并在 doPost() 方法中使用它来服务请求。

当 servlet 同时服务多个请求时,selectNodeList() xpath 查询会出现异常。异常情况如下所示:

Caused by: javax.xml.transform.TransformerException: -1
at org.apache.xpath.XPath.execute(XPath.java:331)
at org.apache.xpath.CachedXPathAPI.eval(CachedXPathAPI.java:328)
at org.apache.xpath.CachedXPathAPI.selectNodeList(CachedXPathAPI.java:255)
at org.apache.xpath.CachedXPathAPI.selectNodeList(CachedXPathAPI.java:235)
at com.pro.bb.servlets.Controller.getDataOrPeriodForReport(Controller.java:511)
... 23 more
Caused by: java.lang.ArrayIndexOutOfBoundsException: -1
at org.apache.xpath.XPathContext.pushCurrentNode(XPathContext.java:808)
at org.apache.xpath.axes.PredicatedNodeTest.acceptNode(PredicatedNodeTest.java:447)
at org.apache.xpath.axes.AxesWalker.nextNode(AxesWalker.java:409)
at org.apache.xpath.axes.WalkingIterator.nextNode(WalkingIterator.java:176)
at org.apache.xpath.axes.NodeSequence.nextNode(NodeSequence.java:320)
at org.apache.xpath.axes.NodeSequence.runTo(NodeSequence.java:474)
at org.apache.xpath.axes.NodeSequence.setRoot(NodeSequence.java:257)
at org.apache.xpath.axes.LocPathIterator.execute(LocPathIterator.java:257)
at org.apache.xpath.XPath.execute(XPath.java:308)

帮我解决这个问题。

最佳答案

CachedXPathAPI 类不是线程安全的。如果您在 servlet 中使用它,则必须手动注意同步访问(或使用多个实例)。

来自Apache Xalan-j javadoc :

Note that any particular instance of CachedXPathAPI must not be operated upon by multiple threads without synchronization; we do not currently support multithreaded access to a single DTM. Similarly, any particular instance of CachedXPathAPI must not be operated upon by multiple threads without synchronization.

关于java - servlet 中的 xpath 查询给出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11146611/

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