GenericServlet 抽象类源码学习

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();
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值