Servlet的基本认识

本内容主要来源于《看透Spring MVC源码分析与实践——韩路彪》一书

Servlet是server+Applet的缩写,表示一个服务器的应用。Servlet其实就是一套规范。

一、servlet接口

  Servlet3.1中servlet接口定义如下:

public interface Servlet {

   //init方法在容器启动时被容器调用(当load-on-startup设置为负数或者不设置时会在Servlet第一次用到时才被调用),只会调用一次。
   public void init(ServletConfig config) throws ServletException;    //getServletConfig方法用于获取ServletConfig。
   public ServletConfig geServletConfig();    //service方法用于处理一个请求。
   public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException;    //getServletInfo方法可以获取一些Servlet相关的信息,这个方法需要自己去实现,默认返回空字符串。
   public String getServletInfo();    //destroy方法用于在Servlet销毁(一般指关闭服务器)时释放一些资源,也只会调用一次。
   public void destroy();
}

  1.1init方法:

    init方法被调用时会接受到一个ServletConfig(Servlet的配置)类型的参数,是容器传进去的。我们在web.xml中定义Servlet时通过init-param标签配置的参数就是通过ServletConfig来保存的。

  1.2 getServletConfig 方法:

  1.3 service方法:

  1.4 getServletInfo方法:

  1.5 destroy方法:

二、ServletConfig接口:

  ServletConfig接口的定义如下:

public interface ServletConfig {

    //获取Servlet的名字,也就是在web.xml中定义的servlet-name
public String getServletName(); //返回的ServletContext代表着我们这个应用本身。其实就是Tomcat中Context的门面类ApplicationContextFacade。
public ServletContext getServletContext(); //用于获取init-param配置的参数
public String getInitParameter(String name); //用于获取配置的所有init-param的名字的集合
public Enumeration<String> getInitParameterNames();
}

  其中方法getServletContext返回值ServletContext代表了应用本身,所以ServletContext里边设置的参数就可以被当前应用的所有的Servlet所共享。

  我们做项目的时候都知道参数可以保存在session中,也可以保存在application中,而servlet的参数就是保存在servletContext中。

  可以这么理解,ServletConfig是Servlet级的,ServletContext是Context级的。当然ServletContext的功能非常强大,不只是保存配置参数。

  在ServletContext的接口中有一个方法:public Servlet getContext(String uripath),它可以根据路径获取到同一个站点下的别的应用的ServletContext,由于安全的原因,一般返回的是null。

三、GenericServlet抽象类:

public abstract class GenericServlet implements Servlet, ServletConfig,
java.io.Serializable {
}

  GenericServlet是Servlet的默认实现,主要做了三件事:

    1.实现了ServletConfig的接口。

      我们可以直接调用ServletConfig里面的方法。因此当我们需要获取ServletContext时就可以直接调用getServletConfig(),而不用调用getServletConfig().getServletContext()了。

    2.提供了无参的init方法。

      GenreicServlet实现了Servlet的init(ServletConfig config)方法,将里面的config赋值给了自身的内部变量config,然后调用了无参的init方法,这个无参的init()方法是个模板方法,可以在子类中通过覆盖它来完成自己的初始化工作。

    @Override
public void init(ServletConfig config) throws ServletException {
this.config = config;
this.init();
}
public void init() throws ServletException {
// NOOP by default
}

    3.提供了两个log方法。

      一个是记录日志,一个是记录异常。

//记录日志
public void log(String msg) {
getServletContext().log(getServletName() + ": " + msg);
}
//记录异常
public void log(String message, Throwable t) {
getServletContext().log(getServletName() + ": " + message, t);
}

  一般我们都是有自己的日志处理方式,所以用的不多。而且GenreicServlet是与具体协议无关的。

