知识库--ServletConfig Object(54)

来源:互联网 发布:足球基本技术动作知乎 编辑:程序博客网 时间:2024/04/29 12:17

The loadServlet method of the StandardWrapper class calls the sender’s init method after the servlet is loaded. The init method is passed an instance of javax.servlet.ServletConfig. You may be wondering how the StandardWrapper object obtains the ServletConfig object.

//实现了ServletConfig接口
Look no further than the StandardWrapper class itself. This class implements the javax.servlet.ServletConfig interface, in addition to the Wrapper interface.

getServletContext, getServletName, getInitParameter, and getInitParameterNames;

Note The StandardWrapper class does not pass itself to the servlet’s init method, however, it wraps itself in a StandardWrapperFacade instance to hide most of its public method from a servlet programmer.

1 The signature of this method is as follows:

    public ServletContext getServletContext();    //调用上级容器的相同方法签名 

A StandardWrapper instance must be a child container of a StandardContext. This is to say, a StandardWrapper’s parent is a StandardContext. From the StandardContext object, you can obtain a ServletContext object calling its getServletContext method in the StandardWrapper class:

    public ServletContext getServletContext(){        if(parent == null){            return null;        }else if(!(parent instanceof Context)){            return null;        }else{            return ((Context)parent).getServletContext();        }    }

2 getServletName method

    public String getServletName();

实际上还是调用了父类 ContainerBase的 getName()方法

    public String getName(){        return name;    }

3 getInitParameter method

    public String getInitParameter(String name)

In StandardWrapper, the initialization parameters are stored in a HashMap named parameters.

    public HashMap parameters = new HashMap();

//填充参数
You populate parameters by calling the StandardWrapper class’s addInitParameter method, passing the name and the value of the parameter.

    public void addInitParameter(String name, String value){        synchronized(parameters){            parameters.put(name,value);        }        fireContainerEvent("addInitParameter",name);    }

The implementation of getInitParameter in StandardWrapper:

    public String getInitParameter(String name){        return (findInitParameter(name));    }    public String findInitParameter(String name){        synchronized(parameters){            return ((String)parameters.get(name));        }    }

4 getInitParameterNames

    public Enumeration getInitParameterNames(){        synchronized(parameters){            return (new Enumerator(parameters.keyset()));        }    }

“Parent and Children

A wrapper represents a container for each individual servlet. As such, a wrapper cannot have a child, and its addChild method should not be called. If you did, you would get a java.lang.IllegalStateException. Here is the implementation of the addChild method in the StandardWrapper class:

public void addChild(Container child){    throw new IllegalStateException("StandardWrapper.notChild");}

A wrapper’s parent can only be an implementation of Context. Its setParent method throws a java.lang.IllegalArgumentException if you pass a non-Context container to it.

    public void setParent(Container container){        if((container != null)  && !(container instanceof Context)){            throw new IllegalArgumentException("standardWrapper.notContext");        }        super.setParent(container);    }
0 0
原创粉丝点击