GenericServlet是一个抽象类,实现了Servlet接口和ServletConfig(以及Serializable),已经完成以下: 1、将init方法中ServletConfig对象赋给一个类变量,以便它能通过调用getServletConfig来获取 2、为Servlet接口中的所有方法都提供默认的实现 3、提供调用ServletConfig对象中所有方法的方法 实例: import java.io.IOException; import java.io.PrintWriter; import javax.servlet.GenericServlet; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.annotation.WebInitParam; import javax.servlet.annotation.WebServlet; /** * Servlet implementation class GenericServletDemoServlet */ @WebServlet( name="GenericServletDemoServlet",urlPatterns="/genric", initParams={ @WebInitParam(name="admin",value="刘"), @WebInitParam(name="email",value="liukunyao@OO.com") } ) public class GenericServletDemoServlet extends GenericServlet { private static final long serialVersionUID = 1L; /** * @see GenericServlet#GenericServlet() */ public GenericServletDemoServlet() { super(); // TODO Auto-generated constructor stub } /** * @see Servlet#service(ServletRequest request, ServletResponse response) */ public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException { ServletConfig servletConfig = getServletConfig(); String admin = servletConfig.getInitParameter("admin"); String email = servletConfig.getInitParameter("email"); response.setCharacterEncoding("utf-8"); PrintWriter writer = response.getWriter(); writer.print("<html>" +"<head>" +"<meta charset='utf-8'>" + "</head>" +"<body>" +"admin:"+admin+"<br>email:"+email + "</body>" + "</html>"); } }