void dispatch(ServletRequest servletRequest,
                  ServletResponse servletResponse,
                  int type)
        throws ServletException,IOException
    {
        HttpServletRequest httpServletRequest=(HttpServletRequest)servletRequest;
        HttpServletResponse httpServletResponse=(HttpServletResponse)servletResponse;

        HttpConnection httpConnection=
            _servletHandler.getHttpContext().getHttpConnection();
        ServletHttpRequest servletHttpRequest=
            (ServletHttpRequest)httpConnection.getRequest().getWrapper();
        
        // wrap the request and response
        DispatcherRequest request = new DispatcherRequest(httpServletRequest,
                                                          servletHttpRequest,
                                                          type);
        DispatcherResponse response = new DispatcherResponse(request,
                                                             httpServletResponse);        
        
        if (type==Dispatcher.__FORWARD)
            servletResponse.resetBuffer();
        
        // Merge parameters
        String query=_query;
        MultiMap parameters=null;
        if (query!=null)
        {
            Map old_params = httpServletRequest.getParameterMap();
            
            // Add the parameters
            parameters=new MultiMap();
            UrlEncoded.decodeTo(query,parameters,request.getCharacterEncoding());
            
            if (old_params!=null && old_params.size()>0)
            {
                // Merge old parameters.
                Iterator iter = old_params.entrySet().iterator();
                while (iter.hasNext())
                {
                    Map.Entry entry = (Map.Entry)iter.next();
                    String name=(String)entry.getKey();
                    String[] values=(String[])entry.getValue();
                    for (int i=0;i<values.length;i++)
                        parameters.add(name, values[i]);
                }
            }

            request.setParameters(parameters);
            
            String old_query=httpServletRequest.getQueryString();
            if (old_query!=null)
                request.setQuery(query+"&"+old_query);
            else
                request.setQuery(query);
        }
        
        Object old_scope = null;
        try
        {
            if (request.crossContext())
            {
                // Setup new _context
                old_scope=
                    _servletHandler.getHttpContext()
                    .enterContextScope(httpConnection.getRequest(),httpConnection.getResponse());
            }
        
            if (isNamed())
            {
                // No further modifications required.
                if (_servletHandler instanceof WebApplicationHandler)
                {
                    JSR154Filter filter = ((WebApplicationHandler)_servletHandler).getJsr154Filter();
                    if (filter!=null && filter.isUnwrappedDispatchSupported())
                    {
                        filter.setDispatch(request, response);
                        _servletHandler.dispatch(null,httpServletRequest,httpServletResponse,_holder, type);
                    }
                    else
                        _servletHandler.dispatch(null,request,response,_holder, type);
                }
                else
                	_servletHandler.dispatch(null,request,response,_holder, type);
            }
            else
            {   
                // Adjust servlet paths
                request.setPaths(_servletHandler.getHttpContext().getContextPath(),
                                 PathMap.pathMatch(_pathSpec,_pathInContext),
                                 PathMap.pathInfo(_pathSpec,_pathInContext));
        

                // are we wrap over or wrap under
                if (_servletHandler instanceof WebApplicationHandler)
                {
                    JSR154Filter filter = ((WebApplicationHandler)_servletHandler).getJsr154Filter();
                    if (filter!=null && filter.isUnwrappedDispatchSupported())
                    {
                        filter.setDispatch(request, response);
                        _servletHandler.dispatch(_pathInContext,httpServletRequest,httpServletResponse,_holder, type);
                    }
                    else
                        _servletHandler.dispatch(_pathInContext,request,response,_holder, type);
                }
                else
                    _servletHandler.dispatch(_pathInContext,request,response,_holder, type);
                
                
                if (type!=Dispatcher.__INCLUDE)
                    response.close();
                else if (response.isFlushNeeded())
                    response.flushBuffer();
            }
        }
        finally
        {
            // restore _context
            if (request.crossContext())
                _servletHandler.getHttpContext()
                    .leaveContextScope(httpConnection.getRequest(),
                                       httpConnection.getResponse(),
                                       old_scope);
        }   
    }
