import javax.servlet.*;
import java.io.IOException;
import java.io.Serializable;
import java.util.Enumeration;
import java.util.ResourceBundle;
/**
* 说明:通用Servlet抽象类
* 这个类将 Servlet 和 ServletConfig 接口同时实现
* 继承该类相当于同时实现了Servlet和ServletConfig接口
* */
public abstract class GenericServlet implements Servlet, ServletConfig, Serializable {
private static final String LSTRING_FILE = "javax.servlet.LocalStrings"; // 配置文件路径
// ResourceBundle 保存配置文件中的信息
private static ResourceBundle lStrings = ResourceBundle.getBundle("javax.servlet.LocalStrings");
/**
* 属性前添加关键字transient,序列化对象的时候,这个属性就不会被序列化。
* */
private transient ServletConfig config;
public GenericServlet() {
}
public void destroy() {
}
/**
* 说明:调用初始化在web.xml中存放的参数值。
* 在web.xml配置文件中配置一个servlet初始参数如下:
* <init-param>
* <param-name>name</param-name>
* <param-value>zhangsan</param-value>
* </init-param>
* @param name 对应<param-name>中的值
* @return String 返回<param-value>中的值
* */
public String getInitParameter(String name) {
ServletConfig sc = this.getServletConfig();
if (sc == null) {
throw new IllegalStateException(lStrings.getString("err.servlet_config_not_initialized"));
} else {
return sc.getInitParameter(name);
}
}
/**
* 说明:获取初始化在web.xml中存放的参数集合
* @return Enumeration<String>
* */
public Enumeration<String> getInitParameterNames() {
ServletConfig sc = this.getServletConfig();
if (sc == null) {
throw new IllegalStateException(lStrings.getString("err.servlet_config_not_initialized"));
} else {
return sc.getInitParameterNames();
}
}
/**
* 说明:在servlet初始化时,容器传递进来一个ServletConfig对象并保存在servlet实例中,
* 该对象允许访问两项内容:初始化参数和ServletContext对象,
* 前者通常由容器在文件中指定,允许在运行时向sevrlet传递有关调度信息,
* 比如说getServletConfig().getInitParameter("debug")
* 后者为servlet提供有关容器的信息。此方法可以让servlet在任何时候获得该对象及配置信息。
* @return ServletConfig
* */
public ServletConfig getServletConfig() {
return this.config;
}
/**
* 说明:一个servlet可以使用getServletContext()方法得到web应用的servletContext
* 即而使用getServletContext的一些方法来获得一些值
* 比如说getServletContext().getRealPath("/")来获得系统绝对路径
* getServletContext().getResource("WEB-INF/config.xml")来获得xml文件的内容
* @return ServletContext
* */
public ServletContext getServletContext() {
ServletConfig sc = this.getServletConfig();
if (sc == null) {
throw new IllegalStateException(lStrings.getString("err.servlet_config_not_initialized"));
} else {
return sc.getServletContext();
}
}
public String getServletInfo() {
return "";
}
/**
* 说明:在 Servlet 的生命期中,仅执行一次 init() 方法,它是在服务器装入 Servlet 时执行的。
* */
public void init(ServletConfig config) throws ServletException {
this.config = config;
this.init();
}
public void init() throws ServletException {
}
/**
* 说明:将指定信息写入Servlet日志文件中
* */
public void log(String msg) {
this.getServletContext().log(this.getServletName() + ": " + msg);
}
public void log(String message, Throwable t) {
this.getServletContext().log(this.getServletName() + ": " + message, t);
}
/**
* 说明:没有做具体实现,需要子类去实现,具体详见Servlet接口篇
* */
public abstract void service(ServletRequest var1, ServletResponse var2) throws ServletException, IOException;
/**
* 说明:顾名思义,获取当前ServletName名称
* @return String
* */
public String getServletName() {
ServletConfig sc = this.getServletConfig();
if (sc == null) {
throw new IllegalStateException(lStrings.getString("err.servlet_config_not_initialized"));
} else {
return sc.getServletName();
}
}
}
GenericServlet 抽象类源码学习
最新推荐文章于 2022-11-29 20:45:00 发布