day08-响应数据

格式
HTTP/1.1 200 OK
状态行
Server: Apache-Coyote/1.1
Content-Length: 0
Date: Fri, 21 Apr 2017 07:37:27 GMT 响应头
空行
Hello
实体内容,响应主体内容
1.状态行
格式
HTTP/1.1 200 OK
200,http状态码
ok,对状态码的翻译或描述

200,正常请求与响应,进行一次正常的通信
304,通知浏览器读取浏览器端的缓存文件
302,页面重定向
403,你没有权限访问这个资源
404,找不到服务器对应的资源文件
500,服务器发生异常,就是你的代码有问题

2.响应头
格式
HTTP请求中的常用响应头
Location: http://www.it315.org/index.jsp –跳转方向
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 –下载
Set-Cookie:SS=Q0=5Lb_nQ; path=/search
Expires: -1 –缓存
Cache-Control: no-cache –缓存
Pragma: no-cache –缓存
Connection: close/Keep-Alive –连接
Date: Tue, 11 Jul 2000 18:23:51 GMT –时间

(1)Location
作用
通知浏览器跳转页面,重定向跳转

代码

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //使用location进行页面跳转必须现先设置http状态码为302
//      response.setStatus(302);
//      //访问这个资源之后,通知浏览器跳转到test.html里面去
//        response.setHeader("Location","/day08_58/test.html");

          //response进行优化,一句话就可以实现上面2句代码(推荐使用)
          response.sendRedirect("/day08_58/test.html");//实现原理就是上面的代码


    }

(2)Content-Encoding

作用
    通知浏览器对资源数据解压后再进行显示数据

代码
需求:向客户端输出4000个长度字符,针对于大数据进行数据压缩

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    //对大的数据进行压缩,那么传输的数据就是压缩文件,浏览器默认会下载这个压缩文件
    //目的:浏览器要显示出来,通知浏览器对压缩文件进行解压再进行显示,Content-Encoding
    response.setHeader("Content-Encoding", "gzip");

    //输出4000个长度字符
    StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < 1000; i++) {
            stringBuilder.append("abcd");
        }

    //使用gzip压缩,GZIPOutputStream可以对数据进行压缩,但是没有写出数据能力,所以需要借助response.getOutputStream()
    GZIPOutputStream gzipOutputStream =  new GZIPOutputStream(response.getOutputStream());
    //对数据进行压缩,数据压缩后将压缩好的数据会放在内存流中,需要手动调用方法将内存流中的数据输出到输出流中
        gzipOutputStream.write(stringBuilder.toString().getBytes());
    //手动调用方法将内存流中的数据输出到输出流中
    gzipOutputStream.finish();

    //输出
//      response.getOutputStream().write(stringBuilder.toString().getBytes());
}

(3)ContentType
作用
通知浏览器采用什么码表进行解码
向客户端浏览器输出中文乱码问题的原理


代码:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//      //1.修改response的默认编码码表
//      response.setCharacterEncoding("utf-8");
//      //2.通知浏览器以utf-8解码
//      response.setHeader("Content-Type", "text/html;charset=utf-8");

        //优化升级(处理向客户端输出中文乱码问题)
        response.setContentType("text/html;charset=utf-8");//实现原理就是上面2句代码

        //向客户端输出中文数据

        //字节流输出
        //response.getOutputStream().write("你好".getBytes());
        //字符流输出
        PrintWriter out = response.getWriter();
        out.write("你好");
    }

(4)Refresh
作用
通知浏览器按照指定时间刷新页面
功能1:刷新页面
功能2:指定3妙后,跳转到test.html页面

代码

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //需求:指定1秒后刷新页面
    //response.setHeader("refresh", "1");//1秒

    //需求2:指定3妙后,跳转到test.html页面
    response.setHeader("refresh", "3;url=/day08_58/test.html");//3秒

    }

(5)Content-Disposition
作用
通知浏览器以附件形式进行下载

需求:向客户端输出一张图片

代码

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //需求:向客户端输出一张图片,客户端要以附件形式下载
        //通知浏览器客户端要以附件形式下载
        //attachment,代表以附件下载
        //filename,下载后的文件名字
        //传输中文,默认请求行与实体内容传输中文会默认进行URL编码操作,针对响应头与请求头传输中文,你要自己进行URL编码
        String name = "柳岩";
        //响应头数据中文要进行URL编码
        name = URLEncoder.encode(name,"UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename="+name+".jpg");
        //读取文件流,再输出
        FileInputStream fileInputStream = new FileInputStream(new File("h:/1.jpg"));//路径中正斜杠只需要写一个,反斜杠需要写2个
        int length = -1;
        byte[] bytes = new byte[1024];
        //创建输出
        while((length=fileInputStream.read(bytes))!=-1){
            response.getOutputStream().write(bytes,0,length);
        }
        fileInputStream.close();

    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值