Could not resolve XML resource [null] with public ID [null]

/ 默认分类 / 没有评论 / 771浏览

现象

项目启动运行时发现报以下错误

严重: Parse error in application web.xml file at jndi:/localhost/WEB-INF/web.xml
java.io.FileNotFoundException: Could not resolve XML resource [null] with public ID [null], system ID [webxml/web_dataclient.xml] and base URI [jndi:/localhost/WEB-INF/web.xml] to a known, local entity.
	at org.apache.tomcat.util.descriptor.LocalResolver.resolveEntity(LocalResolver.java:154)
	at com.sun.org.apache.xerces.internal.util.EntityResolver2Wrapper.resolveEntity(EntityResolver2Wrapper.java:176)
	at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.resolveEntityAsPerStax(XMLEntityManager.java:991)
	at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.startEntity(XMLEntityManager.java:1206)
	at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEntityReference(XMLDocumentFragmentScannerImpl.java:1908)
	at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3067)
	at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:606)
	at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)
	at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:848)
	at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777)

原因

servlet很多,方便管理希望能拆分文件 在web.xml文件里webapp标签上方加上

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app
        [<!ENTITY dataclient SYSTEM  "webxml/web_dataclient.xml">
         <!ENTITY document   SYSTEM  "webxml/web_document.xml">
         <!ENTITY project SYSTEM  "webxml/web_project.xml">
        ]>  

我测试了一下在tomcat 6下面这个方式确实是可以的,但是在tomcat7下面就报错了。

java.io.FileNotFoundException: Could not resolve XML resource [null] with public ID [null], system ID [webxml/web_dataclient.xml] and base URI [jndi:/localhost/WEB-INF/web.xml] to a known, local entity.

然后我又网上搜了一下,得知tomcat 7.0.52开始的版本才会出这个问题,是因为安全的考虑tomcat 7.0.52开始的版本把xmlBlockExterna属性默认为true,要解决这个问题把xmlBlockExterna设成false。

设置前

<Context>
    <!-- Default set of monitored resources -->
    <WatchedResource>WEB-INF/web.xml</WatchedResource>

设置后

<Context xmlBlockExternal="false">
    <!-- Default set of monitored resources -->
    <WatchedResource>WEB-INF/web.xml</WatchedResource>