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

随机推荐
- ySQL for mac使用记录
一.登录 打开终端,输入/usr/local/mysql/bin/mysql -u root -p 初次进入mysql,密码为空.当出现mysql>提示符时,表示你已经进入mysql中.键入ex ...
- docker下使用caffe的命令记录
查看所有的images sudo docker images 利用某个image生成container sudo docker run -it --net=host -v /home/tingting ...
- Java多线程初学者指南(8):从线程返回数据的两种方法
从线程中返回数据和向线程传递数据类似.也可以通过类成员以及回调函数来返回数据.但类成员在返回数据和传递数据时有一些区别,下面让我们来看看它们区别在哪. 一.通过类变量和方法返回数据 使用这种方法返回数 ...
- [原博客] POI系列(4)
正规.严谨.精妙. -POI BZOJ 1531 : [POI2005]Bank notes 裸的背包,可以二进制拆分一下.一个物品比如说有n个,可以拆成 1,2,4,8,16...个. OJ上没有样 ...
- Cxf + Spring3.0 入门开发WebService
转自原文地址:http://sunny.blog.51cto.com/182601/625540/ 由于公司业务需求, 需要使用WebService技术对外提供服务,以前没有做过类似的项目,在网上搜寻 ...
- [转贴]C语言复习笔记-17种小算法-解决实际问题
判断日期为一年中的第几天(考虑闰年) /* * 计算该日在本年中是第几天,注意闰年问题 * 以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天 * 特殊情况,闰年且输入月份大于3时 ...
- ANDROID_MARS学习笔记_S05_005_方向传感器
import android.app.Activity; import android.content.Context; import android.hardware.Sensor; import ...
- ANDROID_MARS学习笔记_S03_005_Geocoder、AsyncTask
一.代码1.xml(1)AndroidManifest.xml <uses-permission android:name="android.permission.ACCESS_FIN ...
- 转: 二十八、Java图形化界面设计——中间容器(Jpanel)
http://blog.csdn.net/liujun13579/article/details/7762835 上一篇讲解了Jframe顶层容器,例子中生成了一个空的窗体,在实际编程过程中,一般很少 ...
- 9.png(9位图)在android中作为background使用导致居中属性不起作用的解决方法
在使用到9.png的布局上面添加 android:padding="0dip" 比如 <LinearLayout android:layout_widt ...