Explanation of Class.getResourceAsStream and how it is set in Tomcat for a simple Java web - app?

陌路散爱 提交于 2019-12-08 12:49:28
Angular University

For 1) The classpath root in a servlet application is by specification the WEB-INF\classes folder of the jar, plus the root of all the jars in WEB-INF/lib of that WAR. Anything in those locations will be seen as the root of the classpath.

For the question on how classpaths in tomcat work, when tomcat deploys it set's the classpath in the following way: each WAR corresponds to a separate class loader which has access to WEB-INF/classes and all the jars in WEB-INF/lib.

By default if the resource searched is not found here, it will be searched for in the tomcat/lib directory. If it's not found there, it will ask the parent class loader, and so on, the explanation can be found here

If there are several web-apps deployed, each WAR will have it's own class loader pointing to it's own WEB-INF/classes and WEB-INF/lib jars.

For 2) there is not method like getClasspath(), ServletContext.getResourceAsStream() is the advised way in servlet applications for getting resources from inside the WAR. The WAR might be zipped or exploded, and this works for both, see this answer.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!