You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Matteo Turra <mt...@kion.it> on 2007/11/15 18:13:47 UTC

Enable/Disable servlet via context param

I would like enable/disable a servlet depending on a context init
parameter.

 

I wrote this code in my servlet. But I'm not sure I the best way to do
it.

 

public void init() {

            String param =
this.getServletContext().getInitParameter("PARAM");

            if ("ON".equalsIgnoreCase(param)) {

                  System.out.println("Servlet: ON");

                  super.init();     

            } else {

                  System.out.println("Servlet: OFF");

                  this.destroy();

            }

      }

 

Any suggestion?

 

Matteo.


Re: Enable/Disable servlet via context param

Posted by Juha Laiho <Ju...@iki.fi>.
Matteo Turra wrote:
> I would like enable/disable a servlet depending on a context init
> parameter.

Have a look at the Servlet specification, and therein the
chapter about Servlet Life Cycle.

> I wrote this code in my servlet. But I'm not sure I the best way to do
> it.

> 
> public void init() {
> 
>             String param =
> this.getServletContext().getInitParameter("PARAM");
> 
>             if ("ON".equalsIgnoreCase(param)) {
> 
>                   System.out.println("Servlet: ON");
> 
>                   super.init();     
> 
>             } else {
> 
>                   System.out.println("Servlet: OFF");
> 
>                   this.destroy();
> 
>             }
> 
>       }

I think that might throw things into a kind of loop: the container could
keep on trying to initialize the servlet.

> Any suggestion?

>From the specification (but please get the spec in order to read this in
proper context):

<<<
During initialization, the servlet instance can throw an UnavailableException or a
ServletException. In this case the servlet must not be placed into active service
and must be released by the servlet container. The destroy method is not called as it
is considered unsuccessful initialization.
>>>

-- 
..Juha

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org