Servlet(三)----Servlet体系与HTTP

## Servlet的体系结构

  Servlet  ---  接口

    |

    |

  GenericServlet  ---  抽象类

    |

    |

  HttpServlet  --  抽象类

  GenericServlet:将Servlet接口中其他的方法做了默认空实现,只将service()方法作为抽象

    *  将来定义Servlet类时,可以继承GenericServlet,实现Service()方法即可。

  HttpServlet:对HTTP协议的一种封装,简化操作

    1、定义类继承HttpServlet

    2、复写doGet/doPost方法

##  Servlet相关配置

  1、urlpartten:Servlet的访问路径

    *  一个Servlet可以定义多个访问路径:  @WebServlet({"/d1","/d2"})

    *  路径定义规则:

      1、/xxx

      2、/xxx/xxx:多层路径,目录结构

      3、*.do

package com.ftj.servlet.demo02;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Servlet路径配置
 */
//@WebServlet({"/d1","/d2"})
    //@WebServlet("/user/*")
    @WebServlet("*.do")
public class HttpServletDemo02 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}

##  HTTP:Hyper Text Transfer Protocol  超文本传输协议

 *概念:

  *  传输协议:定义了客户端和服务器端通信时,发送数据的格式。

  *  特点

    1、基于TCP/IP的高级协议

    2、默认端口号:80

      http://www.baidu.com:80

    3、基于请求/响应模型:一次请求对应一次响应

    4、无状态的:每次请求之间相互独立,不能交互数据

  *  历史版本:

    1、1.0:每次响应都会建立新的连接

    2、1.1:复用连接

 *  请求消息数据格式

  1、请求行

    请求方式  请求URL  请求协议/版本

    GET  /login.html  HTTP/1.1

    *  请求方式:

      *  HPPT协议有7种请求方式,常用的有2种

        *  GET:

        1、请求参数在请求行中,在URL后。

        2、请求的url长度有限制

        3、不太安全

        *  POST:

        1、请求参数在请求体中

        2、请求的url长度没有限制

        3、相对安全

  2、请求头:客户端浏览器告诉服务器一些信息

    请求头名称:请求头值

    *  常见的请求头:

      1、User-Agent:浏览器告诉服务器,我访问你使用的浏览器版本信息

        *  可以在服务器端获取该头的信息,解决浏览器兼容问题

      2、Referer:http://localhost/login.html

        *  告诉服务器,我(当前请求)从哪里来?

          *  作用:  

          1、防盗链

          2、统计工作

  3、请求空行

    空行:用户分割POST请求的请求头和请求体的

  4、请求体(正文):封装POST请求消息的请求参数的

  *  字符串格式:

  1 GET/sample.jspHTTP/1.1
  2 Accept:image/gif.image/jpeg,*/*
  3 Accept-Language:zh-cn
  4 Connection:Keep-Alive
  5 Host:localhost
  6 User-Agent:Mozila/4.0(compatible;MSIE5.01;Window NT5.0)
  7 Accept-Encoding:gzip,deflate
  8
  9 username=jinqiao&password=1234

 *  响应消息数据格式

That which doesn't kill me makes me stronger!
原文地址:https://www.cnblogs.com/21seu-ftj/p/12445143.html