#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. VS2008/MFC —常用控件使用总结 转载

    在公司培训期间,经理给了我们没人10个界面草图,让我们在VS2008下使用MFC设计,因为在经理的帮助和自己的努力下,终于在三天时间内完成,现在就根据在这三天 时间里所用到的控件做出如下总结: 1.D ...

  2. getting “fatal: not a git repository: '.'” when using post-update hook to execute 'git pull' on another repo

    Here is the script that ultimately worked. I think the bit I was originally missing that prevented i ...

  3. 实现 winform 异步跨线程访问UI控件

    在开发winform时经常会用到多线程防止界面出现假死现象,比如当你单击某个按钮时,需要执行很多代码,但是在执行过程中想实时的将当前执行的情况报告给用户,类型进度条或文本什么的. 这个时候很显然,如果 ...

  4. 【python】二进制、八进制、十六进制表示方法(3.0以上)

    2进制是以0b开头的: 例如: 0b11 则表示十进制的3 8进制是以0o开头的: 例如: 0o11则表示十进制的9 (与2.0版本有区别) 16进制是以0x开头的: 例如: 0x11则表示十进制的1 ...

  5. css 多出一行或多行后显示...的方法

    一行超出显示... .mui-ellipsis { overflow: hidden; white-space: nowrap; text-overflow: ellipsis; } 两行超出的显示. ...

  6. 简单python2.7.3安装setuptools模块

    下载setuptools https://pypi.python.org/packages/2.7/s/setuptools/setuptools-0.6c11-py2.7.egg 安装 .6c11- ...

  7. Memento:客户端瘦身

    说是客户端瘦身,其实备忘录模式的本质让调用客户端职责减轻,将客户端的对于实现比如数据恢复之类细节的内容封装在操作类之中.其实面向对象的一重要方面就是划分清楚职责,这样可以减少改到造成的影响,便于扩展. ...

  8. 使用Windbg来检查内存

    Windbg是一款微软开发的调试windows代码的工具,水很深,不过使用windbg来进行clr的调试则比较简单,windbg使用之前需要进行配置. File->Symbol path-> ...

  9. Atlassian、Slack 以及 ChatOps 未来的前景如何?

    在大家纷纷谈起 DevOps 和 ChatOps 的时候,Martin Giles 也对 ChatOps 的前景有自己的看法.他曾是<经济学人>的记者,现在是 Wing Venture C ...

  10. 使用astyle格式化代码

    astyle是一款代码格式化工具,它的下载地址是:http://sourceforge.net/projects/astyle 一.基本命令 astyle --style=ansi main.cs ( ...