http协议-response

HTTP/1.1 200 OK                --响应行
Server: Apache-Coyote/1.1         --响应头(key-vaule)
Content-Length: 24 
Date: Fri, 30 Jan 2015 01:54:57 GMT
                                   --一个空行
this is hello servlet!!!                  --实体内容
状态码
    200 请求已成功,请求所希望的响应头或数据体将随此响应返回。
    302 请求重定向
    404 客户访问的资源找不到
    500 表示服务器的资源发送错误(服务器内部错误)

常见的响应头

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

HttpServletResponse对象

响应行:
    response.setStatus() 设置状态码
响应头:
    response.setHeader("name","value");
实体内容:(客户可以看见的)
    response.getWriter().writer();//发送字符实体内容

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



        /**
         * 3.2响应头
         * */
        response.setHeader("server","JBoss");
        /**
         * 3.3实体内容(浏览器直接可以看到的内容)
         * */
        response.getWriter().write("01.hello world");


    }

请求重定向

@WebServlet(name = "ResponseDemo2",urlPatterns = "/ResponseDemo2")
public class ResponseDemo2 extends HttpServlet {
    /**
     * 需求:跳转到adv。html
     * 使用请求重定向:发送一个302状态码+location的响应头
     *
     * */
    /**
     * 请求重定向浏览器一共想服务器发送两次请求,第一次是ResponseDemo1,第二次是adv。html
     * 服务器也是返回两次,第一次是status302,location。adv.html,第二次是adv。html
     *  */



    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        response.setStatus(302);
//        response.setHeader("location","/some/adv.html");
        //请求重定向简化写法
        response.sendRedirect("/some/adv.html");
    }
}

定时刷新和隔n秒跳转到另外页面

//定时刷新
response.setHeader("refresh","1");
/**
隔n秒跳转到其他页面
*/
response.setHeader("refresh","3;url=/some/adv.html")//隔三秒,跳转到adv.html

content-Type作用

@WebServlet(name = "ResponseDemo4",urlPatterns = "/ResponseDemo4")
public class ResponseDemo4 extends HttpServlet {


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /**
         *1.服务器发送给浏览器的数据类型
         * */
        response.setContentType("text/html;charset=utf-8");
       // response.setContentType("text/html");
        //设置响应实体内容编码

       // response.setCharacterEncoding("utf-8");
        //response.setHeader("content-type","text/html");//和上面的代码等价
        //response.setContentType("text/xml");
        response.getWriter().write("<html><head></head><body>中国</body></html>");

        //File file = new File("e:gakki.jpg");
        /**
         * 设置以下载方式打开文件
         *
         * */
        //response.setHeader("Content-Disposition", "attachment;filename" + file.getName());

        /**
         * 下载图片
         * */
        /**
         * 发送图片
         * */
//        response.setContentType("image/jpg");
//        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);
//        }
//    }
    }
}

完美解决项目编码问题

/**
在每个servlet开头写:

*/
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值