3http响应
HTTP、1.1 200 OK 相应行
Server:Apache-Coyote/1.1 响应头
Content-Length:24
空行
实体内容
3.1响应行
http 协议版本
状态码:当前服务器处理请求的状态(结果)
常见的状态码:
200: 请求处理完成。成功返回
302: 需要浏览器进一步请求,才能完成
404: 浏览器端的错误,没有找到资源
500: 服务器端的错误
状态描述:对状态码的文字补充
3.2响应头:
location: htp//wwit315.org/id.j重定向的地址。结合302 状态使用完成重定向的效果。
Server:apache tomcat:-服 务 器 的 类 型。
Cont ent-Encoding: Bzie:-服务器发送给浏览器的数据压缩格式。
Cont ent-l ength :80:-服务器发送给浏览器的数据长度。
Content-Language: zh-cn-服务器支持语言。
Content-Type: text/html; charset=GB2312:-服务器发送给浏览器的数据类型和数据编码格式。
Last-Modified:Tue,11J1200018:23-服务器资源的最后修改时间。
Refresh: 1;url-htt://www.it3 15.org:--定时刷新或每隔n 秒跳转资源。
Content-Disposition :attachment; filename= aa.zip:..以下载方式打开资源。
Transfer-Encoding: chunked
Set-Cook ie SS = Q0 = 5Lb_nQ; path =/search:服务器发送给浏览器的cookie数据。
Expires:-1通知浏览器不使用缓存。
Cache-Cont rol: no-cacher
Pragma: no-cache
Connection: close/Keep-Alive 搭为态。
Date:Tue,11Jul200018:23:51GMT:响应发出的时间
//1)Tomcat服务器提供了一个HttpServletResponse对象,用于给开发者修改响应数据
2)通过service方法把response对象传入到servlet
3)通过response对象修改响应数据
4)tomcat服务器把response对象转换成响应格式的字符串,发送给浏览器
方法:
response0setStatus(404)设置状态码
response.setHeader(“name”,“value”)修改响应头
response.getWriter().write() 以字符形式发送内容
response.getOutputStream().write() 以字节的形式发送内容
一些代码实例
/**
* 设置状态码
*/
public class ResponseDemo1 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
/**
* 设置content-style响应
*
*/
/**
* 作用
* 1设置输出数据的编码
* 2告诉浏览器自动适应输出数据的编码
*/
/**
* Content-Disposition :attachment; filename= aa.zip 已下载方式打开资源
*/
File file = new File("E:/Programming/MGtest/DOM.jpg");
response.setHeader("content-disposition", "attachment;filename="+file.getName());
FileInputStream in = new FileInputStream(file);
ServletOutputStream out = response.getOutputStream();
byte[] buf=new byte[1024];
int len=0;
while((len=in.read(buf))!=-1){
out.write(buf, 0, len);
}
in.close();
out.close();
}
}