第三课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
方法实例:
@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());(不是很重要)
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 Stringcontaining the value of the named initialization parameter, or nullif the parameter does not exist. |
Enumeration | getInitParameterNames() Returns the names of the servlet's initialization parameters as an Enumerationof Stringobjects, or an empty Enumerationif the servlet has no initialization parameters. |
ServletContext | getServletContext() Returns a reference to the ServletContextin 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());(不是很重要)
相关文章推荐
- codeblocks下如何修改新建文件的默认代码?
- LeetCode Problem5 LongestPalindromicSubstring
- Win32_VideoController 显卡 参数说明
- 单次遍历,等概率随机选取问题
- Entity Framework基础01
- 内存池的实现(二)
- PHP 文件写入方法
- poj 3169 Layout
- LeetCode 34: Search for a Range
- 每天一个linux命令(14):rm 命令
- C++ stl array数组
- Java 内部类
- Android 中通过定时器周期性执行网络请求
- Win32_NetworkAdapter 网卡 参数说明
- Lua中的loadfile、dofile、require详解
- [C++] Const Summary (mind map)
- 内存池的实现(一)
- sicily 1171. The Game of Efil
- Win32_Processor CPU 参数说明
- 测试的本质