You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Jing Zhou <ji...@netspread.com> on 2003/07/31 08:17:30 UTC

Could one figure out the context path in Servlet.init() method?

Hi,

Maybe I overlook something. We can get context path
from HttpServletRequest.getContextPath() at request time.
But could one get the same path when the servlet is
initialized? Or simply say why we do not have an API like
ServletContext.getContextPath()?

Jing
Netspread Carrier
http://www.netspread.com

Re: Could one figure out the context path in Servlet.init() method?

Posted by Jing Zhou <ji...@netspread.com>.
Thanks a lot! We do have a configuration decision based on
a term, we call it *context path* in the database. We use it
to uniquely identify a web application. Through this discussion,
we could call it whatever as long as it helps us to find information
about a particular web application in the database. Using
a context init parameter to initialize the term is the way to go.

Jing


----- Original Message ----- 
From: "Craig R. McClanahan" <cr...@apache.org>
To: "Jing Zhou" <ji...@netspread.com>
Cc: "Struts Users Mailing List" <st...@jakarta.apache.org>
Sent: Thursday, July 31, 2003 2:51 PM
Subject: Re: Could one figure out the context path in Servlet.init() method?


>
>
> On Thu, 31 Jul 2003, Jing Zhou wrote:
>
> > Date: Thu, 31 Jul 2003 13:17:39 -0500
> > From: Jing Zhou <ji...@netspread.com>
> > To: Craig R. McClanahan <cr...@apache.org>,
> >      Struts Users Mailing List <st...@jakarta.apache.org>
> > Subject: Re: Could one figure out the context path in Servlet.init()
> >     method?
> >
> > I see. I looked through the Servlet Spec 2.3 and no restrictions
> > are found to have multiple context paths mapped to one web
> > application. But how such capability is utilized in a servlet
> > container? I do not find information on how to map multiple
> > context paths to one web application. Is this container vendor
> > specific thing?
> >
>
> Since it is not a spec thing, container support for this feature would
> definitely be container specific, as would configuration.
>
> Tomcat, for example, does not support it directly for context paths (you
> can point two <Context> elements at the same directory, but you get two
> different webapp instances), but it does allow an app to have multiple
> hostnames, courtesy of the fact that you can have an <Alias> directive for
> the owning <Host>.  This is mostly used so that things like the following
> two URLs:
>
>   http://www.mycompany.com/myapp/
>
>   http://mycompany.com/myapp/
>
> resolve to the same webapp.  But the principle is the same -- it would not
> be reasonable for a servlet's init() method to ask "what is my server
> name?", because there is more than one.
>
> > Another thought that is surprising me is that a web application
> > could not assume its context paths are *constants* in the
> > application life cycle. Is this assumption valid in general?
> > (Of course one should not change context paths very often
> > even if they could be changed.)
>
> Again server dependent, especially on servers where you can dynamically
> install and remove alias directives.
>
> Portably written applications should make no assumptions about what
> context path they are executing under - that decision should be up to the
> person installing the app instead.  That's why, for example, Struts tags
> and methods all focus on context-relative paths; they work no matter what
> the context path is.  If you've got configuration decisions to make based
> on what the actual context path is (as it sounds like you do), it might be
> better to externalize that stuff into a separate context init parameter,
> which therefore would be available at startup.
>
> >
> > Jing
>
> Craig
>
>
> >
> > ----- Original Message -----
> > From: "Craig R. McClanahan" <cr...@apache.org>
> > To: "Struts Users Mailing List" <st...@jakarta.apache.org>; "Jing
> > Zhou" <ji...@netspread.com>
> > Cc: <sr...@apache.org>
> > Sent: Thursday, July 31, 2003 10:57 AM
> > Subject: Re: Could one figure out the context path in Servlet.init()
method?
> >
> >
> > >
> > >
> > > On Thu, 31 Jul 2003, Jing Zhou wrote:
> > >
> > > >
> > > > I am wondering if there is a way to get the context path without
> > > > using init parameters during the initialization of a servlet.
> > > > Any clues? We have ServletContext.getRealPath(), but not
> > > > the ServletContext.getContextPath(). Why can't the ServletContext
> > > > tell me its context path?
> > > >
> > >
> > > You are assuming that a web applicaton only has one context path.
That
> > > does not have to be the case.
> > >
> > > > Jing
> > >
> > > Craig
> > >
> >
> >
>


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: Could one figure out the context path in Servlet.init() method?

Posted by "Craig R. McClanahan" <cr...@apache.org>.

On Thu, 31 Jul 2003, Jing Zhou wrote:

> Date: Thu, 31 Jul 2003 13:17:39 -0500
> From: Jing Zhou <ji...@netspread.com>
> To: Craig R. McClanahan <cr...@apache.org>,
>      Struts Users Mailing List <st...@jakarta.apache.org>
> Subject: Re: Could one figure out the context path in Servlet.init()
>     method?
>
> I see. I looked through the Servlet Spec 2.3 and no restrictions
> are found to have multiple context paths mapped to one web
> application. But how such capability is utilized in a servlet
> container? I do not find information on how to map multiple
> context paths to one web application. Is this container vendor
> specific thing?
>

