美文网首页
3.Http响应

3.Http响应

作者: _少年不知愁 | 来源:发表于2018-07-16 09:00 被阅读0次

    1.response也由响应行 相应头 实体内容
    2.响应行

    HTTP/1.1 200
    

    http协议版本 状态码
    状态码为服务器处理请求的结果(状态)

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

    3.相应头

    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:保存连接
    

    4.重定向
    重定向实际上请求服务器两次,浏览器根据状态码为302,然后获取响应头中的location的url,向服务器再次发起请求得到的结果

        public void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            /**
             * 需求: 跳转到adv.html
             * 使用请求重定向: 发送一个302状态码+location的响应头
             */
            /*response.setStatus(302);//发送一个302状态码
            response.setHeader("location", "/adv.html"); //location的响应头
    */      
            
            //请求重定向简化写法
            response.sendRedirect("/adv.html");
            
            
        }
    

    5.定时刷新
    浏览器根据相应头中refresh,实现刷新,也可在value设置为3;url,实现x秒后跳转到xx页面

        public void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            /**
             * 定时刷新
             * 原理:浏览器认识refresh头,得到refresh头之后重新请求当前资源
             */
            //response.setHeader("refresh", "1"); //每隔1秒刷新次页面
            
            /**
             * 隔n秒之后跳转另外的资源
             */
            response.setHeader("refresh", "3;url=/adv.html");//隔3秒之后跳转到adv.html
        }
    

    6.文件下载
    通过响应头中 response.setHeader("Content-Disposition", "attachment; filename=" + file.getName());控制直接在页面输出还是文件下载

        public void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            /**
             * 设置响应实体内容编码
             */
            //response.setCharacterEncoding("utf-8");
    
            /**
             * 1. 服务器发送给浏览器的数据类型和内容编码
             */
            //response.setHeader("content-type", "text/html");
           // response.setContentType("text/html;charset=utf-8");//和上面代码等价。推荐使用此方法
            //response.setContentType("text/xml");
            //response.setContentType("image/jpg");
    
    
            //response.getWriter().write("<html><head><title>this is tilte</title></head><body>中国</body></html>");
            //response.getOutputStream().write("<html><head><title>this is tilte</title></head><body>中国</body></html>".getBytes("utf-8"));
    
    
            File file = new File("D:\\onlyoproject\\httpServletTest\\src\\cn\\mj\\response\\rx.jpg");
            /**
             * 设置以下载方式打开文件
             */
          response.setHeader("Content-Disposition", "attachment; filename=" + file.getName());
            /**
             * 下载图片
    
             * 发送图片
             */
            FileInputStream in = new FileInputStream(file);
            byte[] buf = new byte[1024];
            int len = 0;
    
            //把图片内容写出到浏览器
            while ((len = in.read(buf)) != -1) {
                response.getOutputStream().write(buf, 0, len);
            }
        }
    

    相关文章

      网友评论

          本文标题:3.Http响应

          本文链接:https://www.haomeiwen.com/subject/xpaupftx.html