Web核心-Servlet

Servlet其实是ServerApplet--小服务程序或服务链接器,用Java编写的服务器端程序,主要功能在于交互式地浏览和修改数据,生成动态Web内容。与经常使用的协议,如DNS,TCP/IP,HTTP相似,Servlet是做为一整套规范存在的;同时做为J2EE标准的一部分,定义了javaweb开发的标准。Servlet制定了java处理WEB请求的一系列标准,咱们只须要按照标准规定的去作就能够了。
实际上,不管是Struts2的FilterDispatcher仍是SpringMvc的DispatcherServlet,其底层都是经过实现Sevlet或者Servlet类型的扩展【如:GenericServlet】来实现的。java

1.Servlet接口

下图为Servlet3.1中的结构图:
web


由于Servlet是以规范的方式存在的,实际上就是定义一系列规范接口。在Servlet接口中,主要包括如下几个接口:

1)init方法是在容器启动时被容器调用,且只会被调用一次;
2)getServletConfig方法用于获取ServletConfig;
3)service方法用于处理一个具体的请求
4)getServletInfo方法用于获取Servlet相关的信息:版权等。
5)destroy方法用来销毁一个Servlet,和init同样,只会被调用一次,通常在服务器关闭时用于释放一些资源。

init方法调用时会接受一个ServletConfig类型的参数,用于初始化Servlet,由容器传入。ServletConfig,顾名思义,其包含了Serlvet的配置信息。一般状况下,咱们在web.xml文件中定义Serlvet时,会经过init-param标签来进行参数配置。在Springmvc的配置中,一般经过如下方式来配置参数:
spring

2.ServletConfig接口


1)getServletName用于获取Servlet的名字,也就是咱们在web.xml中定义的servlet-name
2)getServletContext返回ServletContext,表明咱们当前应用自己
3)getInitParameter用于获取init-param配置的参数
4)getInitParameterNames用于获取全部init-param配置名字的集合
ServletContext和ServletConfig最多见的使用就是传递初始化参数。来看下spring中的contextConfigServlet的参数配置

经过context-param配置的contextConfigLocation配置到了ServletContext中,再经过Servlet下的init-param配置的contextConfigLocation配置到ServletConfig中,在Servlet中能够经过getInitParameter方法获取具体的信息。

3.GenericServlet

GenericServlet是Servlet的默认实现,代码以下:bash

package javax.servlet;
import java.io.IOException;
import java.io.Serializable;
import java.util.Enumeration;
import java.util.ResourceBundle;
public abstract class GenericServlet
  implements Servlet, ServletConfig, Serializable
{
  private static final String LSTRING_FILE = "javax.servlet.LocalStrings";
  private static ResourceBundle lStrings = ResourceBundle.getBundle("javax.servlet.LocalStrings");
  private transient ServletConfig config;
  public void destroy()
  {
  }
  public String getInitParameter(String name)
  {
    ServletConfig sc = getServletConfig();
    if (sc == null) {
      throw new IllegalStateException(lStrings.getString("err.servlet_config_not_initialized"));
    }
    return sc.getInitParameter(name);
  }
  public Enumeration getInitParameterNames()
  {
    ServletConfig sc = getServletConfig();
    if (sc == null) {
      throw new IllegalStateException(lStrings.getString("err.servlet_config_not_initialized"));
    }
    return sc.getInitParameterNames();
  }
  public ServletConfig getServletConfig()
  {
    return this.config;
  }
  public ServletContext getServletContext()
  {
    ServletConfig sc = getServletConfig();
    if (sc == null) {
      throw new IllegalStateException(lStrings.getString("err.servlet_config_not_initialized"));
    }
    return sc.getServletContext();
  }
  public String getServletInfo()
  {
    return "";
  }
  public void init(ServletConfig config)
    throws ServletException
  {
    this.config = config;
    init();
  }
  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 paramServletRequest, ServletResponse paramServletResponse)
    throws ServletException, IOException;
  public String getServletName()
  {
    ServletConfig sc = getServletConfig();
    if (sc == null) {
      throw new IllegalStateException(lStrings.getString("err.servlet_config_not_initialized"));
    }
    return sc.getServletName();
  }
}复制代码

从其继承和实现关系来看,GenericServlet主要作了3件事:
1.实现了ServletConfig接口,这样咱们就能够直接调用ServletConfig里面的方法;
GenericServlet实现了ServletConfig,能够在须要的时候直接调用ServletConfig中的方法,不须要再先获取ServletConfig对象;好比,获取ServletContext的时候能够直接调用getServletContext,而无需调用getServletConfig().getServletContext(),可是实际上,其底层的内部实现仍是在内部仍是进行了getServletConfig().getServletContext()的调用。
服务器


2.提供了无参的init方法
GenericServlet实现了Servlet的init(ServletConfig config)方法,在里面将config设置给了其内部变量config,而后调用了无参的init方法;此方法能够在子类中经过覆盖它来完成初始化工做。

这种方式具备的有点包括如下几点:
a.config设置为内部属性,这样能够在ServletConfig的接口方法中直接调用Config的相应方法来执行;
b.咱们在写Serlvet的时候能够不用再关心Config,只须要执行本身的初始化逻辑便可
c.在重写init方法时,不须要再调用super.init(config)。
3.提供了Log方法
GenericServlet提供了2个log方法,一个用于记录日志,一个用于记录异常。其具体的实现是经过传给ServletConfig的日志实现的。
GenericServlet是与具体协议无关的。

4.HttpServlet

HttpServlet是基于Http协议实现的Servlet的基类,写Servlet时直接继承HttpServlet便可,不须要再重头实现Servlet接口,SpringMvc中的dispatcherServlet就是HttpServlet的子类。 HttpServlet是与Http协议相关的,HttpServlet处理请求主要是经过重写父类的service方法来完成具体的请求处理的。在service方法中首先是将ServletRequest和ServletResponse转换成HttpServletRequest和HttpServletResponse,而后根据请求的不一样路由到不一样的处理过程当中去【处理方法就是咱们常见的doXXX的方法。最多见的就是doGet和doPost】
mvc

相关文章
相关标签/搜索