简体   繁体   中英

How to know the path of the war of the current application in tomcat

I've a tomcat (with JSF) application. I want to know the path of the war of the current application.

I've tried the following code to know where tomcat is ((ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext()).getRealPath("")

However, after I add antiResourceLocking="true" to the context definition, the previous command is returning a temporal directory.

I've been debugging, and I see that in the

((ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext())

There is a variable named context with one attribute named docBase, which contains the info that I need. However I can't get this by any way.

The reason of this is: I'm doing an auto-update system, so I need to get the current war in order to apply some patches to it. Therefore I need the current war file path.

要么不可能,要么没人知道如何解决这个问题。

Honestly I have to say this is not possible, or only with some very fault-prone hack like this

I presume that the real class of FacesContext.getCurrentInstance().getExternalContext().getContext() id in this case org.apache.catalina.core.StandardContext

So you may acces docBase like this

javax.faces.context.ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
if(externalContext instanceof org.apache.catalina.core.StandardContext) {
   String path = ((org.apache.catalina.core.StandardContext)externalContext .getContext()).getDocBase();
}

But this this is in my opinion not a solution as you're highly dependend on tomcat's internals. Following issues remain: * you have no guarantee that it's really an instance of StandardContext * if this works for a given tomcat version, it may stop working without announcement in future * in any case it does not work on any other app server

If just worte done this answer as you have discover by debuging this field. In general it's a very bad practice to debug some code you access though an API (here servlet API) and then find a way to access som internals not properly provided by this API, even if it's theoretically possible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM