HTTP & request & response

HTTP

  • The concept: Hyper Text Transfer Protocol Hypertext Transfer Protocol
  • Transfer Protocol: define, when the client and server communications, data transmission format
  •    特点:
        	1. 基于 TCP/IP 的高级协议
      		2. 默认端口号:80
      		3. 基于请求/响应模型的:一次请求对应一次响应
      		4. 无状态的:每次请求之间相互独立,不能交互数据					 			
      	历史版本:
      		1.0:每一次请求响应都会建立新的连接
      	    1.1:复用连接
    
  • A data request message format
  1. The request line
    request method request url request protocol / version
    GET /login.html HTTP / 1.1

     * 请求方式:	 
       HTTP 协议有 7 中请求方式,常用的有 2 种:
      GET:1. 请求参数在请求行中,在 url 后;
           2. 请求的 url 长度有限制的
           3. 不太安全
      POST:1. 请求参数在请求体中;
      		 2. 请求的 url 长度没有限制的;
     		 3. 相对安全
    
  2. Request header: the client browser to tell some information about the server
    request header name: request header value

    常见的请求头:
     1. User-Agent:浏览器告诉服务器,我访问你使用的浏览器版本信息;
      可以在服务器端获取该头的信息,解决浏览器的兼容性问题
     2. Referer: http://localhost/login.html
     	告诉服务器,我(当前请求)从哪里来?
       作用:1. 防盗链:2. 统计工作:
    
  3. Request blank lines
    blank lines, that is, for dividing the request header POST request, and the request body.

  4. Request body (body):
    * Packaging POST Request message is parameter (GET request without thereof)

  • A response message: The server sends to the client data * data format:
  1. Response line

     1. 组成:协议/版本 响应状态码 状态码描述
     2. 响应状态码:服务器告诉客户端浏览器本次请求和响应的一个状态。
     	1. 状态码都是 3 位数字
     	2. 分类:
     		1. 1xx:服务器就收客户端消息,但没有接受完成,等待一段时间后,发送 1xx 多状态码
     		2. 2xx:成功。代表: 200
     		3. 3xx:重定向。代表: 302(重定向), 304(访问缓存)
     		4. 4xx:客户端错误。
     			* 代表:
     				* 404(请求路径没有对应的资源)
     				* 405:请求方式没有对应的 doXxx 方法
     		5. 5xx:服务器端错误。代表: 500(服务器内部出现异常)
    
  2. Response headers:

     1. 格式:头名称: 值
     2. 常见的响应头:
     	1. Content-Type:服务器告诉客户端本次响应体数据格式以及编码格式
     	2. Content-disposition:服务器告诉客户端以什么格式打开响应体数据
     		* 值:
     			* in-line:默认值,在当前页面内打开
     			* attachment;filename=xxx:
     					 以附件形式打开响应体。文件下载
    
  3. In response blank line

  4. Response body: data transmission

request

  1. request and response objects principles
    1. request and response objects are created by the server. We use them
    2. request is subject to the acquisition request message, response message in response to the object is set to

  2. request object inherits architecture:

     ServletRequest -- 接口
     	| 继承
     HttpServletRequest -- 接口
     	| 实现
     org.apache.catalina.connector.RequestFacade 类(tomcat)
    
  3. request features:

  • A data acquisition request message

    1, row data acquisition request
    GET / day14 / demo1? Name = zhangsan HTTP / 1.1

     	 * 方法:
     	 1. 获取请求方式 : GET
     		* String getMethod()
     	 2. (*)获取虚拟目录: /day14
     		* String getContextPath()
     	 3. 获取 Servlet 路径: /demo1
     		* String getServletPath()
     	 4. 获取 get 方式请求参数: name=zhangsan
     		* String getQueryString()
     	 5. (*)获取请求 URI: /day14/demo1
     		* String getRequestURI(): /day14/demo1
     		* StringBuffer getRequestURL() :http://localhost/day14/demo1
     		* URL:统一资源定位符 : http://localhost/day14/demo1 中华人民共和国
     		* URI:统一资源标识符 : /day14/demo1 共和国
     		6. 获取协议及版本: HTTP/1.1
     		* String getProtocol()
     		7. 获取客户机的 IP 地址:
     		* String getRemoteAddr()
    
  1. Data acquisition request header

     	* 方法:		
     	* (*)String getHeader(String name):
      			通过请求头的名称获取请求头的值
     	* Enumeration<String> getHeaderNames():
      			获取所有的请求头名称
    
  2. The volume data acquisition request:
    * request body: POST request only mode, only the request body,
    encapsulating the POST request request parameters in the request body

     	* 步骤:
     	1. 获取流对象
     		* BufferedReader getReader():获取字符输入流,
     		只能操作字符数据
     		* ServletInputStream getInputStream():获取字节输入流,
     	   可以操作所有类型数据(* 在文件上传知识点后讲解)
     	2. 再从流对象中拿数据
    
  • Other functions:
  1. Acquisition request parameters common way: regardless of get or post request methods can use the following methods to get the request parameters

     	1. String getParameter(String name): 
     		根 据 参 数 名 称 获 取 参 数 值.
     		username=zs&password=123
     	2. String[] getParameterValues(String name):
     		 根 据 参 数 名 称 获 取 参 数 值 的 数 组。
     		hobby=xx&hobby=game
     	3. Enumeration<String> getParameterNames():
     		获取所有请求的参数名称
     	4. Map<String,String[]> getParameterMap():
     		获取所有参数的 map 集合* 
     	中文乱码问题:
     	* get 方式: tomcat 8 已经将 get 方式乱码问题解决了
     	* post 方式:会乱码
     	* 解决:在获取参数前,设置 request 的编码 				 	
     	*request.setCharacterEncoding("utf-8");
    
  2. Forwards the request: a resource in a way to jump inside the server

     1. 步骤:
     	1. 通 过 request 对 象 获 取 请 求 转 发 器 对 象 : 				
     		RequestDispatcher
     		getRequestDispatcher(String path)
     	2. 使 用 RequestDispatcher 对 象 来 进 行 转 发 : 	
     		forward(ServletRequest request,
     		ServletResponse response)
     2. 特点:
     	1. 浏览器地址栏路径不发生变化
     	2. 只能转发到当前服务器内部资源中。
     	3. 转发是一次请求
    
  3. Share data:

     	* 域对象:一个有作用范围的对象,可以在范围内共享数据
     	* request 域:代表一次请求的范围,
     	       一般用于请求转发的多个资源中共享数据
     
     	* 方法:
     	1. void setAttribute(String name,Object obj):存储数据
     	2. Object getAttitude(String name):通过键获取值
     	3. void removeAttribute(String name):通过键移除键值对
    
  4. 获取 ServletContext:
    * ServletContext getServletContext()

