Http(1)
#http协议版本
http1.0:当前浏览器客户端与服务器端建立连接之后,只能发送一次请求,一次请求之后连接关闭。
http1.1:当前浏览器客户端与服务器端建立连接之后,可以在一次连接中发送多次请求。(基本都使用1.1)
#请求资源
URL: 统一资源定位符。http://localhost:8080/day11/testImg.html。只能定位互联网资源。是URI的子集。
URI: 统一资源标记符。/day11/hello。用于标记任何资源。可以是本地文件系统,局域网的资(//192.168.14.10/myweb/index.html),可以是互联网。
#请求方式
常见的请求方式: GET 、 POST、 HEAD、 TRACE、 PUT、 CONNECT 、DELETE
常用的请求方式: GET(浏览器默认的请求方式) 和 POST
#请求头
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 -- 请求发出的时间
#请求实体
只有post请求才有实体。(post请求一般用于提交比较交敏感的数据)
HttpServletRequest对象
HttpServletRequest对象作用是用于获取请求数据
核心的API:
请求行:
request.getMethod(); 请求方式
request.getRequetURI() / request.getRequetURL() 请求资源
request.getProtocol() 请求http协议版本
请求头:
request.getHeader("名称") 根据请求头获取请求值
request.getHeaderNames() 获取所有的请求头名称
实体内容:
request.getInputStream() 获取实体内容数据
示例代码
1、jsp代码
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form action="/HttpTest/HttpTest" method="get"> 用户名:<input type="text" name="name"></br> 密 码 :<input type="text" name="password"></br> <input type="submit" value="提交"> </form> </hr> </br> </br> <form action="/HttpTest/HttpTest" method="POST"> 用户名:<input type="text" name="name"></br> 密 码 :<input type="text" name="password"></br> <input type="submit" value="提交"> </form> </body> </html>
2、servlet代码
public class HttpTest extends HttpServlet { private static final long serialVersionUID = 1L; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //t1(request); // t2(request); } //接收post方式的请求 @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { InputStream in=req.getInputStream(); //得到实体内容 byte[] buf=new byte[1024]; int len=0; while ((len=in.read(buf))!=-1) { String str=new String(buf,0,len); System.out.println(str); } } private void t2(HttpServletRequest request) { /** * 请求头 */ Enumeration<String> enums=request.getHeaderNames(); while (enums.hasMoreElements()) { String string = (String) enums.nextElement(); String value=request.getHeader(string); System.out.println(string+" : "+value); } } private void t1(HttpServletRequest request) { /** * 请求行 */ System.out.println("请求的方式:"+request.getMethod()); System.out.println("URI:"+request.getRequestURI()); System.out.println("URL:"+request.getRequestURL()); System.out.println("http:"+request.getProtocol()); } }
随机推荐
- C#连接、访问MySQL数据库
一.准备工具 visual stuido(本示例使用visual studio 2010) MySql.Data.dll mysql_installer_community_V5.6.21.1_set ...
- hierarchyviewer偶然不能使用的解决方法
在DDMS的device中可以看到设备,并显示可以debug的状态,可以看到不显示进程的信息,但是hierarchyviewer也却不显示各个Window. 在控制台的打印信息如下: - hierar ...
- Chain of Responsibility
比较经典的距离是请假申请(<大话设计模式>中的例子),请假是要逐级判断,只有级别到了才有权利审批,从构造上面其实"装饰"模式和"职责链"之间有相通的 ...
- 在HTML页面布局中,position的值有几种,默然的值是什么
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8&qu ...
- 有关collection中的一些数据结构
Collection接口 Collection是最基本的集合接口,一个Collection代表一组Object,即Collection的元素(Elements).一些Collection允许相同的元素 ...
- 滴滴过节送10元打车券是不是bug
自从滴滴跟快的去年合作以后,也不玩烧钱大战了,也没法打到免费的车了,乘客打车优惠也少了. 但是现在的滴滴在过节的时候还是会返滴滴代金券,而且金额都比较大,超出了打车的起步价.半年前这边的司机会经常利用 ...
- struts2 拦截器1
action invoke前会调用,invoke后会调用 public class FirstInterceptor extends AbstractInterceptor{ @Override pu ...
- 【JavsScript】XMLHttpRequest2的进步之处
本文参考自:XMLHttpRequest2 新技巧 (重点保留demo,方便自己日后查阅) HTML5是现在web开发中的热点,虽然关于web app和local app一直有争论,但是从技术学习的角 ...
- SparkSQL配置和使用初探
1.环境 OS:Red Hat Enterprise Linux Server release 6.4 (Santiago) Hadoop:Hadoop 2.4.1 Hive:0.11.0 JDK:1 ...
- cout输出字符串指针
先给出通过字符型指针输出字符串的示例代码,如下: #include <iostream>using std::cout;using std::endl; int main(){ const ...