#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());
    }

}

随机推荐

  1. Windows phone 之常用控件

    一.TextBox TextBox 显示和编辑单格式.多行文本的控件 将TextWrapping的特性设置为Wrap会使文本在到达TextBox控件的边缘时换至新行.必要时会自动扩展TextBox以便 ...

  2. 在win10 64位下编译,提示[C++ Error] E2075 Incorrect project override option: (x86)\borland\cbuilder6\lib\vcl60.csm

    options->compiler  右边有个file name 改下就好了从$(BCB)\lib\vcl60.csm 改为c:\PROGRA~1\borland\CBUILD~1\lib\vc ...

  3. php Imagick库readImage()报Postscript delegate failed 解决方法(失效)

    需要安装 ghostscript http://www.ghostscript.com/download/gsdnld.html

  4. 浅析tornado web框架

    tornado简介 1.tornado概述 Tornado就是我们在 FriendFeed 的 Web 服务器及其常用工具的开源版本.Tornado 和现在的主流 Web 服务器框架(包括大多数 Py ...

  5. QFTP走了以后QNetworkAccessManager出现了

    QNetworkAccessManager Class Header:    #include <QNetworkAccessManager>qmake:     QT += networ ...

  6. 学习Swift -- 错误处理

    错误处理 错误处理是响应错误以及从错误中返回的过程.swift提供第一类错误支持,包括在运行时抛出,捕获,传送和控制可回收错误. 一些函数和方法不能总保证能够执行所有代码或产生有用的输出.可空类型用来 ...

  7. jq总结1

    选择器 /** * 多目标选择器 * 可以选择多个元素或者表达式, * 包装成 jQuery 对象的集合 * 例子:$("div,span") */ $("table t ...

  8. C++中Reference与指针(Pointer)的使用对比

    整理日: 2015年3月18日 引用(reference)和指针(pointer)是学C++过程中最令人头疼的问题,常常不知道什么时候用哪个合适,又常常弄混.找到Dan Saks的这篇文章,讲的很清楚 ...

  9. deep learning framework(不同的深度学习框架)

    常用的deep learning frameworks 基本转自:http://www.codeceo.com/article/10-open-source-framework.html 1. Caf ...

  10. Hust 1231 Coin

    题目链接 题意: 亮亮有N个有瑕疵的硬币,有瑕疵意味着抛一枚硬币正面向上的概率 不等于 反面向上的概率 也即概率不等于0.5. 现在亮亮一次抛N个硬币 , 恰好有K个硬币正面向上 ,接着他又想抛一次 ...