Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How to get the contextPath in a servlet's init-method?

Reply
Thread Tools

How to get the contextPath in a servlet's init-method?

 
 
Stephan Koser
Guest
Posts: n/a
 
      01-14-2005
Hi,

is there a way to get the context path inside the servlet's init-method?
In an URL like http://localhost/myApp/... i want to get the Attribute
/myApp.

I know, I can get ist with HttpServletRequest#getContextPath(), but in the
init method I don't have a servletRequest.
Is there another way to get it that also works with different Java
Containers?

...
bye Stephan...


 
Reply With Quote
 
 
 
 
au714@osfn.org
Guest
Posts: n/a
 
      01-14-2005
Stephan Koser wrote:
> Hi,
>
> is there a way to get the context path inside the servlet's

init-method?
> In an URL like http://localhost/myApp/... i want to get the Attribute
> /myApp.
>


public void init() throws ServletException {
ServletContext context = getServletContext();
System.out.println("Real Path = " + context.getRealPath("/"));
}

 
Reply With Quote
 
 
 
 
John C. Bollinger
Guest
Posts: n/a
 
      01-14-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> Stephan Koser wrote:
>
>>Hi,
>>
>>is there a way to get the context path inside the servlet's

>
> init-method?
>
>>In an URL like http://localhost/myApp/... i want to get the Attribute
>>/myApp.
>>

>
>
> public void init() throws ServletException {
> ServletContext context = getServletContext();
> System.out.println("Real Path = " + context.getRealPath("/"));
> }
>


That doesn't answer the question posed. The real path to the context
root does not necessarily have anything at all to do with the context
path relative to the server root.

In answer to the OP's question, it is not possible to do what you want
in general, because the same servlet may be accessible via more than one
path. The context path is partly a function of the request, which is
why it is only available from the request object. If you _really_ need
this information at init time (and you probably don't), then you can
fudge by putting it in one of the servlet's init parameters. This
probably will introduce a dependency between the webapp's deployment
descriptor and the servlet container's configuration, so do not
undertake it lightly.


John Bollinger
(E-Mail Removed)
 
Reply With Quote
 
gimme_this_gimme_that@yahoo.com
Guest
Posts: n/a
 
      01-15-2005
Would setting a parameter/value pair to the startup servlet in the
web.xml file and then using
that be a satisfactory solution?

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java/TomCat How to Configure a Servlet contextPath Rich Morey Java 9 11-01-2008 05:51 PM
Serious cache problem - Page_Load get never get called !!! Ryan Park ASP .Net 2 01-03-2005 05:52 PM
Re: use anonymous but get get an authentication box Curt_C [MVP] ASP .Net 2 09-21-2004 06:39 PM
use anonymous but get get an authentication box =?Utf-8?B?U2hhcmlm?= ASP .Net 0 09-21-2004 01:01 PM



Advertisments