File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Full URL of a webapp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Full URL of a webapp" Watch "Full URL of a webapp" New topic

Full URL of a webapp

Raghuveer Kumarakrishnan
Ranch Hand

Joined: Mar 13, 2005
Posts: 32
How do you get the full URL of a webapp?

For example if the webapp is on a server with ip
on port 8080 and the context path is /somewebapp
then I want an output string

Is there a way to do this at startup(as opposed to using a ServletRequest)?

I can get the ip address by using

I am stumped about the rest, any ideas?

Thanks in advance

Raghu<br />SCJP 1.4<br />SCWCD 1.4<br />SCBCD 1.3
Craig Bayley
Ranch Hand

Joined: Sep 27, 2007
Posts: 46
When you say "at startup" can you be more specific about what point you need this...Do you mean server startup? Do you mean when the webapp is started? What do you need it for? Someone may be able to give more advice with a little more detail. I've only ever done this kind of thing from an HTTPRequest, so I'm not sure what to suggest.
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63862

What part are you missing? The context path? You can get that from the servlet context. I don;t know about the port.

[Asking smart questions] [About Bear] [Books by Bear]
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

The server might be getting HTTP and HTTPS requests, so that would mean different ports on different requests. Therefore you can't get "the port" unless you have a request at hand, and you can ask what port it came into.

Likewise you can't tell whether the request was sent to the server using its name or its IP address. And a server can be configured to have more than one name. Therefore you can't get the host part of the URL either, unless you have a request at hand.
Raghuveer Kumarakrishnan
Ranch Hand

Joined: Mar 13, 2005
Posts: 32
Thanks for the replies.

This is the requirement,I want the webapplication to generate its path along with context and port and report it to an another admin application (the reporting is done through xml over http) . The admin module stores this info in the db.

I have a singleton that I initialize when the context is created and starts a thread to communicate with the admin app, this thread carries the report to the admin app which among other things tells the full path. So I was looking to add this path generating feature to that singleton.

On looking at the servlet api I too concluded that the request is needed to get the port but I wanted to make sure I am not missing something obvious.
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

It sounds like you're having a hard time understanding Paul's post.
Try re-reading it again.

The bottom line is that the application doesn't and can't know the the full URL when it starts up. It won't find this out until it actually gets a request.

Furthermore, it's entirely possible for an application to be accessed from more than one domain or from the IP alone.
It's not a one to one relationship.

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
I agree. Here's the link:
subject: Full URL of a webapp
It's not a secret anymore!