response对象
request和response对象分别代表着请求和响应
获取客户端的请求信息,只需要request对象
对应的 想客户端发送信息,只需要response对象
response响应输出,其实现ServletResponse 接口,或继承自HttpServletResponse类
方 法 名 | 说 明 |
---|---|
void addCookie(Cookie cookie) | 请客户端写入Cookie对象 |
PrintWriter getWriter() | 获取字符输出流对象 |
void sendRedirect(String location) | 请求重定向(页面跳转) |
void setCharacterEncoding(String charset) | 设置编译内容字符集 |
void setContentType(String type) | 设置客户端的处理内容类型 |
void setHeader(String name, String value) | 设置输出响应头信息 |
ServletOutputStream getOutputStream() | 获取二进制输出流对象 |
response对象 – 中文乱码
乱码原因分析:
当调用out.write()时,容器默认会使用"iso-8859-1"字符集去编 码(iso-8859-1西欧字符集,不支持中文)。
解决思路: 使服务器与客户端编码方式一致即可。
服务器解决方案
response.setContentType("text/html;charset=utf-8");
在定义 out 之前加上这一句
客户端
Html加
<meta http-equiv="content-type" content="text/html;charset=utf-8">
response对象 – 重定向
服务器端
response.sendRedirect("../demo01");
客户端HTML重定向技术:
<a href='http://www.garysu.cn/'>我转</a>
客户端JS重定向技术:
window.location.href='http://www.garysu.cn/';