您的位置:首页 > 其它

第三课ServletConfig对象

2015-10-24 15:42 423 查看
servletconfig:封装了servlet的配置信息,并且可以获取servletcontext对象。

1、配置servlet的初始化参数:

<servlet>

         <servlet-name>helloServlet</servlet-name>

         <servlet-class>com.atgaozhen.servlet.HelloServlet</servlet-class>

         <init-param>
        <param-name>user</param-name>
        <param-value>root</param-value>

         </init-param>

         <init-param>
        <param-name>password</param-name>
        <param-value>root</param-value>

         </init-param>

         <load-on-startup>1</load-on-startup>

</servlet>

2、获取初始化参数:getInitParameter(String name)

获取指定参数名的初始化参数

javax.servlet

Interface ServletConfig

 String
getInitParameter(String name)


          Returns a
String
containing the value
of the named initialization parameter, or
null
if the parameter does not exist.
 Enumeration
getInitParameterNames()


          Returns the names of the servlet's initialization parameters as an
Enumeration
of
String
objects, or an empty
Enumeration
if the servlet has no initialization parameters.
 ServletContext
getServletContext()


          Returns a reference to the
ServletContext
in which the caller is executing.
 String
getServletName()


          Returns the name of this servlet instance.
方法实例:

@Override
public void init(ServletConfig servletConfig) throws ServletException {
System.out.println("init(ServletConfig arg0)");
   String user=servletConfig.getInitParameter("user");
   System.out.println("user:"+user);
   
   Enumeration<String>names=servletConfig.getInitParameterNames();
   while(names.hasMoreElements()){
    String name=names.nextElement();
    String value=servletConfig.getInitParameter(name);
    System.out.println("name:"+name+":"+value);
   }
}

 System.out.println(servletConfig.getServletName());(不是很重要)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: