public void removeInternalServlet ( String name , String pathSpec , Class < ? extends HttpServlet > clazz ) { if ( null == clazz ) { return ; } ServletHandler servletHandler = webAppContext . getServletHandler ( ) ; List < ServletHolder > newServletHolders = new ArrayList < ServletHolder > ( ) ; List < ServletMapping > newServletMappings = new ArrayList < ServletMapping > ( ) ; String clazzName = clazz . getName ( ) ; Set < String > holdersToRemove = new HashSet < String > ( ) ; //find all the holders that hold the servlet to be removed for ( ServletHolder holder : servletHandler . getServlets ( ) ) { try { if ( clazzName . equals ( holder . getServlet ( ) . getClass ( ) . getName ( ) ) && name . equals ( holder . getName ( ) ) ) { holdersToRemove . add ( holder . getName ( ) ) ; } else { newServletHolders . add ( holder ) ; } } catch ( ServletException e ) { LOG . error ( "exception in removeInternalServlet() when iterating through" + "servlet holders" + StringUtils . stringifyException ( e ) ) ; } } //if there is no holder to be removed, then the servlet does not exist in //current context if ( holdersToRemove . size ( ) < 1 ) { return ; } //only add the servlet mapping if it is not to be removed for ( ServletMapping mapping : servletHandler . getServletMappings ( ) ) { //if the mapping's servlet is not to be removed, add to new mappings if ( ! holdersToRemove . contains ( mapping . getServletName ( ) ) ) { newServletMappings . add ( mapping ) ; } else { String [ ] pathSpecs = mapping . getPathSpecs ( ) ; boolean pathSpecMatched = false ; if ( pathSpecs != null && pathSpecs . length > 0 ) { for ( String pathSpecInMapping : pathSpecs ) { if ( pathSpecInMapping . equals ( pathSpec ) ) { pathSpecMatched = true ; break ; } } } //if the pathspec does not match, then add to the new mappings if ( ! pathSpecMatched ) { newServletMappings . add ( mapping ) ; } } } servletHandler . setServletMappings ( newServletMappings . toArray ( new ServletMapping [ newServletMappings . size ( ) ] ) ) ; servletHandler . setServlets ( newServletHolders . toArray ( new ServletHolder [ newServletHolders . size ( ) ] ) ) ; }
Remove an internal servlet in the server .