public class ServletConfigDemo extends HttpServlet { private ServletConfig servletConfig; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获取Servlet得名字 this.servletConfig.getServletName(); } @Override public void init(ServletConfig config) throws ServletException { //从带参init方法中,提取ServletConfig对象 this.servletConfig = config; } }
//调用 GenericServlet 提供的 getServletConfig 方法获得 ServletConfig 对象 ServletConfig servletConfig = this.getServletConfig();
返回值类型 | 方法 | 功能描述 |
---|---|---|
String | getInitParameter(String name) | 根据初始化参数名 name,返回对应的初始化参数值。 |
Enumeration<String> | getInitParameterNames() | 返回 Servlet 所有的初始化参数名的枚举集合,如果该 Servlet 没有初始化参数,则返回一个空的集合。 |
ServletContext | getServletContext() | 返回一个代表当前 Web 应用的 ServletContext 对象。 |
String | getServletName() | 返回 Servlet 的名字,即 web.xml 中 <servlet-name> 元素的值。 |
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" metadata-complete="false" version="4.0"> <servlet> <servlet-name>MyServlet</servlet-name> <servlet-class>net.biancheng.www.MyServlet</servlet-class> <!-- Servlet 初始化参数 --> <init-param> <param-name>name</param-name> <param-value>编程帮</param-value> </init-param> <!-- Servlet 初始化参数 --> <init-param> <param-name>URL</param-name> <param-value>www.lmcjl.com</param-value> </init-param> </servlet> </web-app>以上配置说明如下:
package net.biancheng.www; import javax.servlet.ServletException; import javax.servlet.annotation.WebInitParam; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet(urlPatterns = {"/MyServlet"}, initParams = {@WebInitParam(name = "name", value = "编程帮"), @WebInitParam(name = "URL", value = "www.lmcjl.com")}) public class MyServlet extends HttpServlet { private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }
package net.biancheng.www; import java.io.IOException; import java.io.PrintWriter; import java.util.Enumeration; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.annotation.WebInitParam; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * * @author 编程帮 www.lmcjl.com * 获取Servlet的初始化参数 * */ @WebServlet(urlPatterns = { "/ReadConfigServlet" }, initParams = { @WebInitParam(name = "name", value = "编程帮"), @WebInitParam(name = "URL", value = "www.lmcjl.com") }) public class ReadConfigServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter writer = response.getWriter(); // 获取ServletConfig对象 ServletConfig config = getServletConfig(); // 获取servletName String servletName = config.getServletName(); // 返回 servlet 的初始化参数的名称的集合 Enumeration<String> initParameterNames = config.getInitParameterNames(); // 遍历集合获取初始化参数名称 while (initParameterNames.hasMoreElements()) { // 获取初始化参数名称 String initParamName = initParameterNames.nextElement(); // 获取相应的初始参数的值 String initParamValue = config.getInitParameter(initParamName); // 向页面输出 writer.write(initParamName + " : " + initParamValue + "<br/>"); } // 关闭流 writer.close(); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
本文链接:http://task.lmcjl.com/news/13486.html