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);
}
}
}

随机推荐
- CGDataCmd
1,"Get Inf Joint from file" 选择文件中储存的骨骼信息; 2,"Export skinWeight" 导出权重; 3," ...
- asp.net与MVC4的路由原理和过程【学习笔记】
当IIS收到一个http请求,把请求信息发给对应的HttpModel(实际是实现类UrlRoutingModule),在HttpModel中会注册HttpApplication 类中的PostReso ...
- const char * 的终结贴(看完无需其他任何文章,从此不再蛋疼)
我之前也是以为我对const char *ptr 这种形式的写法是掌握了的,真的,不就是说一个指针是不可改变的吗? 那么问题就来了,到底是ptr指针本身不能改变,还是ptr执行的值不能改变呢? 从网上 ...
- 【BZOJ 3110】 [Zjoi2013]K大数查询(整体二分)
[题目] Description 有N个位置,M个操作.操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位置加入一个数c如果是2 a b c形式,表示询问从第a个位置到 ...
- USB 2.0 A型、B型、Mini和Micro接口定义及封装
USB全称Universal Serial Bus(通用串行总线),目前USB 2.0接口分为四种类型A型.B型.Mini型还有后来补充的Micro型接口,每种接口都分插头和插座两个部分,Micro还 ...
- 解析Android开发优化之:对Bitmap的内存优化详解
在Android应用里,最耗费内存的就是图片资源.而且在Android系统中,读取位图Bitmap时,分给虚拟机中的图片的堆栈大小只有8M,如果超出了,就会出现OutOfMemory异常.所以,对于图 ...
- Python3整理文件
晚上打算把播放器下载下来的音乐拷贝到mp3里边,但是它是如下形式存放的,相当头痛…… 作为程序员,想到使用python来遍历这个目录,并将有大于限制的音乐文件拷贝到指定目录,相关实现代码如下: # a ...
- 用MATLAB画函数的曲线
用MATLAB画函数曲线 2013年8月11日 命令funtool 这是单变量函数分析的交互界面,比较方便,特别适用于y=f(x)型,即y与x分开的函数形式.见下图
- com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: SELECT command denied to user’
Linux环境 Mysql+Hibernate command denied to user 错误 错误信息 如下: com.mysql.jdbc.exceptions.jdbc4.MySQLSynt ...
- xcode duplicate symbol _GAD_MD5 解决方法
添加了mobi的广告平台后,在Device状态打包时,出现此错误. duplicate symbol _GAD_MD5 in: 解决方法: Targets ->Build Setting 中设 ...