【J2EE】 关于response对象

两种响应流

  1.response.getWriter()  //字符流

    >默认为iso-8859-1,setCharactorEncoding("utf-8") setContentType("text/html;charset=utf-8") //此方法会调用setCharactorEncoding并设置响应头的contentType

    

    

  2.response.getOutputStrem()  //字节流

    注:response只能响应一种流,如同时使用 会抛出IIIegalStateException

设置响应头

  response.setHeader("content-type","text/html;charset=uft-8")

  response.setHeader("Refresh","5; URL=http://www.baidu.cn")  //5秒后跳转到指定网址

设置状态码

  response.setStatus(404)  

重定向的两种写法:

  1.

    response.setStatus(302)  //(302是重定向的状态码)

    response.setHeader("Location", "url");  // 设置url

  2.

    response.sendRedired(url)

  

  

  

原文地址:https://www.cnblogs.com/LightChan/p/7183888.html