zoukankan      html  css  js  c++  java
  • [原创]java WEB学习笔记11:HttpServlet(HttpServletRequest HttpServletRsponse) 以及关于 Servlet 小结

    本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用

    内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系。

    本人互联网技术爱好者,互联网技术发烧友

    微博:伊直都在0221

    QQ:951226918

    -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

     1.HttpServlet介绍(具体方法可参考:java WEB学习笔记08:HttpServletRequest & ServletRequest) HttpServletResponse

      1)public   abstract  class   HttpServlet     extends  GenericServlet    implements   java.io.Serializable  是继承GenericServlet 的一个针对HTTP 协议定制的Servlet。

      2)在service()方法中直接把ServletRequest 和 ServletResponse 转为 HttpServletRequest 和 HttpServletResponse,并且调用了重载的service(HttpServletRequest req, HttpServletResponse resp)

         获取请求方式:request.getMethod().根据请求方式又创建了doXxx()方法(xxx 为具体的请求方式,比如doGet, doPost)

     1  @Override
     2     public void service(ServletRequest req, ServletResponse res)
     3         throws ServletException, IOException {
     4 
     5         HttpServletRequest  request;
     6         HttpServletResponse response;
     7 
     8         try {
     9             request = (HttpServletRequest) req;
    10             response = (HttpServletResponse) res;
    11         } catch (ClassCastException e) {
    12             throw new ServletException("non-HTTP request or response");
    13         }
    14         service(request, response);
    15     
    16 
    17 protected void service(HttpServletRequest req, HttpServletResponse resp)
    18         throws ServletException, IOException {
    19 
    20         String method = req.getMethod();
    21 
    22         if (method.equals(METHOD_GET)) {
    23             long lastModified = getLastModified(req);
    24             if (lastModified == -1) {
    25                 // servlet doesn't support if-modified-since, no reason
    26                 // to go through further expensive logic
    27                 doGet(req, resp);
    28             } else {
    29                 long ifModifiedSince;
    30                 try {
    31                     ifModifiedSince = req.getDateHeader(HEADER_IFMODSINCE);
    32                 } catch (IllegalArgumentException iae) {
    33                     // Invalid date header - proceed as if none was set
    34                     ifModifiedSince = -1;
    35                 }
    36                 if (ifModifiedSince < (lastModified / 1000 * 1000)) {
    37                     // If the servlet mod time is later, call doGet()
    38                     // Round down to the nearest second for a proper compare
    39                     // A ifModifiedSince of -1 will always be less
    40                     maybeSetLastModified(resp, lastModified);
    41                     doGet(req, resp);
    42                 } else {
    43                     resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
    44                 }
    45             }
    46 
    47         } else if (method.equals(METHOD_HEAD)) {
    48             long lastModified = getLastModified(req);
    49             maybeSetLastModified(resp, lastModified);
    50             doHead(req, resp);
    51 
    52         } else if (method.equals(METHOD_POST)) {
    53             doPost(req, resp);
    54 
    55         } else if (method.equals(METHOD_PUT)) {
    56             doPut(req, resp);
    57 
    58         } else if (method.equals(METHOD_DELETE)) {
    59             doDelete(req, resp);
    60 
    61         } else if (method.equals(METHOD_OPTIONS)) {
    62             doOptions(req,resp);
    63 
    64         } else if (method.equals(METHOD_TRACE)) {
    65             doTrace(req,resp);
    66 
    67         } else {
    68             //
    69             // Note that this means NO servlet supports whatever
    70             // method was requested, anywhere on this server.
    71             //
    72 
    73             String errMsg = lStrings.getString("http.method_not_implemented");
    74             Object[] errArgs = new Object[1];
    75             errArgs[0] = method;
    76             errMsg = MessageFormat.format(errMsg, errArgs);
    77 
    78             resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED, errMsg);
    79         }
    80     }

    3.一般在实际开发中,直接继承HttpServlet,并根据请求方法doXxx() 方法接口

     优点:直接针对性的覆盖doXxx() 方法;直接使用 HttpServletRequest 和  HttpServletResponse 

     4.关于Servlet小结

      1)了解GenericServlet 原理和实现 

      2)掌握 HttpServlet 实现的原理 及 源码 

  • 相关阅读:
    Redis主从,集群部署及迁移
    Nginx跨域了解及模拟和解决
    app管理平台 app-host
    FastDFS文件系统使用经验
    FastDFS文件系统迁移和数据恢复
    从单体架构到微服务架构演进
    配置中心之Nacos简介,使用及Go简单集成
    从单体应用到微服务开发旅程
    写DockerFile的一些技巧
    QPS,TPS,并发用户数,吞吐量关系
  • 原文地址:https://www.cnblogs.com/jasonHome/p/5506922.html
Copyright © 2011-2022 走看看