Http(3)
响应行
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);
}
}
}

随机推荐
- TDirectory.GetLogicalDrives获取本地逻辑驱动器
使用函数: System.IOUtils.TDirectory.GetLogicalDrives class function GetLogicalDrives: TStringDynArray; s ...
- [转]ef获取某个表中的部分字段值
我有个新闻表 id,title,body,createtime,author,click 使用ef4.1 仅仅读取 id,title,createtime 并显示在页面上. public static ...
- CCTV评论员评论步行者与奇才的比赛
步行者客场迎战主场作战的奇才,奇才的战士可能由于过度兴奋或是过度紧张身体僵硬,本来能打进的球都失掉了.反而,由于步行者取得了两位数的领先,越大心情越放松,打出了过去很少见的流畅局面. CCTV评论员就 ...
- jQuery制作Web全屏效果
需要的资源 1.jQuery版本库是必不可少的2.jQuery FullScreen plugin如果你下载不方便的话,你可以直接把下面的代码copy到你本地JQuery FullScreen plu ...
- 【技术贴】解决vss中提交pdf下载打开空白乱码
vss客户端需要安装一个Vss2005的补丁程序,而且之前上传的pdf文件重新删掉,再次上传进Vss中,再下载打卡就ok了. 补丁名称vs80-kb943847-x86-intl.exe 别人的csd ...
- Java 声明和访问控制(一) 数组的声明 private可以修饰类吗
数组的声明: int []a[] = new int[4][];//是正确的 int[] array = new int[2]{1,2};//是错误的 数组的长度是不可改变的,不能通过任何方式改变大小 ...
- Android 使用XmlPullParser解析xml
这里我们假设要解析的xml文件名为:test.xml,我们将其放在assets路径中. xml文件内容为: <?xml version='1.0' encoding='utf-8' standa ...
- 【POJ 3162】 Walking Race (树形DP-求树上最长路径问题,+单调队列)
Walking Race Description flymouse's sister wc is very capable at sports and her favorite event is ...
- 如何在win下编译thunderbird
最近突然想研究一下thunderbird的实现,于是在WIN2K3下对其进行了系列的编译,特将编译的一些心得与大家共享.其实编译过程已经非常简单了,本文以VC8 ( VISUAL STUDIO 200 ...
- 解析Android开发优化之:对Bitmap的内存优化详解
在Android应用里,最耗费内存的就是图片资源.而且在Android系统中,读取位图Bitmap时,分给虚拟机中的图片的堆栈大小只有8M,如果超出了,就会出现OutOfMemory异常.所以,对于图 ...