四、HttpServlet抽象类:

  HttpServlet使用HTTP协议实现的Servlet的基类,因此在我们写Servlet的时候直接继承HttpServlet就可以了。Spring MVC中的DispatcherServlet就是继承的HttpServlet。

  因为是和协议相关的,所以这里就比较关心请求的事情了。在HttpServlet中主要重写了service方法,将父类中(Servlet接口)service方法的参数ServletRequest和ServletResponse转换为HttpServletRequest和HttpServletResponse。

//重写父类的方法
@Override
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException { HttpServletRequest request;
HttpServletResponse response; try {
//强转参数
request = (HttpServletRequest) req;
response = (HttpServletResponse) res;
} catch (ClassCastException e) {
//如果请求类型不相符,则抛出异常
throw new ServletException("non-HTTP request or response");
}
//调用本类中http的处理方法
service(request, response);
} //本类中方法
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException { //获取请求方法类型
String method = req.getMethod(); //根据不同的请求的方法调用不同的处理方法
if (method.equals(METHOD_GET)) {
long lastModified = getLastModified(req);
if (lastModified == -1) {
// servlet doesn't support if-modified-since, no reason
// to go through further expensive logic
doGet(req, resp);
} else {
long ifModifiedSince;
try {
ifModifiedSince = req.getDateHeader(HEADER_IFMODSINCE);
} catch (IllegalArgumentException iae) {
// Invalid date header - proceed as if none was set
ifModifiedSince = -1;
}
if (ifModifiedSince < (lastModified / 1000 * 1000)) {
// If the servlet mod time is later, call doGet()
// Round down to the nearest second for a proper compare
// A ifModifiedSince of -1 will always be less
maybeSetLastModified(resp, lastModified);
doGet(req, resp);
} else {
resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
}
}
} else if (method.equals(METHOD_HEAD)) {
long lastModified = getLastModified(req);
maybeSetLastModified(resp, lastModified);
doHead(req, resp);
} else if (method.equals(METHOD_POST)) {
doPost(req, resp);
} else if (method.equals(METHOD_PUT)) {
doPut(req, resp);
} else if (method.equals(METHOD_DELETE)) {
doDelete(req, resp);
} else if (method.equals(METHOD_OPTIONS)) {
doOptions(req,resp);
} else if (method.equals(METHOD_TRACE)) {
doTrace(req,resp);
} else {
//
// Note that this means NO servlet supports whatever
// method was requested, anywhere on this server.
//
String errMsg = lStrings.getString("http.method_not_implemented");
Object[] errArgs = new Object[1];
errArgs[0] = method;
errMsg = MessageFormat.format(errMsg, errArgs); resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED, errMsg);
}
}

  这里只是将Servlet中的一些源码做些分析。

servlet-servlet的简单认识——源码解析的更多相关文章

  1. KBEngine简单RPG-Demo源码解析(1)

    一:环境搭建1. 确保已经下载过KBEngine服务端引擎,如果没有下载请先下载          下载服务端源码(KBEngine):              https://github.com ...

  2. SpringMVC一点简单地源码解析

    . 1.1 init(初始化) 在第一次发出请求时,会调用HttpServletBean 的init()方法 org.springframework.web.servlet.HttpServletBe ...

  3. KBEngine简单RPG-Demo源码解析(2)

    七:服务端资产库文件夹结构http://kbengine.org/cn/docs/concepts/directorys.html看assets, 注意:demo使用的不是默认的assets资产目录, ...

  4. KBEngine简单RPG-Demo源码解析(3)

    十四:在世界中投放NPC/MonsterSpace的cell创建完毕之后, 引擎会调用base上的Space实体, 告知已经获得了cell(onGetCell),那么我们确认cell部分创建好了之后就 ...

  5. 设计模式-简单工厂Coding+jdk源码解析

    感谢慕课geely老师的设计模式课程,本套设计模式的所有内容均以课程为参考. 前面的软件设计七大原则,目前只有理论这块,因为最近参与项目重构,暂时没有时间把Coding的代码按照设计思路一点点写出来. ...

  6. 简单理解 OAuth 2.0 及资料收集,IdentityServer4 部分源码解析

    简单理解 OAuth 2.0 及资料收集,IdentityServer4 部分源码解析 虽然经常用 OAuth 2.0,但是原理却不曾了解,印象里觉得很简单,请求跳来跳去,今天看完相关介绍,就来捋一捋 ...

  7. springMVC源码解析--ViewResolver视图解析器执行(三)

    之前两篇博客springMVC源码分析--ViewResolver视图解析器(一)和springMVC源码解析--ViewResolverComposite视图解析器集合(二)中我们已经简单介绍了一些 ...

  8. Spring-Session实现Session共享实现原理以及源码解析

    知其然,还要知其所以然 ! 本篇介绍Spring-Session的整个实现的原理.以及对核心的源码进行简单的介绍! 实现原理介绍 实现原理这里简单说明描述: 就是当Web服务器接收到http请求后,当 ...

  9. Spring5源码解析-论Spring DispatcherServlet的生命周期

    Spring Web框架架构的主要部分是DispatcherServlet.也就是本文中重点介绍的对象. 在本文的第一部分中,我们将看到基于Spring的DispatcherServlet的主要概念: ...

