首页 > 学院 > 开发设计 > 正文

SevletConfig

2019-11-06 07:32:39
字体:
来源:转载
供稿:网友

ServletConfig 封装了servlet的配置信息

http://tomcat.apache.org/tomcat-8.5-doc/servletapi/javax/servlet/ServletConfig.html

<servlet>	<servlet-name>hellServlet</servlet-name>	<servlet-class>HellServlet</servlet-class>		<!-- 配置Servlet的初始化参数。且节点必须在load-on-srartup之前>	<init-param>		<param-name>user</param-name>		<param-value>root</param-value>	</init-param>		<init-param>		<param-name>passWord</param-name>		<param-value>123456</param-value>	</init-param>		<!-- 它必须是一个整数, 当值为0或者大于0时, 表示容器在应用启动时就加载>	<!--当值小于0或者没有指定时,则表示容器在该servlet被选择时才会去加载>	<!--正数的值越小,该servlet的优先级越高,应用启动时就越先加载。>	<!--当值相同时,容器就会自己选择顺序来加载。>	<load-on-srartup>-1</load-on-srartup></servlet>getInitParameter(String name) 获取指定参数名的初始化参数getInitParameterNames() 获取参数名组成的Enumeration对象

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

getServletName

java.lang.String getServletName()Returns the name of this servlet instance. The name may be provided via server administration, assigned in the web application deployment descriptor, or for an unregistered (and thus unnamed) servlet instance it will be the servlet's class name.Returns:the name of the servlet instance

getServletContext

ServletContext getServletContext()Returns a reference to the ServletContext in which the caller is executing.Returns:a ServletContext object, used by the caller to interact with its servlet containerSee Also:ServletContext

getInitParameter

java.lang.String getInitParameter(java.lang.String name)Returns a String containing the value of the named initialization parameter, or null if the parameter does not exist.Parameters:name - a String specifying the name of the initialization parameterReturns:a String containing the value of the initialization parameter

getInitParameterNames

java.util.Enumeration<java.lang.String> 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.Returns:an Enumeration of String objects containing the names of the servlet's initialization parameters


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表