1.config实质上是ServletConfig的一个实例
2.config常用来获取Servlet的初始化参数
web.xml
index.jsp config com.test.servlet.Config name 小翁 age 22 config /config
Config.java
package com.test.servlet;import java.io.IOException; import java.io.PrintWriter;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 Config extends HttpServlet { /** * */ private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/html;charset=utf-8"); ServletConfig config = getServletConfig(); String name = config.getInitParameter("name"); String age = config.getInitParameter("age"); PrintWriter out = resp.getWriter(); out.println(name+"今年"+age+"岁!"); } }