随机推荐

  1. 深度学习卷积网络中反卷积/转置卷积的理解 transposed conv/deconv

    搞明白了卷积网络中所谓deconv到底是个什么东西后,不写下来怕又忘记,根据参考资料,加上我自己的理解,记录在这篇博客里. 先来规范表达 为了方便理解,本文出现的举例情况都是2D矩阵卷积,卷积输入和核 ...

  2. docker下创建crontab定时任务失败

    创建过程 基础镜像采用的centos7.2,需要安装一下crontab,在dockerfile中加以下语句就可以了: # crontab jobs RUN yum -y install crontab ...

  3. 链接学习之obj文件探索

    Windows的gcc环境,往官网http://sourceforge.net/project/showfiles.php?group_id=2435 下载MinGW,安装,安装完毕后按照包 配置环境 ...

  4. 【iCore4 双核心板_uC/OS-II】例程九:消息队列

    一.实验说明: 前面介绍通过信息传递可以进行任务间的交流,信息也可以直接发送给一个任务,在uC/OS-II中每一个任务在它们内部都有一个消息队列,也即任务消息队列,用户可以直接给一个任务发送消息,不需 ...

  5. 【转】C# 高性能 TCP 服务的多种实现方式

    原文链接: http://www.cnblogs.com/gaochundong/p/csharp_tcp_service_models.html 开源库: https://github.com/ga ...

  6. ASP.NET Web Forms - 网站导航(Sitemap 文件)

    [参考]ASP.NET Web Forms - 导航 ASP.NET 带有内建的导航控件. 网站导航 维护大型网站的菜单是困难而且费时的. 在 ASP.NET 中,菜单可存储在文件中,这样易于维护.文 ...

  7. tensorflow, TypeError:'Tensor' object is not callable

    解决办法:两个tensor相乘,需要加“*”.

  8. windows下设置PHP环境变量

    1.找到“高级系统设置”(二选一的方法找到环境变量) ① 我的电脑-属性-高级-环境变量 ②win8,10 直接在搜索框搜 “查看高级系统设置”-环境变量 2.找到变量"Path" ...

  9. java.lang.ClassCastException:weblogic.xml.jaxp.RegistryDocumentBuilderFactory cannot be cast to javax.xml.parsers.DocumentBuilderFactory

    java.lang.ClassCastException:weblogic.xml.jaxp.RegistryDocumentBuilderFactory cannot be cast to java ...

  10. OSG相关扩展工程

    https://blog.csdn.net/wang15061955806/article/details/51003803 OSG的相关扩展,OSG针对每个特定应用,也有很多的开发者进行开发和完善, ...