响应行

1、常见的状态:

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

2、常见的响应头

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

3、HttpServletResponse对象

HttpServletResponse对象修改响应信息:

  响应行:

  response.setStatus()  设置状态码

响应头:

   response.setHeader("name","value")  设置响应头

实体内容:

   response.getWriter().writer();   发送字符实体内容

  response.getOutputStream().writer()  发送字节实体内容

public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        /**
         * 通过response对象改变相应信息
         */
            //1、响应行
            //response.setStatus(404);    //修改状态码
            //response.sendError(404);        //发送404状态码+404错误页面

        //2、改变响应头
        response.setHeader("server", "JBoss");

        //3、实体内容
        //    response.getWriter().write("1、hello world");    //字符内容
        response.getOutputStream().write("2、hello world".getBytes());     //字节内容
    }

案例分析

一、请求的重定向

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Demo2 extends HttpServlet {

    /**
     * 案例一:请求重定向
     * 相当于超链接跳转页面
     */
    private static final long serialVersionUID = 1L;

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        /**
         *  需求:跳转到另一个页面
         *  使用重定向,发送一个302状态码+location的响应头
         */
//        response.setStatus(302);
//        response.setHeader("location", "/HttpTest/index.jsp");    //location是响应头

        //请求重定向的简化写法
        response.sendRedirect( "/HttpTest/index.jsp");
    }
}

案例二

public class Demo3 extends HttpServlet {

    /**
     * 案例  定时刷新
     */
    private static final long serialVersionUID = 1L;

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

}

案例三

public class Demo4 extends HttpServlet {

    /**
     * 将图片写出到浏览器
     */
    private static final long serialVersionUID = 1L;

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("image/jpg");
        /**
         * 设置已下载的方式打开文件
         */
        File file=new File("F:/4.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);
        }

    }

}

随机推荐

  1. copy,retain,assign,strong,weak的区别

    引用地址:http://www.aichengxu.com/view/32930 一.assign,copy,retain 1.copy是内容复制,新建一个相同内容的不同指针,retain为指针复制, ...

  2. android 布局权重问题(转载)

    //权重和父容器orientation有关 horizontal 指水平方向权重  android:layout_width vertical  指垂直方向权重   android:layout_he ...

  3. ~/.vimrc config

    runtime! debian.vim "设置编码 set encoding=utf- set fencs=utf-,ucs-bom,shift-jis,gb18030,gbk,gb2312 ...

  4. vs2013调试崩溃,重启电脑依旧崩溃

    如果大家遇到 VS断点调试程序崩溃的问题,可以排查是不是有这个问题 VSx新安装了插件 点击工具---扩展和更新  禁用最新安装的程序 一般就没有问题了

  5. Python socket编程应用

    最近因为考试各种复习顺便刷电视剧,感觉跟小伙伴玩的越来越不开心了,一定是最近太闲了,恩.于是想研究一下代理服务器,下载了一份代码,发现竟然还涉及到socket编程,所以把之前网络课的socket聊天室 ...

  6. OA学习笔记-006-SPRING2.5与hibernate3.5整合

    一.为什么要整合 1,管理SessionFactory实例(只需要一个) 2,声明式事务管理 spirng的作用 IOC 管理对象.. AOP 事务管理.. 二.整合步骤 1.整合sessionFac ...

  7. leetcode面试准备: CountPrimes

    1 题目 Description: Count the number of prime numbers less than a non-negative number, n. 接口:public in ...

  8. USACO3.31Riding the Fences(输出欧拉路径)

    都忘了欧拉路径是什么了.. 用dfs搜 标记边  刚开始直接从I-N搜 直接超时 2了 先找符合起点和终点的点搜 即度数是奇数 d单dfs也超了 后来换了个姿势.. /* ID: shangca2 L ...

  9. [LeetCode#250] Count Univalue Subtrees

    Problem: Given a binary tree, count the number of uni-value subtrees. A Uni-value subtree means all ...

  10. webbrowser控件事件

    Beforenavigate2: Fired before navigate occurs in the given WebBrowser(window or frameset element). T ...