On Thu, March 16, 2006 10:52 am, Wojciech Ciesielski said:
>
>>>
>>> I need to find URL to my application
>>> (http://myhost:8080/myAppContext) from within init() method of struts
>>> plugin (implementing PlugIn interface). Any ideas how can I do this
>>> programatically without specifying it explicitly in one of
>>> application configuration files?
>> You can't do it, in any part of your web-app! But what do you need
>> your application URL for?
>> Ciao
>> Antonio

Well, that certainly *is* information you can get within a webapp... it's
fairly easy to construct that URL from with an Action for instance.  From
a plug-in though, I'm not so sure (interestingly, this question comes up
frequently... would make for a good Wiki post...)

Let's walk the object hierarchy and see if we can't find any help...
Remember we're trying to construct http://myhost:8080/myAppContext
dynamically...

PlugIn:
void init(ActionServlet servlet, ModuleConfig config)

Ok, we have an ActionServlet instance... ActionServlet extends from
HttpServlet... HttpServlet exposes a getServletContext() method...

ServletContext:
getServletContextName()

Ah, ok, we have the myAppContext portion at least!  Can we get host name? 
Well, generically we can:

try {
  InetAddress addr = InetAddress.getLocalHost();
  String hostname = addr.getHostName();
} catch (UnknownHostException e) { }

Ok, so now we need the port and the protocol... and I'm stumped :)  I
don't think you could get the protocol without making a request, and the
port I would think you would *have* to know before-hand anyway.  Can you
use config params for those two pieces of information?  If it's always
HTTP, then you just need to get the port, but again, that seems like
something that would be OK as an init param.

Frank

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to