if (_classLoader!=null) { current_thread=Thread.currentThread(); old_classloader=current_thread.getContextClassLoader(); current_thread.setContextClassLoader(_classLoader); }
    public boolean handle(String target, HttpServletRequest request, HttpServletResponse response, int dispatch)            throws IOException, ServletException    {        boolean handled=false;        boolean new_context=false;        Request base_request=null;        Context old_context=null;        String old_context_path=null;        String old_servlet_path=null;        String old_path_info=null;        ClassLoader old_classloader=null;        Thread current_thread=null;                        base_request=(request instanceof Request)?(Request)request:HttpConnection.getCurrentConnection().getRequest();        old_context=base_request.getContext();                // Are we already in this context?        if (old_context!=_context)        {            new_context=true;                        // Check the vhosts            if (_vhosts!=null && _vhosts.length>0)            {                String vhost=request.getServerName();                boolean match=false;                // TODO consider non-linear lookup                for (int i=0;!match && i<_vhosts.length;i++)                    match=_vhosts[i]!=null && _vhosts[i].equalsIgnoreCase(vhost);                if (!match)                    return false;            }                        // Check the real hosts            if (_hosts!=null && _hosts.length>0)            {                String host=request.getLocalName();                boolean match=false;                // TODO consider non-linear lookup                for (int i=0;!match && i<_hosts.length;i++)                    match=_hosts[i]!=null && _hosts[i].equalsIgnoreCase(host);                if (!match)                    return false;            }                        // Nope - so check the target.            if (dispatch==REQUEST)            {                if (target.equals(_contextPath))                {                    target=_contextPath;                    if (!target.endsWith("/"))                    {                        response.sendRedirect(target+"/");                        return true;                    }                }                else if (target.startsWith(_contextPath) && (_contextPath.length()==1 || target.charAt(_contextPath.length())=='/'))                {                    if (_contextPath.length()>1)                        target=target.substring(_contextPath.length());                }                else                 {                    // Not for this context!                    return false;                }            }        }                try        {            old_context_path=base_request.getContextPath();            old_servlet_path=base_request.getServletPath();            old_path_info=base_request.getPathInfo();                        // Update the paths            base_request.setContext(_context);            if (dispatch!=INCLUDE && target.startsWith("/"))            {                if (_contextPath.length()==1)                    base_request.setContextPath("");                else                    base_request.setContextPath(_contextPath);                base_request.setServletPath(null);                base_request.setPathInfo(target);            }                        // Set the classloader            if (_classLoader!=null)            {                current_thread=Thread.currentThread();                old_classloader=current_thread.getContextClassLoader();                current_thread.setContextClassLoader(_classLoader);            }                        // Handle the REALLY SILLY request events!            ServletRequestEvent event=null;            if (new_context)            {                if (_requestListeners!=null)                {                    event = new ServletRequestEvent(_context,request);                    for(int i=0;i<LazyList.size(_requestListeners);i++)                        ((ServletRequestListener)LazyList.get(_requestListeners,i)).requestInitialized(event);                }                for(int i=0;i<LazyList.size(_requestAttributeListeners);i++)                    base_request.addEventListener(((ServletRequestListener)LazyList.get(_requestAttributeListeners,i)));            }                        // Handle the request            try            {                handled = getHandler().handle(target, request, response, dispatch);            }            finally            {                // Handle more REALLY SILLY request events!                if (new_context)                {                    for(int i=0;i<LazyList.size(_requestListeners);i++)                        ((ServletRequestListener)LazyList.get(_requestListeners,i)).requestDestroyed(event);                                        for(int i=0;i<LazyList.size(_requestAttributeListeners);i++)                        base_request.removeEventListener(((ServletRequestListener)LazyList.get(_requestAttributeListeners,i)));                }            }        }        finally        {            if (old_context!=_context)            {                // reset the classloader                if (_classLoader!=null)                {                    current_thread.setContextClassLoader(old_classloader);                }                                // reset the context and servlet path.                base_request.setContext(old_context);                base_request.setContextPath(old_context_path);                base_request.setServletPath(old_servlet_path);                base_request.setPathInfo(old_path_info);             }        }                return handled;    }