目录

什么是http协议 http协议:对浏览器客户端 和 服务器端 之间数据传输的格式规范

查看http协议的工具* 使用火狐的firebug插件(右键->firebug->网络)

  • 使用谷歌的“审查元素”
  • 使用系统自带的telnet工具(远程访问工具)
    • telnet localhost 8080 访问tomcat服务器
    • ctrl+] 回车 可以看到回显
    • 输入请求内容

GET /day09/hello HTTP/1.1 Host: localhost:8080
* 回车,即可查看到服务器响应信息。

http协议内容```html

请求(浏览器-》服务器)

GET /day09/hello HTTP/1.1

Host: localhost:8080

User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8

Accept-Language: zh-cn,en-us;q=0.8,zh;q=0.5,en;q=0.3

Accept-Encoding: gzip, deflate

Connection: keep-alive

```html
响应(服务器-》浏览器)
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Length: 24
Date: Fri, 30 Jan 2015 01:54:57 GMT this is hello servlet!!!

Http请求```html

GET /day09/hello HTTP/1.1 -请求行

Host: localhost:8080 --请求头(多个key-value对象)

User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8

Accept-Language: zh-cn,en-us;q=0.8,zh;q=0.5,en;q=0.3

Accept-Encoding: gzip, deflate

Connection: keep-alive

--一个空行

name=eric&password=123456 --(可选)实体内容



## 请求行 GET /day09/hello HTTP/1.1
### http协议版本* http1.0:当前浏览器客户端与服务器端建立连接之后,只能发送一次请求,一次请求之后连接关闭。
* http1.1:当前浏览器客户端与服务器端建立连接之后,可以在一次连接中发送多次请求。(基本都使用1.1) ### 请求资源* URL: 统一资源定位符。http://localhost:8080/day09/testImg.html。只能定位互联网资源。是URI 的子集。
* URI:统一资源标记符。/day09/hello。用于标记任何资源。可以是本地文件系统,局域网的资源(//192.168.14.10/myweb/index.html), 可以是互联网。
### 请求方式 常见的请求方式: GET 、 POST、 HEAD、 TRACE、 PUT、 CONNECT 、DELETE 常用的请求方式: GET 和 POST 表单提交:
<form action="提交地址" method="GET/POST"> <form> GET vs POST 区别 #### GET方式提交
* 地址栏(URI)会跟上参数数据。以?开头,多个参数之间以&分割。
```html GET /day09/testMethod.html?name=eric&password=123456 HTTP/1.1
Host: localhost:8080
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: zh-cn,en-us;q=0.8,zh;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Referer: http://localhost:8080/day09/testMethod.html
Connection: keep-alive
  • GET提交参数数据有限制,不超过1KB。

  • GET方式不适合提交敏感密码。

  • 注意: 浏览器直接访问的请求,默认提交方式是GET方式

POST方式提交

  • 参数不会跟着URI后面。参数而是跟在请求的实体内容中。没有?开头,多个参数之间以&分割。

POST /day09/testMethod.html HTTP/1.1
Host: localhost:8080
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: zh-cn,en-us;q=0.8,zh;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Referer: http://localhost:8080/day09/testMethod.html
Connection: keep-alive name=eric&password=123456
  • POST提交的参数数据没有限制。
  • POST方式提交敏感数据。

请求头```html

Accept: text/html,image/* -- 浏览器接受的数据类型

Accept-Charset: ISO-8859-1 -- 浏览器接受的编码格式

Accept-Encoding: gzip,compress --浏览器接受的数据压缩格式

Accept-Language: en-us,zh- --浏览器接受的语言

Host: www.it315.org:80 --(必须的)当前请求访问的目标地址(主机:端口)

If-Modified-Since: Tue, 11 Jul 2000 18:23:51 GMT --浏览器最后的缓存时间

Referer: http://www.it315.org/index.jsp -- 当前请求来自于哪里

User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0) --浏览器类型

Cookie:name=eric -- 浏览器保存的cookie信息

Connection: close/Keep-Alive -- 浏览器跟服务器连接状态。close: 连接关闭 keep-alive:保存连接。

Date: Tue, 11 Jul 2000 18:23:51 GMT -- 请求发出的时间



## 3.3 实体内容只有POST提交的参数会放到实体内容中

## 3.4 HttpServletRequest对象>HttpServletRequest对象作用是用于获取请求数据。

核心的API:

    请求行:
request.getMethod(); 请求方式 request.getRequetURI() / request.getRequetURL() 请求资源 request.getProtocol() 请求http协议版本 请求头: request.getHeader("名称") 根据请求头获取请求值 request.getHeaderNames() 获取所有的请求头名称 实体内容: request.getInputStream() 获取实体内容数据 **浏览器默认的提交方式是GET** 但是这并不是通用方法,通用的使用API:
* request.getParameterNames():获取一个集合,里面存放参数名称
* request.getParameter(String):获取指定参数名称的参数值
* request.getParameterValues(String):返回指定参数集合的值 ### Demo:
1.新建Html,输出数据:
```html
<!DOCTYPE html>
<html>
<head>
<title>参数提交</title>
</head> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8"> <body>
<h3>GET方式提交</h3>
<form action="/WebDemo1/RequsetDemo5" method="GET">
用户名:<input type="text" name="name"/><br/>
密码:<input type="password" name="password"/><br/>
<input type="submit" value="提交"/>
</form>
<hr/> <h3>POST方式提交</h3>
<form action="/WebDemo1/RequsetDemo5" method="POST">
用户名:<input type="text" name="name"/><br/>
密码:<input type="password" name="password"/><br/>
<input type="submit" value="提交"/>
</form>
</body>
</html>

2.创建一个Servlet接收数据

package per.liyue.code.getparam;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class RequsetDemo5 extends HttpServlet { /**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("get方式接收参数:");
//
String value = request.getQueryString();
System.out.println(value);
} /**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("使用Post接收参数");
BufferedReader read = new BufferedReader(
new InputStreamReader(
request.getInputStream()));
String lineRead = null;
while (null != (lineRead=read.readLine())) {
System.out.println(lineRead);
}
} }

但是二者读取参数方法不一致, 实际使用时候,有统一方法调用:

package per.liyue.code.getparam;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Enumeration; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class RequsetDemo5 extends HttpServlet { /**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("get方式接收参数-非统一方法:");
//不统一方法
String Query = request.getQueryString();
System.out.println(Query);
System.out.println("get方式接收参数-统一方法:");
Enumeration<String> en = request.getParameterNames();
if (en.hasMoreElements()) {
System.out.print("get到的参数名称:");
//因为html中的编码是utf-8,所以这里要进行转码保证不出错!
String name = new String(en.nextElement().getBytes("iso-8859-1"), "utf-8");
String value = new String(request.getParameter(name).getBytes("iso-8859-1"), "utf-8");
System.out.print("参数名称:" + name + "get到的参数值:" + value);
}
} /**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("使用Post接收参数-非统一方法");
//非统一方法
// BufferedReader read = new BufferedReader(
// new InputStreamReader(
// request.getInputStream()));
// String lineRead = null;
// while (null != (lineRead=read.readLine())) {
// System.out.println(lineRead);
// }
/*
* 使用统一方法后,可以直接调用deGet中已经实现的参数提取
* 并且,post中获取只能一次,也是就上面的非同一方法和下面的方法同时只存在于一个
*/
doGet(request, response);
} }

编码问题在上面代码中已经手动解析,这里还有一个方法:

/*
* 这个方法要在获取参数之前调用才生效,是个全局设定,但是要注意的是:
* 该方法是对实体内容中的数据编码有效,也就是说,post的数据在实体内容中,
* 对post有效。而get的参数在uri后面,所以多get无效!!!
*/
request.setCharacterEncoding("utf-8");

HTTP 响应

HTTP/1.1 200 OK --响应行

Server: Apache-Coyote/1.1 --响应头(key-vaule)

Content-Length: 24

Date: Fri, 30 Jan 2015 01:54:57 GMT --一个空行

this is hello servlet!!! --实体内容

响应行

http协议版本

状态码: 服务器处理请求的结果(状态)

常见的状态:

  • 200 : 表示请求处理完成并完美返回
  • 302: 表示请求需要进一步细化。
  • 404: 表示客户访问的资源找不到。
  • 500: 表示服务器的资源发送错误。(服务器内部错误)

状态描述 ## 常见的响应头>Location: http://www.it315.org/index.jsp -表示重定向的地址,该头和302的状态码一起使用。

Server:apache tomcat ---表示服务器的类型

Content-Encoding: gzip -- 表示服务器发送给浏览器的数据压缩类型

Content-Length: 80 --表示服务器发送给浏览器的数据长度

Content-Language: zh-cn --表示服务器支持的语言

Content-Type: text/html; charset=GB2312 --表示服务器发送给浏览器的数据类型及内容编码

Last-Modified: Tue, 11 Jul 2000 18:23:51 GMT --表示服务器资源的最后修改时间

Refresh: 1;url=http://www.it315.org --表示定时刷新

Content-Disposition: attachment; filename=aaa.zip --表示告诉浏览器以下载方式打开资源(下载文件时用到)

Transfer-Encoding: chunked

Set-Cookie:SS=Q0=5Lb_nQ; path=/search --表示服务器发送给浏览器的cookie信息(会话管理用到)

Expires: -1 --表示通知浏览器不进行缓存

Cache-Control: no-cache

Pragma: no-cache

Connection: close/Keep-Alive --表示服务器和浏览器的连接状态。close:关闭连接 keep-alive:保存连接

HttpServletResponse对象HttpServletResponse对象修改响应信息:

  • 响应行: response.setStatus() 设置状态码

  • 响应头:response.setHeader("name","value") 设置响应头

  • 实体内容:

    • response.getWriter().writer(); 发送字符实体内容

    • response.getOutputStream().writer() 发送字节实体内容

Demo

import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class ResponseDemo1 extends HttpServlet { /**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/*
* Response响应行
*/
//这里返回状态码
response.setStatus(404);
//这里就直接返回了错误码,直接页面报错
response.sendError(404);
/*
* Response响应头
*/
response.setHeader("这里是响应头名称", "这里是响应值");
/*
* Response实体内容
*/
//字符输出
response.getWriter().write("这里是实体的测试-字符");
//字节输出
response.getOutputStream().write("这里是实体的测试-字节".getBytes());
} /**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } }

页面跳转-使用Location头

浏览器识别到302状态码后,会想服务器再发一次请求,请求的地址就是Location的Value值

//发送302状态码
response.setStatus(302);
response.setHeader("Location", "\项目名称\跳转的目标页面.html"); //可以写为,下面这行代码效果等同于上面代码
response.sendRedirect("\项目名称\跳转的目标页面.html");

定时刷新-使用Refresh

/*
* 定时刷新1:
* 浏览器识别refresh,后面的1表示每1秒刷新一次当前页面
*/
response.setHeader("refresh", "1"); /*
* 定时刷新2:
* 定时跳转到指定页面:3秒后刷新到指定的url页面
*/
response.setHeader("refesh", "3;url=\项目名称\具体页面.html");
`` ### 设置服务器发送给浏览器数据类型 ```Java
//定义发送数据类型:
//形式1:
response.setHeader("content-type", "text/html"); //形式2:
response.setContentType("text/html"); //其他类型
xml:"text/xml"
jpg:"image/jpg"

这里不同数据类型的写法,在tomcat的配置文件中有。路径为:$\apache-tomcat-8.0.36\conf\下的标签中

定义下载

//下载
File file = new File("");
response.setHeader("Content-Desposition", "attachment; filename=" + file.getName());

项目中的编码问题

在每个Servlet开头定义上:

request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");

