Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find the URL of my web service?

Tags:

java

url

service

I know this might seem a stupid question but I am just not able to find any information regarding this question. I have a java web service (generated using NetBeans) and inside the web service class I would like to know the URL at which the web service was deployed. For example, if I am deploying the web service on my local glassFish server, the web service is available at "http://localhost:8080/MyService/" where "MyService" is the name of my service. The reason I need to know this URL is because my web service generates some files that I need to make available at this URL. For example, the web service call returns a URL "http://localhost:8080/MyService/report.html" I have found some links about "WebServiceContext" but I am not able to get the URL at which my web service is running.

Edited

To clarify: Inside MyWebService.java class I want to find out the URL at which my web service was deployed (in this case, my web service is running at "http://localhost:8080/MyService/", but once it is deployed on a production server, this URL will change)

like image 321
Johnny Avatar asked Feb 03 '11 22:02

Johnny


2 Answers

Easier in my opinion, for example:

@GET
public URI redirectSendMail(@Context UriInfo ui) {
  return ui.getBaseUri();
}

If we want to send a String back to the client, indicating the exact path of some resource, we might want something like this.

@GET
public String getResourcePath(@Context UriInfo ui) {
  return ui.getAbsolutePath();
}
like image 77
Rozgeboy Avatar answered Sep 28 '22 16:09

Rozgeboy


If you are asking how to find the hostname (e.g. 'localhost' or 'www.example.com') that your servlet container is listening to you have a few options:

  • Add a configuration point that is set at deployment time (e.g. config file, system property)
  • Look into if your servlet container exposes any 'virtual host' configuration via JMX or read its config files (e.g. tomcat hosts)
  • Find the IP Address of the server and do a DNS lookup to get its configured hostname
  • Inspect the 'Host' header of the incoming HttpServletRequest

    String hostname = request.getRequestHeader("Host");

like image 27
Uriah Carpenter Avatar answered Sep 28 '22 16:09

Uriah Carpenter