You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Dan Armbrust <da...@gmail.com> on 2009/11/12 17:47:41 UTC

How to cancel a servlet startup?

If I have a servlet which fails during init() for whatever reason -
the example below takes a null pointer....

public class MyServlet extends HttpServlet
{
	private static final long serialVersionUID = 7997991143724219371L;

	@Override
	public void destroy()
	{
		//do stuff....
		super.destroy();
	}

	@Override
	public void init() throws ServletException
	{
		try
		{
			String a = null;
			a.toString();
		}
		catch (Exception e)
		{
			System.err.println("Startup error - cancelling startup." +  e);
			try
			{
				destroy();
			}
			catch (Exception e1)
			{
				//noop
			}
			throw new ServletException("Startup failing due to unexpected error: " + e);
		}
	}
}


How can I make tomcat cancel the deployment of the entire war file
that this servlet was distributed with?

I thought that throwing a ServletException back up to Tomcat would
make the webapp unavailable - but Tomcat continues to serve pages from
this webapp even though the startup failed.  That doesn't seem like
correct behavior... am I missing a setting somewhere?

Thanks,

Dan

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


Re: How to cancel a servlet startup?

Posted by Kris Schneider <ks...@gmail.com>.
>From a spec perspective, you're only guaranteed that the specific
servlet that throws an exception from its init method will be taken
out of service, not the entire app. One possibility might be to do the
system checks in a ServletContextListener and then have a Filter
operate on every request, inspect the status of your system checks
(hopefully a very quick inspection!), and redirect if there's a
problem.