response

Function: Setup Response message

  1. Setting response line

     1. 格式: HTTP/1.1 200 ok
     2. 设置状态码: setStatus(int sc)
    
  2. Set response header: setHeader (String name, String value)

  3. Sets response body:

     * 使用步骤:
     	1. 获取输出流
     		* 字符输出流: PrintWriter getWriter()
     		* 字节输出流: ServletOutputStream getOutputStream()
     	2. 使用输出流,将数据输出到客户端浏览器* 
     	3. 
    
Case:
  1. Complete redirection

     * 重定向:资源跳转的方式
     * 代码实现:
     	//1. 设置状态码为 302
     	 response.setStatus(302);
     	//2.设置响应头 location
     	 response.setHeader("location","/day15/responseDemo2");
     	//简单的重定向方法
     	response.sendRedirect("/day15/responseDemo2");
    
  • Redirection features: redirect

      1. 地址栏发生变化
      2. 重定向可以访问其他站点(服务器)的资源
      3. 重定向是两次请求。不能使用 request 对象来共享数据
    
  • Forwarding features: forward

      1. 转发地址栏路径不变
      2. 转发只能访问当前服务器下的资源
      3. 转发是一次请求,可以使用 request 对象来共享数据
    
  • the difference between forward and redirect

      路径写法:
      1. 路径分类
      	1. 相对路径:通过相对路径不可以确定唯一资源
      		* 如: ./index.html
      		* 不以/开头,以.开头路径
      		* 规则:找到当前资源和目标资源之间的相对位置关系
      		* ./:当前目录
      		* ../:后退一级目录
      	2. 绝对路径:通过绝对路径可以确定唯一资源
      		* 如: http://localhost/day15/responseDemo2 			/day15/responseDemo2
      		* 以/开头的路径
      		* 规则:判断定义的路径是给谁用的?判断请求将来从哪儿发出
      		* 给客户端浏览器使用:需要加虚拟目录(项目的访问路径)
      		* 建议虚拟目录动态获取: request.getContextPath()
      		* <a> , <form> 重定向...
      		* 给服务器使用:不需要加虚拟目录* 转发路径
    
  1. Server output character data to the browser

     * 步骤:
     		1. 获取字符输出流
     		2. 输出数据
     * 注意:
     	* 乱码问题:
     		1. PrintWriter pw = response.getWriter();获取的流的默认编码是 ISO-8859-1
     		2. 设置该流的默认编码
     		3. 告诉浏览器响应体使用的编码
     		//简单的形式,设置编码,是在获取流之前设置
     	response.setContentType("text/html;charset=utf-8");
    
  2. Server output byte data to the browser
    * steps:
    1. Obtain the output byte stream
    2. Output data

  3. Verification code

    1. 本质:图片
    2. 目的:防止恶意表单注册
    
Published 11 original articles · won praise 1 · views 149

Guess you like

Origin blog.csdn.net/weixin_45330007/article/details/104483489