- dd
- dd
- dd
- dd
- dd
- dd
- ServletConfig: 封装了Servlet 的配置信息,并且可以获取 ServletContext 对
- 配置初始化参数:
<servlet> <servlet-name>helloServlet</servlet-name> <servlet-class>servlettest.HelloServlet</servlet-class> <!-- 配置Servlet 初始化参数, 且该节点必须在load-on-startup 的前面 --> <init-param> <!-- 参数名 --> <param-name>username</param-name> <!-- 参数值 --> <param-value>root</param-value> </init-param> <init-param> <param-name>password</param-name> <param-value>12345</param-value> </init-param> <!-- 指定Servlet被创建的时机 --> <load-on-startup>-1</load-on-startup> </servlet>
-
- 获取初始化参数:
- getInitParameter(String name): 获取指定参数名的初始化参数
- getInitParameterNames(): 获取参数名组成的Enumeration对象
- getServletName():用的比较少:
- 获取初始化参数:
@Override public void init(ServletConfig servletConfig) throws ServletException { System.out.println("This is init"); String user = servletConfig.getInitParameter("user"); System.out.println("user=" + user); Enumeration<String> parameterNmaes = servletConfig.getInitParameterNames(); while (parameterNmaes.hasMoreElements()){ String name = parameterNmaes.nextElement(); String value = servletConfig.getInitParameter(name); System.out.println(name + " = " + value); } String servletName = servletConfig.getServletName(); System.out.println(servletName); }
时间: 2024-11-07 13:55:06