Java Web Application Deployment Context Detection.

F

Felix Roberto

Good evening, to all,

thax for letting me join youre group,
for me its a plesure.

i joined cause i need a good support group,
to help me with the development of my work,
and i have a few un-answered questions y would like to ask.

Well here is my first question.

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.

2.) And second i want to detect the folder it was deployed in.

rigth now these 2 elements i am configuring it by hand, but now
i would like the application to detect them by it self.

PS. Sorry for my bad english
 
V

Vincent van Beveren

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
 
F

Felix Roberto

Vincent said:
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.


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

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top