4.HTTP入门.md的更多相关文章

  1. 作业03-面向对象入门.md

    1. 本周学习总结 初学面向对象,会学习到很多碎片化的概念与知识.尝试学会使用思维导图将这些碎片化的概念.知识点组织起来.请使用工具画出本周学习到的知识点及知识点之间的联系.步骤如下: 1.1 写出你 ...

  2. Android Studio单元测试入门

    Android Studio单元测试入门 通常在开发Android app的时候经常会写一些小函数并验证它是否运行正确,通常做法我们是把这个函数放到某个界面(Activity上)执行一下,运行整个工程 ...

  3. 2018-8-10-docfx-做一个和微软一样的文档平台

    title author date CreateTime categories docfx 做一个和微软一样的文档平台 lindexi 2018-08-10 19:16:51 +0800 2018-2 ...

  4. docfx 做一个和微软一样的文档平台

    开发中,有一句话叫 最不喜欢的是写文档,最不喜欢的是看别人家代码没有文档.那么世界上文档写最 la 好 ji 的就是微软了,那么微软的api文档是如何做的?难道请了很多人去写文档? 实际上微软有工具用 ...

  5. C# 设计模式 责任链

    责任链模式是一种对象的行为模式.在责任链模式里,很多对象由每一个对象对其下家的引用而连接起来形成一条链.请求在这个链上传递,直到链上的某一个对象决定处理此请求.发出这个请求的客户端并不知道链上的哪一个 ...

  6. Day13_商品详情及静态化

    学于黑马和传智播客联合做的教学项目 感谢 黑马官网 传智播客官网 微信搜索"艺术行者",关注并回复关键词"乐优商城"获取视频和教程资料! b站在线视频 0.学习 ...

  7. Docsify使用指南(打造最快捷、最轻量级的个人&团队文档)

    前言 网上关于动态文档生成工具有很多如:Docsify. VuePress.Docute .Hexo这些都是一些非常优秀的文档生成工具,本章主要介绍如何快速使用Docsify搭建一个快捷.轻量级的个人 ...

  8. 基于.NetCore开发博客项目 StarBlog - (3) 模型设计

    系列文章 基于.NetCore开发博客项目 StarBlog - (1) 为什么需要自己写一个博客? 基于.NetCore开发博客项目 StarBlog - (2) 环境准备和创建项目 基于.NetC ...

  9. 【转】.MD语法入门

    @2019-02-13 [小记] .MD语法入门

随机推荐

  1. 用微信小程序连接WordPress网站

    随着微信小程序的功能越来越强,特别对个人开发者的开放,让个人开发者有机会尝试微信小程序.如果你有自己的个人网站,就可以把个人网站搬到微信小程序里,通过小程序直接访问网站的内容. 要想微信小程序可以获取 ...

  2. (转)C# 控制蜂鸣器发声

    原文地址:http://blog.csdn.net/tsinfeng/article/details/6201918 在C#中可以通过以下四种方式来实现蜂鸣或者报警,播放声音之类的功能.XP下对蜂鸣有 ...

  3. 百度 OCR API 的使用以及与 Tesseract 的简单对比

    目录 百度 OCR API 初探 用 Python 调用百度 OCR API 与 Tesseract 的简单对比 百度 OCR API 初探 近日得知百度在其 APIStore 上开放了 OCR 的 ...

  4. python学习之----深网和暗网

    深网是网络的一部分,与浅网(surface Web)对立.浅网是互联网上搜索引擎可以抓 到的那部分网络.据不完全统计,互联网中其实约90% 的网络都是深网.因为谷歌不 能做像表单提交这类事情,也找不到 ...

  5. jdbc(MySQL)

    1.连接数据库 2.使用配置文件 3.启用连接池 4.事务 JDBC WHAT? 用于执行 SQL 语句的 Java API WHY? 不需要了解每一种数据库连接操作方式 HOW? 加载驱动.获取连接 ...

  6. linux问题集

    Too many authentication failures for root (code 2) 原因:服务器可能由于装了一下安全软件导致有时用ssh远程工具登陆不了,提示太多认证失败for ro ...

  7. linux配置防火墙,开启端口

    Centos7,配置防火墙,开启端口 1.查看已开放的端口(默认不开放任何端口) firewall-cmd --list-ports 2.开启80端口 firewall-cmd --zone=publ ...

  8. hive数据倾斜原因以及解决办法

    何谓数据倾斜?数据倾斜指的是,并行处理的数据集 中,某一部分(如Spark的一个Partition)的数据显著多于其它部分,从而使得该部分的处理速度成为整个数据集处理的瓶颈. 表现为整体任务基本完成, ...

  9. 考勤管理系统V1.0.3

    1.0.3:添加了缺勤名单统计.导出功能: 原本的设想是直接上手Node.js 连接MySQL,写一个能增删改查数据的功能,很遗憾小林属实弟弟,但这个“系统”我会一点点完善的,这一次的代码比之前好看了 ...

  10. js页面百分比缩放

    <script> var docEl = document.documentElement, resizeEvt = 'orientationchange' in window ? 'or ...