Since it is not a spec thing, container support for this feature would
definitely be container specific, as would configuration.

Tomcat, for example, does not support it directly for context paths (you
can point two <Context> elements at the same directory, but you get two
different webapp instances), but it does allow an app to have multiple
hostnames, courtesy of the fact that you can have an <Alias> directive for
the owning <Host>.  This is mostly used so that things like the following
two URLs:

  http://www.mycompany.com/myapp/

  http://mycompany.com/myapp/

resolve to the same webapp.  But the principle is the same -- it would not
be reasonable for a servlet's init() method to ask "what is my server
name?", because there is more than one.

> Another thought that is surprising me is that a web application
> could not assume its context paths are *constants* in the
> application life cycle. Is this assumption valid in general?
> (Of course one should not change context paths very often
> even if they could be changed.)

Again server dependent, especially on servers where you can dynamically
install and remove alias directives.

Portably written applications should make no assumptions about what
context path they are executing under - that decision should be up to the
person installing the app instead.  That's why, for example, Struts tags
and methods all focus on context-relative paths; they work no matter what
the context path is.  If you've got configuration decisions to make based
on what the actual context path is (as it sounds like you do), it might be
better to externalize that stuff into a separate context init parameter,
which therefore would be available at startup.

>
> Jing

Craig


>
> ----- Original Message -----
> From: "Craig R. McClanahan" <cr...@apache.org>
> To: "Struts Users Mailing List" <st...@jakarta.apache.org>; "Jing
> Zhou" <ji...@netspread.com>
> Cc: <sr...@apache.org>
> Sent: Thursday, July 31, 2003 10:57 AM
> Subject: Re: Could one figure out the context path in Servlet.init() method?
>
>
> >
> >
> > On Thu, 31 Jul 2003, Jing Zhou wrote:
> >
> > >
> > > I am wondering if there is a way to get the context path without
> > > using init parameters during the initialization of a servlet.
> > > Any clues? We have ServletContext.getRealPath(), but not
> > > the ServletContext.getContextPath(). Why can't the ServletContext
> > > tell me its context path?
> > >
> >
> > You are assuming that a web applicaton only has one context path.  That
> > does not have to be the case.
> >
> > > Jing
> >
> > Craig
> >
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: Could one figure out the context path in Servlet.init() method?

Posted by Jing Zhou <ji...@netspread.com>.
I see. I looked through the Servlet Spec 2.3 and no restrictions
are found to have multiple context paths mapped to one web
application. But how such capability is utilized in a servlet
container? I do not find information on how to map multiple
context paths to one web application. Is this container vendor
specific thing?

Another thought that is surprising me is that a web application
could not assume its context paths are *constants* in the
application life cycle. Is this assumption valid in general?
(Of course one should not change context paths very often
even if they could be changed.)

Jing

----- Original Message ----- 
From: "Craig R. McClanahan" <cr...@apache.org>
To: "Struts Users Mailing List" <st...@jakarta.apache.org>; "Jing
Zhou" <ji...@netspread.com>
Cc: <sr...@apache.org>
Sent: Thursday, July 31, 2003 10:57 AM
Subject: Re: Could one figure out the context path in Servlet.init() method?


>
>
> On Thu, 31 Jul 2003, Jing Zhou wrote:
>
> >
> > I am wondering if there is a way to get the context path without
> > using init parameters during the initialization of a servlet.
> > Any clues? We have ServletContext.getRealPath(), but not
> > the ServletContext.getContextPath(). Why can't the ServletContext
> > tell me its context path?
> >
>
> You are assuming that a web applicaton only has one context path.  That
> does not have to be the case.
>
> > Jing
>
> Craig
>


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: Could one figure out the context path in Servlet.init() method?

Posted by "Craig R. McClanahan" <cr...@apache.org>.

On Thu, 31 Jul 2003, Jing Zhou wrote:

>
> I am wondering if there is a way to get the context path without
> using init parameters during the initialization of a servlet.
> Any clues? We have ServletContext.getRealPath(), but not
> the ServletContext.getContextPath(). Why can't the ServletContext
> tell me its context path?
>

You are assuming that a web applicaton only has one context path.  That
does not have to be the case.

> Jing

Craig

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: Could one figure out the context path in Servlet.init() method?

Posted by Jing Zhou <ji...@netspread.com>.
>From within the Servlet.init() method, one could
get the ServletContext by calling getServletConfig().getServletContext().
My question is could we figure out the context path
inside the Servlet.init() method?

The ServletContextListener interface could provide
the ServletContext, but not the context path as what we
could get from the call HttpServletRequest.getContextPath().

I am wondering if there is a way to get the context path without
using init parameters during the initialization of a servlet.
Any clues? We have ServletContext.getRealPath(), but not
the ServletContext.getContextPath(). Why can't the ServletContext
tell me its context path?

