> I got a new web application, develop,
> and i am looking to detect 2 things about it in runtime.
>
> 1.) i want to detect the context the application was deployed under.
First you'll need to get the ServletContext. There are serveral ways,
I'll explain two.
You can implement a ServletContextListener. It has a contextInitialized'
method. The ServletContextEvent method has a getServletContext() method.
You'll need to install the contextListener in your web.xml. There is
enough documentation about that.
Another way is to implement a servlet, it has a init method which passes
a ServletConfig object. That has a getServletContext() method, which you
can use.
Now you have the ServletContext. It has a getServerInfo() method, which
you can invoke to see which application server you are running on.
> 2.) And second i want to detect the folder it was deployed in.
You can't really do that reliably, since a web-app might be a war file,
there is no requirement that the web-app is actually on the physical
file-system.
However, with the ServletConfig you have a method getRealPath(). You can
use getRealPath("/") to get the root of your webapplication, but this
method might return null in some environments / configurations.
Vincent
Felix Roberto - 02 Aug 2006 14:26 GMT
> > I got a new web application, develop,
> > and i am looking to detect 2 things about it in runtime.
[quoted text clipped - 27 lines]
>
> Vincent
Thax for the answer vincent, interesting, the idea, you gave me
helped me out with another problem.
but i think you did not understand my question.
i got varios versions off a application running on one same server.
problem is i need to detect under wich context they are running,
exameple
http://127.0.0.1/app1/
http://127.0.0.1/app2/
http://127.0.0.1/app3/
i just need a way to know that the first instance of the application is
running on app1
that the second is on app2 and the 3rd on app3
at first instance i did configure it the deployment descriptor, and got
it with a enviromental context :
InitialContext().lookup("java:/comp/env")
but i want to stop configuring these little things by hand.
PS. this is all for a web-application framework we develop internally
and are using.