void dispatch ( ServletRequest servletRequest , ServletResponse servletResponse , int type ) throws ServletException , IOException { HttpServletRequest httpServletRequest = ( HttpServletRequest ) servletRequest ; HttpServletResponse httpServletResponse = ( HttpServletResponse ) servletResponse ; HttpConnection httpConnection = _servletHandler . getHttpContext ( ) . getHttpConnection ( ) ; ServletHttpRequest servletHttpRequest = ( ServletHttpRequest ) httpConnection . getRequest ( ) . getWrapper ( ) ; DispatcherRequest request = new DispatcherRequest ( httpServletRequest , servletHttpRequest , type ) ; DispatcherResponse response = new DispatcherResponse ( request , httpServletResponse ) ; if ( type == Dispatcher . __FORWARD ) servletResponse . resetBuffer ( ) ; String query = _query ; MultiMap parameters = null ; if ( query != null ) { Map old_params = httpServletRequest . getParameterMap ( ) ; parameters = new MultiMap ( ) ; UrlEncoded . decodeTo ( query , parameters , request . getCharacterEncoding ( ) ) ; if ( old_params != null && old_params . size ( ) > 0 ) { Iterator iter = old_params . entrySet ( ) . iterator ( ) ; while ( iter . hasNext ( ) ) { Map . Entry entry = ( Map . Entry ) iter . next ( ) ; String name = ( String ) entry . getKey ( ) ; String [ ] values = ( String [ ] ) entry . getValue ( ) ; for ( int i = 0 ; i < values . length ; i ++ ) parameters . add ( name , values [ i ] ) ; } } request . setParameters ( parameters ) ; String old_query = httpServletRequest . getQueryString ( ) ; if ( old_query != null ) request . setQuery ( query + "&" + old_query ) ; else request . setQuery ( query ) ; } Object old_scope = null ; try { if ( request . crossContext ( ) ) { old_scope = _servletHandler . getHttpContext ( ) . enterContextScope ( httpConnection . getRequest ( ) , httpConnection . getResponse ( ) ) ; } if ( isNamed ( ) ) { if ( _servletHandler instanceof WebApplicationHandler ) { JSR154Filter filter = ( ( WebApplicationHandler ) _servletHandler ) . getJsr154Filter ( ) ; if ( filter != null && filter . isUnwrappedDispatchSupported ( ) ) { filter . setDispatch ( request , response ) ; _servletHandler . dispatch ( null , httpServletRequest , httpServletResponse , _holder , type ) ; } else _servletHandler . dispatch ( null , request , response , _holder , type ) ; } else _servletHandler . dispatch ( null , request , response , _holder , type ) ; } else { request . setPaths ( _servletHandler . getHttpContext ( ) . getContextPath ( ) , PathMap . pathMatch ( _pathSpec , _pathInContext ) , PathMap . pathInfo ( _pathSpec , _pathInContext ) ) ; if ( _servletHandler instanceof WebApplicationHandler ) { JSR154Filter filter = ( ( WebApplicationHandler ) _servletHandler ) . getJsr154Filter ( ) ; if ( filter != null && filter . isUnwrappedDispatchSupported ( ) ) { filter . setDispatch ( request , response ) ; _servletHandler . dispatch ( _pathInContext , httpServletRequest , httpServletResponse , _holder , type ) ; } else _servletHandler . dispatch ( _pathInContext , request , response , _holder , type ) ; } else _servletHandler . dispatch ( _pathInContext , request , response , _holder , type ) ; if ( type != Dispatcher . __INCLUDE ) response . close ( ) ; else if ( response . isFlushNeeded ( ) ) response . flushBuffer ( ) ; } } finally { if ( request . crossContext ( ) ) _servletHandler . getHttpContext ( ) . leaveContextScope ( httpConnection . getRequest ( ) , httpConnection . getResponse ( ) , old_scope ) ; } }
/* ------------------------------------------------------------