Jing


----- Original Message ----- 
From: "Steve Raeburn" <sr...@apache.org>
To: "Struts Users Mailing List" <st...@jakarta.apache.org>
Sent: Thursday, July 31, 2003 2:07 AM
Subject: RE: Could one figure out the context path in Servlet.init() method?


> The ServletContextListener interface has a callback method  -
> contextInitialized() - that passes in a ServletContextEvent parameter. You
> can get the context from getServletContext() method of
ServletContextEvent.
>
> This was added in Servlet 2.3 spec.
>
> Steve
>
> > -----Original Message-----
> > From: Jing Zhou [mailto:jing@netspread.com]
> > Sent: July 30, 2003 11:18 PM
> > To: Struts Users Mailing List
> > Subject: Could one figure out the context path in Servlet.init() method?
> >
> >
> > Hi,
> >
> > Maybe I overlook something. We can get context path
> > from HttpServletRequest.getContextPath() at request time.
> > But could one get the same path when the servlet is
> > initialized? Or simply say why we do not have an API like
> > ServletContext.getContextPath()?
> >
> > Jing
> > Netspread Carrier
> > http://www.netspread.com
> >
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


RE: Could one figure out the context path in Servlet.init() method?

Posted by Steve Raeburn <sr...@apache.org>.
The ServletContextListener interface has a callback method  -
contextInitialized() - that passes in a ServletContextEvent parameter. You
can get the context from getServletContext() method of ServletContextEvent.

This was added in Servlet 2.3 spec.

Steve

> -----Original Message-----
> From: Jing Zhou [mailto:jing@netspread.com]
> Sent: July 30, 2003 11:18 PM
> To: Struts Users Mailing List
> Subject: Could one figure out the context path in Servlet.init() method?
>
>
> Hi,
>
> Maybe I overlook something. We can get context path
> from HttpServletRequest.getContextPath() at request time.
> But could one get the same path when the servlet is
> initialized? Or simply say why we do not have an API like
> ServletContext.getContextPath()?
>
> Jing
> Netspread Carrier
> http://www.netspread.com
>



---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


RE: Path Finder

Posted by Steve Raeburn <sr...@apache.org>.
Place the <html:base/> tag in the 'head' section of your JSP. Relative paths
will be then be calculated based on the location of your JSP.

Alternatively, use the <html:rewrite> tag to rewrite the value of the URL as
a context-relative path.
  <link TITLE="risk style" REL="stylesheet" HREF="<html:rewrite
page="/inc/risk.css"/>" TYPE="text/css">

Steve

> -----Original Message-----
> From: Altug B. Altintas [mailto:altuga@riskturk.com]
> Sent: July 30, 2003 11:35 PM
> To: 'Struts Users Mailing List'
> Subject: Path Finder
>
>
> Hi,
>
> My Jsp page include the followings;
>
>             <link TITLE="risk style"  REL="stylesheet"
> HREF="../inc/risk.css" TYPE="text/css">
> 	<script  LANGUAGE="JavaScript1.2"
> SRC="../js/Calendar1-82.js"></SCRIPT>
> 	<script  LANGUAGE="JavaScript1.2"
> SRC="../js/Validate.js"></SCRIPT>
> 	<script  LANGUAGE="JavaScript1.2"
> SRC="../js/functions.js"></SCRIPT>
>
> When i call this JSP page there is no problem,
> http://localhost:8080/engine-struts/jsp/yield/verigirisi-struts.jsp
>
> CSS file applied to this page  but when  an error occurs, it goes
> valition pages (struts forwards it).
>
> http://localhost:8080/engine-struts/hesapla.do
>
> In this validation page i saw that my css file is not applied to that
> page. CSS file  is not applied because it doesn't find "../inc/risk.css"
>
> i try all the combination but i couldn't solve it..
>
> Any idea ?
>
> Regards.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org
>
>
>



---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Path Finder

Posted by "Altug B. Altintas" <al...@riskturk.com>.
Hi, 

My Jsp page include the followings;

            <link TITLE="risk style"  REL="stylesheet"
HREF="../inc/risk.css" TYPE="text/css">
	<script  LANGUAGE="JavaScript1.2"
SRC="../js/Calendar1-82.js"></SCRIPT>
	<script  LANGUAGE="JavaScript1.2"
SRC="../js/Validate.js"></SCRIPT>
	<script  LANGUAGE="JavaScript1.2"
SRC="../js/functions.js"></SCRIPT>

When i call this JSP page there is no problem,
http://localhost:8080/engine-struts/jsp/yield/verigirisi-struts.jsp

CSS file applied to this page  but when  an error occurs, it goes
valition pages (struts forwards it).

http://localhost:8080/engine-struts/hesapla.do

In this validation page i saw that my css file is not applied to that
page. CSS file  is not applied because it doesn't find "../inc/risk.css"

i try all the combination but i couldn't solve it.. 

Any idea ? 

Regards.


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org