12_ServletConfig对象

【ServletConfig对象简述】

在Servlet的配置文件中,可以使用一个或多个<init-param>标签为Servlet配置一些初始化参数。

当Servlet配置了初始化参数后,web容器在创建Servlet实例对象时,会自动将这些初始化参数封装到ServletConfig对象中,并在调用Servlet的init方法的时候,将ServletConfig对象传递给Servlet,进而,我们同通过ServletConfig对象就可以得到当前Servlet的初始化参数的信息。

【ServletConfig的API】

String getInitParameter(String name)       得到参数

Enumeration getInitParameterNames()      得到所有参数

ServletContext getServletContext()           得到上下文对象

String getServletName()                          得到Servlet名称

【简单案例----从init方法中获取init-param中的参数值】

[ web.xml ]

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>HelloServlet</display-name> <servlet>
<servlet-name>FirstServlet</servlet-name>
<servlet-class>com.Higgin.servlet.FirstServlet</servlet-class>
<init-param>
<param-name>name</param-name>
<param-value>Higgin</param-value>
</init-param>

</servlet> <servlet-mapping>
<servlet-name>FirstServlet</servlet-name>
<url-pattern>/test</url-pattern>
</servlet-mapping> </web-app>

[ FirstServlet ]

package com.Higgin.servlet;

import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class FirstServlet extends HttpServlet { private ServletConfig config; public void init(ServletConfig config) throws ServletException {
this.config=config;
} protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name=config.getInitParameter("name"); //使用ServletConfig对象的getParameter(param-name)来获取对应的value值
System.out.println("name=="+name);
System.out.println("doGet方法执行---");
} protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
System.out.println("doPost方法执行---");
} }

[ 运行结果 ]

12_ServletConfig对象

【简单案例2----获取init-param中的 指定的参数值 和  所有的参数值】

[ web.xml ]

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>HelloServlet</display-name> <servlet>
<servlet-name>FirstServlet</servlet-name>
<servlet-class>com.Higgin.servlet.FirstServlet</servlet-class>
<init-param>
<param-name>name</param-name>
<param-value>Higgin</param-value>
</init-param> <init-param>
<param-name>age</param-name>
<param-value>18</param-value>
</init-param> <init-param>
<param-name>eamil</param-name>
<param-value>12345@qq.com</param-value>
</init-param> <init-param>
<param-name>sex</param-name>
<param-value>男</param-value>
</init-param>
</servlet> <servlet-mapping>
<servlet-name>FirstServlet</servlet-name>
<url-pattern>/test</url-pattern>
</servlet-mapping> </web-app>

[ FirstServlet.java ]

package com.Higgin.servlet;

import java.io.IOException;
import java.util.Enumeration; import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class FirstServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //得到指定的值
String value =this.getServletConfig().getInitParameter("name");
System.out.println("指定name----------"+value); //得到所有的
Enumeration e=this.getServletConfig().getInitParameterNames();
while(e.hasMoreElements()){
String name=(String) e.nextElement();
String value1=this.getServletConfig().getInitParameter(name);
System.out.println("遍历"+name+"-------"+value1);
}
} protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
System.out.println("doPost方法执行---");
} }

[ 运行结果 ]

12_ServletConfig对象

[ 提示 ]

实际开发中,有一些不适合在Servlet程序中写死的模块,这些数据可以通过配置方式给Servlet,例如Servlet采用那种编码、Servlet连接哪个库、Servlet哪个配置文件。

上一篇:深入了解Java内存模型——JMM


下一篇:apache2.4配置虚拟目录