-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Syed,

On 6/7/2009 12:53 PM, syed shah wrote:
> I want to enforce single instance creation for the servlet because I have
> some code that serves the user requests and i want to implement caching and
> handle synchronization myself, thanks and best regards Fahad

Usually, only one instance of your servlet will be created. You could
write some code to check for this, of course.

Something like this should work:

public class MyServlet extends HttpServlet
{
    private static boolean _isInUse;

    public MyServlet()
        throws ServletException
    {
        super();

        synchronized(getClass()) {
            if(_isInUse) {
                throw new ServletException("Sorry, only one at a time");
            }

            _isInUse = true;
        }
    }

    ...

    public void destroy()
    {
        synchronized(getClass()) {
            _isInUse = false;
        }
    }
}

I'm not actually sure why you'd ever want to do this, though. :(

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

iEYEARECAAYFAkotO68ACgkQ9CaO5/Lv0PBXBgCgm0hf3J73t7GCXsLl9KHY5spf
c5YAn1NSuZYMrk6r9FqFFdv8OqxQij/B
=DZ4r
-----END PGP SIGNATURE-----

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

Reply via email to