共计 3042 个字符,预计需要花费 8 分钟才能阅读完成。
一、GenericServlet 概述
GenericServlet 是 Servlet 接口的实现类,我们可以通过继承 GenericServlet 来编写自己的 Servlet。
自己定义的增强 Servlet 实现类
public class BServlet implements Servlet {private ServletConfig config; | |
/* | |
* 需要就写,不需要就不写 | |
*/ | |
public void destroy() {System.out.println("啊~ 我要死了!"); | |
} | |
/* | |
* 请放心,这个方法一定会在 init() 方法之后被调用!* init() 被调用后,本类的成员 this.config 已经有值了!*/ | |
public ServletConfig getServletConfig() {return this.config; | |
} | |
/* | |
* 没有用的东西,爱实现不实现 | |
*/ | |
public String getServletInfo() {return "我是一个快乐的 Servlet"; | |
} | |
/* | |
* 由 Tomcat 来调用,并且只调用一次 | |
* 它是这些方法中第一个被调用的,它会在构造器之后马上被调用!*/ | |
public void init(ServletConfig config) throws ServletException {// 把 tomcat 传递的 Servletconfig 赋值给本类的一个成员,其实就是把它保存起来,方便在其他方法中使用! | |
this.config = config; | |
init();} | |
/* | |
* 这个方法是本类自己定义的!不是 Servlet 接口中的方法 | |
*/ | |
public void init() { } | |
public void service(ServletRequest arg0, ServletResponse arg1) | |
throws ServletException, IOException {/* | |
* 这里是否可以使用 ServletConfig 的成员呢?*/ | |
System.out.println("每次处理请求都会被调用!"); | |
} | |
public ServletContext getServletContext() {return config.getServletContext();} | |
public String getServletName() {return config.getServletName();} | |
public String getInitParameter(String name) {return config.getInitParameter(name); | |
} | |
} |
下面是 GenericServlet 类的源代码:
GenericServlet.java
public abstract class GenericServlet implements Servlet, ServletConfig, | |
java.io.Serializable {private static final long serialVersionUID = 1L; | |
private transient ServletConfig config; | |
public GenericServlet() {} | |
public void destroy() {} | |
public String getInitParameter(String name) {return getServletConfig().getInitParameter(name); | |
} | |
public Enumeration<String> getInitParameterNames() {return getServletConfig().getInitParameterNames(); | |
} | |
public ServletConfig getServletConfig() {return config; | |
} | |
public ServletContext getServletContext() {return getServletConfig().getServletContext(); | |
} | |
public String getServletInfo() {return ""; | |
} | |
/* | |
实现了 Servlet 的 init(ServletConfig) 方法,把参数 config 赋给了本类的成员 config,然后再调用本类自己的无参的 init() 方法。*/ | |
public void init (ServletConfig config) throws ServletException {this.config = config; | |
this.init();} | |
/* | |
这个方法是 GenericServlet 自己的方法,而不是从 Servlet 继承下来的。当我们自定义 Servlet 时,如果想完成初始化作用就不要再重复 init(ServletConfig) 方法了,而是应 该去重写 init() 方法。因为在 GenericServlet 中的 init(ServletConfig) 方法中保存 了 ServletConfig 对象,如果覆盖了保存 ServletConfig 的代码,那么就不能再使用 ServletConfig 了。*/ | |
public void init () throws ServletException {} | |
public void log(String msg) {getServletContext().log(getServletName() + ":" + msg); | |
} | |
public void log(String message, Throwable t) {getServletContext().log(getServletName() + ":" + message, t); | |
} | |
public abstract void service(ServletRequest req, ServletResponse res) | |
throws ServletException, IOException; | |
public String getServletName() {return config.getServletName();} | |
} |
二 GenericServlet 的 init() 方法
在 GenericServlet 中,定义了一个 ServletConfig config 实例变量,并在 init(ServletConfig) 方法中把参数 ServletConfig 赋给了实例变量。然后在该类的很多方法中使用了实例变量 config。
如果子类覆盖了 GenericServlet 的 init(StringConfig) 方法,那么 this.config=config 这一条语句就会被覆盖了,也就是说 GenericServlet 的实例变量 config 的值为 null,那么所有依赖 config 的方法都不能使用了。如果真的希望完成一些初始化操作,那么去覆盖 GenericServlet 提供的 init() 方法,它是没有参数的 init() 方法,它会在 init(ServletConfig) 方法中被调用。
三、实现了 ServletConfig 接口
GenericServlet 还实现了 ServletConfig 接口,所以可以直接调用 getInitParameter()、getServletContext() 等 ServletConfig 的方法。
正文完
星哥玩云-微信公众号