On Thu, Nov 12, 2009 at 1:07 PM, Dan Armbrust
<da...@gmail.com> wrote:
> I'll tell you what, if you can tell me how to prevent my users (who
> have full control over the application / installation / hardware where
> this is running) from being able to shoot themselves in the foot and
> do something that causes my app to fail - I'll buy you a case of beer
> and not worry about this.
>
> Until then, my servlet needs to do system checks - and if something is
> wrong, it needs to not deploy.  Thats the bit I haven't yet figured
> out...  How do I get tomcat to disable the entire context, when I
> detect that something is broken during startup?  And ideally, redirect
> the users to an error screen that tells them that it's broken..
>
> Thanks,
>
> Dan
>
> On Thu, Nov 12, 2009 at 11:42 AM, Joseph Morgan
> <jo...@ignitesales.com> wrote:
>> Dan,
>>
>> Pardon my advice, but... this sounds like a programming/config/illegal
>> state error that shouldn't make it to production.
>>
>> Of course, you could simply add instrumentation to the system to detect
>> that this servlet didn't do its thing, and route every request to a
>> holding page.
>>
>> Joe
>>
>> -----Original Message-----
>> From: Dan Armbrust [mailto:daniel.armbrust.list@gmail.com]
>> Sent: Thursday, November 12, 2009 10:48 AM
>> To: Tomcat Users List
>> Subject: How to cancel a servlet startup?
>>
>> If I have a servlet which fails during init() for whatever reason -
>> the example below takes a null pointer....
>>
>> public class MyServlet extends HttpServlet
>> {
>>        private static final long serialVersionUID =
>> 7997991143724219371L;
>>
>>        @Override
>>        public void destroy()
>>        {
>>                //do stuff....
>>                super.destroy();
>>        }
>>
>>        @Override
>>        public void init() throws ServletException
>>        {
>>                try
>>                {
>>                        String a = null;
>>                        a.toString();
>>                }
>>                catch (Exception e)
>>                {
>>                        System.err.println("Startup error - cancelling
>> startup." +  e);
>>                        try
>>                        {
>>                                destroy();
>>                        }
>>                        catch (Exception e1)
>>                        {
>>                                //noop
>>                        }
>>                        throw new ServletException("Startup failing due
>> to unexpected error: " + e);
>>                }
>>        }
>> }
>>
>>
>> How can I make tomcat cancel the deployment of the entire war file
>> that this servlet was distributed with?
>>
>> I thought that throwing a ServletException back up to Tomcat would
>> make the webapp unavailable - but Tomcat continues to serve pages from
>> this webapp even though the startup failed.  That doesn't seem like
>> correct behavior... am I missing a setting somewhere?
>>
>> Thanks,
>>
>> Dan

-- 
Kris Schneider

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


RE: How to cancel a servlet startup?

Posted by Joseph Morgan <jo...@ignitesales.com>.
OK... now you're asking for miracles!  You need to write "users@miracleworkers.com".... har de har!

-----Original Message-----
From: Dan Armbrust [mailto:daniel.armbrust.list@gmail.com] 
Sent: Thursday, November 12, 2009 12:08 PM
To: Tomcat Users List
Subject: Re: How to cancel a servlet startup?

I'll tell you what, if you can tell me how to prevent my users (who
have full control over the application / installation / hardware where
this is running) from being able to shoot themselves in the foot and
do something that causes my app to fail - I'll buy you a case of beer
and not worry about this.

Until then, my servlet needs to do system checks - and if something is
wrong, it needs to not deploy.  Thats the bit I haven't yet figured
out...  How do I get tomcat to disable the entire context, when I
detect that something is broken during startup?  And ideally, redirect
the users to an error screen that tells them that it's broken..

Thanks,

Dan

On Thu, Nov 12, 2009 at 11:42 AM, Joseph Morgan
<jo...@ignitesales.com> wrote:
> Dan,
>
> Pardon my advice, but... this sounds like a programming/config/illegal
> state error that shouldn't make it to production.
>
> Of course, you could simply add instrumentation to the system to detect
> that this servlet didn't do its thing, and route every request to a
> holding page.
>
> Joe
>
> -----Original Message-----
> From: Dan Armbrust [mailto:daniel.armbrust.list@gmail.com]
> Sent: Thursday, November 12, 2009 10:48 AM
> To: Tomcat Users List
> Subject: How to cancel a servlet startup?
>
> If I have a servlet which fails during init() for whatever reason -
> the example below takes a null pointer....
>
> public class MyServlet extends HttpServlet
> {
>        private static final long serialVersionUID =
> 7997991143724219371L;
>
>        @Override
>        public void destroy()
>        {
>                //do stuff....
>                super.destroy();
>        }
>
>        @Override
>        public void init() throws ServletException
>        {
>                try
>                {
>                        String a = null;
>                        a.toString();
>                }
>                catch (Exception e)
>                {
>                        System.err.println("Startup error - cancelling
> startup." +  e);
>                        try
>                        {
>                                destroy();
>                        }
>                        catch (Exception e1)
>                        {
>                                //noop
>                        }
>                        throw new ServletException("Startup failing due
> to unexpected error: " + e);
>                }
>        }
> }
>
>
> How can I make tomcat cancel the deployment of the entire war file
> that this servlet was distributed with?
>
> I thought that throwing a ServletException back up to Tomcat would
> make the webapp unavailable - but Tomcat continues to serve pages from
> this webapp even though the startup failed.  That doesn't seem like
> correct behavior... am I missing a setting somewhere?
>
> Thanks,
>
> Dan
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>

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


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


RE: How to cancel a servlet startup?

Posted by Joseph Morgan <jo...@ignitesales.com>.
Dan... Similar to what they are saying, but why don't you have a system
script that will shut down that particular app if the startup fails...
that is.  When the startup fails, have it fire off the system script
that shuts down the context?  

Or, does it fail so catastrophically that you have no control over that?
If so, maybe a system level monitor to shut down the context if the
startup fails.

-----Original Message-----
From: Dan Armbrust [mailto:daniel.armbrust.list@gmail.com] 
Sent: Thursday, November 12, 2009 1:18 PM
To: Tomcat Users List
Subject: Re: How to cancel a servlet startup?

Thanks for all the tips.  I'll have to put something together with a
filter.

Just wanted to make sure there wasn't an easier way that I was missing.

Dan

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


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


Re: How to cancel a servlet startup?

Posted by Dan Armbrust <da...@gmail.com>.
Thanks for all the tips.  I'll have to put something together with a filter.

Just wanted to make sure there wasn't an easier way that I was missing.

Dan

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


Re: How to cancel a servlet startup?

Posted by Tim Funk <fu...@apache.org>.
I'll one up it to make it trivial ...

// put this in a filter mapped to everything
doFilter(...) {
   if (servletContext.getAttribute("initFailed")) {
     response.sendError(503);
     return;
   }
   chain.doFilter(request,response);
}

// and put this in any servlet or listerer
} catch(Throwable e) {
   servletContext.getAttribute("initFailed", "Y");
}


If you have access to URLRewrite filter - then you don't even need to 
write the filter above - but that configuration is left as an exercise 
to the user


-Tim



Mark Thomas wrote:
> Dan Armbrust wrote:
>> I'll tell you what, if you can tell me how to prevent my users (who
>> have full control over the application / installation / hardware where
>> this is running) from being able to shoot themselves in the foot and
>> do something that causes my app to fail - I'll buy you a case of beer
>> and not worry about this.
>>
>> Until then, my servlet needs to do system checks - and if something is
>> wrong, it needs to not deploy.  Thats the bit I haven't yet figured
>> out...  How do I get tomcat to disable the entire context, when I
>> detect that something is broken during startup?  And ideally, redirect
>> the users to an error screen that tells them that it's broken..
> 
> Sounds like a job for a filter / context listener combination. Not the only
> solution something like:
> - context listener fire when app starts
> - do checks
> - set static with the result
> - all requests run through filter
> - filter checks static
> - if OK - allow request
> - if !OK don't allow request & return error page.
> 
> Mark
> 
>> Thanks,
>>
>> Dan
>>
>> On Thu, Nov 12, 2009 at 11:42 AM, Joseph Morgan
>> <jo...@ignitesales.com> wrote:
>>> Dan,
>>>
>>> Pardon my advice, but... this sounds like a programming/config/illegal
>>> state error that shouldn't make it to production.
>>>
>>> Of course, you could simply add instrumentation to the system to detect
>>> that this servlet didn't do its thing, and route every request to a
>>> holding page.
>>>
>>> Joe
>>>
>>> -----Original Message-----
>>> From: Dan Armbrust [mailto:daniel.armbrust.list@gmail.com]
>>> Sent: Thursday, November 12, 2009 10:48 AM
>>> To: Tomcat Users List
>>> Subject: How to cancel a servlet startup?
>>>
>>> If I have a servlet which fails during init() for whatever reason -
>>> the example below takes a null pointer....
>>>
>>> public class MyServlet extends HttpServlet
>>> {
>>>        private static final long serialVersionUID =
>>> 7997991143724219371L;
>>>
>>>        @Override
>>>        public void destroy()
>>>        {
>>>                //do stuff....
>>>                super.destroy();
>>>        }
>>>
>>>        @Override
>>>        public void init() throws ServletException
>>>        {
>>>                try
>>>                {
>>>                        String a = null;
>>>                        a.toString();
>>>                }
>>>                catch (Exception e)
>>>                {
>>>                        System.err.println("Startup error - cancelling
>>> startup." +  e);
>>>                        try
>>>                        {
>>>                                destroy();
>>>                        }
>>>                        catch (Exception e1)
>>>                        {
>>>                                //noop
>>>                        }
>>>                        throw new ServletException("Startup failing due
>>> to unexpected error: " + e);
>>>                }
>>>        }
>>> }
>>>
>>>
>>> How can I make tomcat cancel the deployment of the entire war file
>>> that this servlet was distributed with?
>>>
>>> I thought that throwing a ServletException back up to Tomcat would
>>> make the webapp unavailable - but Tomcat continues to serve pages from
>>> this webapp even though the startup failed.  That doesn't seem like
>>> correct behavior... am I missing a setting somewhere?
>>>
>>> Thanks,
>>>
>>> Dan
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>>> For additional commands, e-mail: users-help@tomcat.apache.org
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>>> For additional commands, e-mail: users-help@tomcat.apache.org
>>>
>>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>> For additional commands, e-mail: users-help@tomcat.apache.org
>>
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
> 
> 
> 

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


Re: How to cancel a servlet startup?

Posted by Mark Thomas <ma...@apache.org>.
Dan Armbrust wrote:
> I'll tell you what, if you can tell me how to prevent my users (who
> have full control over the application / installation / hardware where
> this is running) from being able to shoot themselves in the foot and
> do something that causes my app to fail - I'll buy you a case of beer
> and not worry about this.
> 
> Until then, my servlet needs to do system checks - and if something is
> wrong, it needs to not deploy.  Thats the bit I haven't yet figured
> out...  How do I get tomcat to disable the entire context, when I
> detect that something is broken during startup?  And ideally, redirect
> the users to an error screen that tells them that it's broken..

Sounds like a job for a filter / context listener combination. Not the only
solution something like:
- context listener fire when app starts
- do checks
- set static with the result
- all requests run through filter
- filter checks static
- if OK - allow request
- if !OK don't allow request & return error page.

Mark

> 
> Thanks,
> 
> Dan
> 
> On Thu, Nov 12, 2009 at 11:42 AM, Joseph Morgan
> <jo...@ignitesales.com> wrote:
>> Dan,
>>
>> Pardon my advice, but... this sounds like a programming/config/illegal
>> state error that shouldn't make it to production.
>>
>> Of course, you could simply add instrumentation to the system to detect
>> that this servlet didn't do its thing, and route every request to a
>> holding page.
>>
>> Joe
>>
>> -----Original Message-----
>> From: Dan Armbrust [mailto:daniel.armbrust.list@gmail.com]
>> Sent: Thursday, November 12, 2009 10:48 AM
>> To: Tomcat Users List
>> Subject: How to cancel a servlet startup?
>>
>> If I have a servlet which fails during init() for whatever reason -
>> the example below takes a null pointer....
>>
>> public class MyServlet extends HttpServlet
>> {
>>        private static final long serialVersionUID =
>> 7997991143724219371L;
>>
>>        @Override
>>        public void destroy()
>>        {
>>                //do stuff....
>>                super.destroy();
>>        }
>>
>>        @Override
>>        public void init() throws ServletException
>>        {
>>                try
>>                {
>>                        String a = null;
>>                        a.toString();
>>                }
>>                catch (Exception e)
>>                {
>>                        System.err.println("Startup error - cancelling
>> startup." +  e);
>>                        try
>>                        {
>>                                destroy();
>>                        }
>>                        catch (Exception e1)
>>                        {
>>                                //noop
>>                        }
>>                        throw new ServletException("Startup failing due
>> to unexpected error: " + e);
>>                }
>>        }
>> }
>>
>>
>> How can I make tomcat cancel the deployment of the entire war file
>> that this servlet was distributed with?
>>
>> I thought that throwing a ServletException back up to Tomcat would
>> make the webapp unavailable - but Tomcat continues to serve pages from
>> this webapp even though the startup failed.  That doesn't seem like
>> correct behavior... am I missing a setting somewhere?
>>
>> Thanks,
>>
>> Dan
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>> For additional commands, e-mail: users-help@tomcat.apache.org
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>> For additional commands, e-mail: users-help@tomcat.apache.org
>>
>>
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
> 


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


Re: How to cancel a servlet startup?

Posted by Dan Armbrust <da...@gmail.com>.
I'll tell you what, if you can tell me how to prevent my users (who
have full control over the application / installation / hardware where
this is running) from being able to shoot themselves in the foot and
do something that causes my app to fail - I'll buy you a case of beer
and not worry about this.

Until then, my servlet needs to do system checks - and if something is
wrong, it needs to not deploy.  Thats the bit I haven't yet figured
out...  How do I get tomcat to disable the entire context, when I
detect that something is broken during startup?  And ideally, redirect
the users to an error screen that tells them that it's broken..

Thanks,

Dan

On Thu, Nov 12, 2009 at 11:42 AM, Joseph Morgan
<jo...@ignitesales.com> wrote:
> Dan,
>
> Pardon my advice, but... this sounds like a programming/config/illegal
> state error that shouldn't make it to production.
>
> Of course, you could simply add instrumentation to the system to detect
> that this servlet didn't do its thing, and route every request to a
> holding page.
>
> Joe
>
> -----Original Message-----
> From: Dan Armbrust [mailto:daniel.armbrust.list@gmail.com]
> Sent: Thursday, November 12, 2009 10:48 AM
> To: Tomcat Users List
> Subject: How to cancel a servlet startup?
>
> If I have a servlet which fails during init() for whatever reason -
> the example below takes a null pointer....
>
> public class MyServlet extends HttpServlet
> {
>        private static final long serialVersionUID =
> 7997991143724219371L;
>
>        @Override
>        public void destroy()
>        {
>                //do stuff....
>                super.destroy();
>        }
>
>        @Override
>        public void init() throws ServletException
>        {
>                try
>                {
>                        String a = null;
>                        a.toString();
>                }
>                catch (Exception e)
>                {
>                        System.err.println("Startup error - cancelling
> startup." +  e);
>                        try
>                        {
>                                destroy();
>                        }
>                        catch (Exception e1)
>                        {
>                                //noop
>                        }
>                        throw new ServletException("Startup failing due
> to unexpected error: " + e);
>                }
>        }
> }
>
>
> How can I make tomcat cancel the deployment of the entire war file
> that this servlet was distributed with?
>
> I thought that throwing a ServletException back up to Tomcat would
> make the webapp unavailable - but Tomcat continues to serve pages from
> this webapp even though the startup failed.  That doesn't seem like
> correct behavior... am I missing a setting somewhere?
>
> Thanks,
>
> Dan
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>

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


RE: How to cancel a servlet startup?

Posted by Joseph Morgan <jo...@ignitesales.com>.
Dan,

Pardon my advice, but... this sounds like a programming/config/illegal
state error that shouldn't make it to production.  

Of course, you could simply add instrumentation to the system to detect
that this servlet didn't do its thing, and route every request to a
holding page.

Joe

-----Original Message-----
From: Dan Armbrust [mailto:daniel.armbrust.list@gmail.com] 
Sent: Thursday, November 12, 2009 10:48 AM
To: Tomcat Users List
Subject: How to cancel a servlet startup?

If I have a servlet which fails during init() for whatever reason -
the example below takes a null pointer....

public class MyServlet extends HttpServlet
{
	private static final long serialVersionUID =
7997991143724219371L;

	@Override
	public void destroy()
	{
		//do stuff....
		super.destroy();
	}

	@Override
	public void init() throws ServletException
	{
		try
		{
			String a = null;
			a.toString();
		}
		catch (Exception e)
		{
			System.err.println("Startup error - cancelling
startup." +  e);
			try
			{
				destroy();
			}
			catch (Exception e1)
			{
				//noop
			}
			throw new ServletException("Startup failing due
to unexpected error: " + e);
		}
	}
}


How can I make tomcat cancel the deployment of the entire war file
that this servlet was distributed with?

I thought that throwing a ServletException back up to Tomcat would
make the webapp unavailable - but Tomcat continues to serve pages from
this webapp even though the startup failed.  That doesn't seem like
correct behavior... am I missing a setting somewhere?

Thanks,

Dan

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


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


Re: How to cancel a servlet startup?

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Dan,

On 11/12/2009 11:47 AM, Dan Armbrust wrote:
> How can I make tomcat cancel the deployment of the entire war file 
> that this servlet was distributed with?

By the time the servlets are started, the context (webapp) has already
started up, though that is not defined by the servlet spec per se.

> I thought that throwing a ServletException back up to Tomcat would 
> make the webapp unavailable

The servlet specification does not require that the container behave in
this way.

> but Tomcat continues to serve pages from this webapp even though the
> startup failed.  That doesn't seem like correct behavior... am I
> missing a setting somewhere?

What you are observing is correct behavior. Note that no requests will
be routed to a servlet that has not successfully completed its init()
method, so you've got that going for you.

Unfortunately, I don't really have any ideas of how to mitigate this,
other than to set some kind of context attribute that contains the names
of all the successfully-started servlets, and then write a filter to
check to make sure the list is complete before processing any requests.
That's a little hacky and still doesn't really get you what you want,
though.

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkr8jkAACgkQ9CaO5/Lv0PCrIwCgl/QKZZBLau7LuSJSvBpUyV0d
Y5MAn1cTg5cBK1rQCqXfd+NdfrDimC5p
=ZDYt
-----END PGP SIGNATURE-----

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