You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Randy Layman <ra...@aswethink.com> on 2001/08/08 17:30:16 UTC

RE: Problems with

	The answer really lies in how the spec indicates that instances of
servlets are supposed to work.  For each seperate URL pattern that a servlet
responds to, a different instance is created.  Each instance receives its
own (seperately defeined) set of init-parameters.

	The spec never specifies the /servlet/className and this feature is
provided by Tomcat only because so many people want the feature.  These
servlets (created by the servlet invoker) are a seperate instance from your
URL pattern defined servlet, and therefore has a different set of
parameters.

	If you really wanted this to work, the one workaround that I can
think of right now it to set static variables in the servlet from the init
method.  Then you would need to make sure that only the correct servlet set
the init parameters (like pass a parameter named "GoodSet" and only if that
parameter is defined with a specificed value would you actuall set the
static variables).

	Randy

> -----Original Message-----
> From: Claes Holmerson [mailto:claes.holmerson@pipechain.com]
> Sent: Wednesday, August 08, 2001 11:51 AM
> To: 'tomcat-user@jakarta.apache.org'
> Subject: Problems with <init-param>
> 
> 
> Hello!
> 
> I can not get the init-parameters that I specify in web.xml to work
> correctly. When I invoke the servlet as 
> 
> http://localhost:8080/pipechain/servlet/se.masystem.pipeline.w
> eb.servlet.Log
> inServlet 
> 
> then in init(ServletConfig):
> 
> config.getInitParameter("propertyfile");
> 
> returns null, but if I invoke the servlet with 
> 
> http://localhost:8080/pipechain/login
> 
> it works! Should not these urls be equivalent? (Tomcat 3.2.2)
> 
> /Claes 
> 
> (please cc me as I don't subscribe to the list)
> 
> 
> 
> Below is the relevant fragment from web.xml:
> 
>     <servlet>
>         <servlet-name>
>             Login
>         </servlet-name>
>         <servlet-class>
>             se.masystem.pipeline.web.servlet.LoginServlet
>         </servlet-class>
>         <init-param>
>             <param-name>propertyfile</param-name>
>             <param-value>start.properties</param-value>
>         </init-param>
>     </servlet>
> 
>     <servlet-mapping>
>         <servlet-name>
>             Login
>         </servlet-name>
>         <url-pattern>
>             /login
>         </url-pattern>
>     </servlet-mapping>
> 
> 
> 
> ----------------------------------------------------------------------
> Claes Holmerson
> PipeChain AB
> Porfyrvägen 14		+46 (0)46 32 52 42     office phone
> SE-224 78 LUND		+46 (0)709 89 52 42   mobile phone
> Sweden			+46 (0)46 15 10 74     office fax
>                            
> http://www.pipechain.com/
> --------------------------------------------------------------
> ----------
> 

RE: Problems with

Posted by "Craig R. McClanahan" <cr...@apache.org>.
Per the spec, servlet initialization parameters work like this:

* Servlet initialization parameters are tied to <servlet> definitions
  in web.xml

* The container uses the <servlet-mapping> settings in web.xml to map
  whatever request URI comes in to a particular <servlet> definition.

* Therefore, a servlet can only see initialization parameters if it
  has been explicitly defined *and* mapped.

* The way that Tomcat does anonymous servlet invoking ("/servlet/{name}")
  is by defining the "invoker" servlet, and then mapping the "/servlet/*"
  URL pattern to it.  Therefore, according to the above rules, any
  servlet executed through the invoker servlet would see the
  initialization parameters for the invoker servlet itself.

As Randy pointed out, invoker service is totally outside the
specification, so you cannot expect identical behavior on all
containers.  The safe thing to do is use explicit <servlet> definitions
and <servlet-mapping> mappings, which will always work as described above.

Craig McCLanahan

  


On Wed, 8 Aug 2001, Randy Layman wrote:

> 
> 	The answer really lies in how the spec indicates that instances of
> servlets are supposed to work.  For each seperate URL pattern that a servlet
> responds to, a different instance is created.  Each instance receives its
> own (seperately defeined) set of init-parameters.
> 
> 	The spec never specifies the /servlet/className and this feature is
> provided by Tomcat only because so many people want the feature.  These
> servlets (created by the servlet invoker) are a seperate instance from your
> URL pattern defined servlet, and therefore has a different set of
> parameters.
> 
> 	If you really wanted this to work, the one workaround that I can
> think of right now it to set static variables in the servlet from the init
> method.  Then you would need to make sure that only the correct servlet set
> the init parameters (like pass a parameter named "GoodSet" and only if that
> parameter is defined with a specificed value would you actuall set the
> static variables).
> 
> 	Randy
> 
> > -----Original Message-----
> > From: Claes Holmerson [mailto:claes.holmerson@pipechain.com]
> > Sent: Wednesday, August 08, 2001 11:51 AM
> > To: 'tomcat-user@jakarta.apache.org'
> > Subject: Problems with <init-param>
> > 
> > 
> > Hello!
> > 
> > I can not get the init-parameters that I specify in web.xml to work
> > correctly. When I invoke the servlet as 
> > 
> > http://localhost:8080/pipechain/servlet/se.masystem.pipeline.w
> > eb.servlet.Log
> > inServlet 
> > 
> > then in init(ServletConfig):
> > 
> > config.getInitParameter("propertyfile");
> > 
> > returns null, but if I invoke the servlet with 
> > 
> > http://localhost:8080/pipechain/login
> > 
> > it works! Should not these urls be equivalent? (Tomcat 3.2.2)
> > 
> > /Claes 
> > 
> > (please cc me as I don't subscribe to the list)
> > 
> > 
> > 
> > Below is the relevant fragment from web.xml:
> > 
> >     <servlet>
> >         <servlet-name>
> >             Login
> >         </servlet-name>
> >         <servlet-class>
> >             se.masystem.pipeline.web.servlet.LoginServlet
> >         </servlet-class>
> >         <init-param>
> >             <param-name>propertyfile</param-name>
> >             <param-value>start.properties</param-value>
> >         </init-param>
> >     </servlet>
> > 
> >     <servlet-mapping>
> >         <servlet-name>
> >             Login
> >         </servlet-name>
> >         <url-pattern>
> >             /login
> >         </url-pattern>
> >     </servlet-mapping>
> > 
> > 
> > 
> > ----------------------------------------------------------------------
> > Claes Holmerson
> > PipeChain AB
> > Porfyrvägen 14		+46 (0)46 32 52 42     office phone
> > SE-224 78 LUND		+46 (0)709 89 52 42   mobile phone
> > Sweden			+46 (0)46 15 10 74     office fax
> >                            
> > http://www.pipechain.com/
> > --------------------------------------------------------------
> > ----------
> > 
>