1.需要定义ServletConfig对象来接收servlet配置的初始化参数。
2.当servlet配置了初始化参数后,web容器在创建servlet实例对象时, 会自动将这些初始化参数封装到ServletConfig对象中,并在调用servlet的init方法时,将ServletConfig对象传递给servlet。进而,程序员通过ServletConfig对象就可以得到当前servlet的初始化参数信息。
3.注意:获取servlet初始值(需要在init()方法中初始化)//注意,这里必须设置,自动加载,<load-on-startup>1</load-on-startup>//或者配置映射才能初始化servlet初始化参数,直接在类中不能初始化。
以下是代码:
*helloworld.java
package myweb; import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration; import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; /**
* Servlet implementation class helloworld
*/
public class helloworld extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* 定义ServletConfig对象来接收servlet配置的初始化参数
*/
private ServletConfig config;
/**
* 当servlet配置了初始化参数后,web容器在创建servlet实例对象时,
* 会自动将这些初始化参数封装到ServletConfig对象中,并在调用servlet的init方法时,
* 将ServletConfig对象传递给servlet。进而,程序员通过ServletConfig对象就可以
* 得到当前servlet的初始化参数信息。
*/
@Override
public void init(ServletConfig config) throws ServletException {
this.config = config;
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //.获取servlet初始值(需要在init()方法中初始化)
//注意,这里必须设置,自动加载,<load-on-startup>1</load-on-startup>
//或者配置映射才能初始化servlet初始化参数,直接在类中不能初始化
//获取在web.xml中配置的初始化参数
String paramVal = this.config.getInitParameter("name");//获取指定的初始化参数
response.getWriter().print(paramVal);
response.getWriter().print("<hr/>");
//获取所有的初始化参数
Enumeration<String> e = config.getInitParameterNames();
while(e.hasMoreElements()){
String name = e.nextElement();
String value = config.getInitParameter(name);
response.getWriter().print(name + "=" + value + "<br/>");
}
} /**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
} }
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"> <servlet>
<servlet-name>helloworld</servlet-name>
<servlet-class>myweb.helloworld</servlet-class> <!--配置helloworld(servlet)的初始化参数 -->
<init-param>
<param-name>name</param-name>
<param-value>xianghu</param-value>
</init-param>
<init-param>
<param-name>password</param-name>
<param-value>123456</param-value>
</init-param>
<init-param>
<param-name>charset</param-name>
<param-value>UTF-8</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>helloworld</servlet-name>
<url-pattern>/Default</url-pattern>
</servlet-mapping>
</web-app>