HTTP请求中的常用消息头
accept:浏览器通过这个头告诉服务器,它所支持的数据类型
Accept-Charset: 浏览器通过这个头告诉服务器,它支持哪种字符集
Accept-Encoding:浏览器通过这个头告诉服务器,支持的压缩格式
Accept-Language:浏览器通过这个头告诉服务器,它的语言环境
Host:浏览器通过这个头告诉服务器,想访问哪台主机
If-Modified-Since: 浏览器通过这个头告诉服务器,缓存数据的时间
Referer:浏览器通过这个头告诉服务器,客户机是哪个页面来的 防盗链
Connection:浏览器通过这个头告诉服务器,请求完后是断开链接还是何持链接
状态行格式: HTTP版本号 状态码 原因叙述<CRLF>
举例:HTTP/1.1 200 OK
状态码用于表示服务器对请求的处理结果,它是一个三位的十进制数。响应状态码分为5类,如下所示:
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setStatus(302);//设置服务器的响应状态码 *设置响应头,服务器通过 Location这个头,来告诉浏览器跳到哪里,这就是所谓的请求重定向 */ response.setHeader("Location", "/JavaWeb_HttpProtocol_Study_20140528/1.jsp"); }
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String data = "abcdabcdabcdabcdabcdabcdab" + "cdabcdabcdabcdabcdabcdabcdabcdabc" + "dabcdabcdabcdabcdabcdabcdabcdabc" + "dabcdabcdabcdabcdabcdabcdabcdabcdab" + "cdabcdabcdabcdabcdabcdabcdabcdabcdab" + "cdabcdabcdabcdabcdabcdabcdabcdabcdab" + "cdabcdabcdabcdabcdabcdabcdabcdabcdab" + "cdabcdabcdabcdabcdabcdabcdabcdabcdabcd"; System.out.println("原始数据的大小为:" + data.getBytes().length); ByteArrayOutputStream bout = new ByteArrayOutputStream(); GZIPOutputStream gout = new GZIPOutputStream(bout); //buffer gout.write(data.getBytes()); gout.close(); //得到压缩后的数据 byte g[] = bout.toByteArray(); response.setHeader("Content-Encoding", "gzip"); response.setHeader("Content-Length",g.length +""); response.getOutputStream().write(g); }
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { /** * 浏览器能接收(Accept)的数据类型有: * application/x-ms-application, * image/jpeg, * application/xaml+xml, * image/gif, * image/pjpeg, * application/x-ms-xbap, * application/vnd.ms-excel, * application/vnd.ms-powerpoint, * application/msword, */ response.setHeader("content-type", "image/jpeg");//使用content-type响应头指定发送给浏览器的数据类型为"image/jpeg" //读取位于项目根目录下的img文件夹里面的WP_20131005_002.jpg这张图片,返回一个输入流 InputStream in = this.getServletContext().getResourceAsStream("/img/WP_20131005_002.jpg"); byte buffer[] = new byte[1024]; int len = 0; OutputStream out = response.getOutputStream();//得到输出流 while ((len = in.read(buffer)) > 0) {//读取输入流(in)里面的内容存储到缓冲区(buffer) out.write(buffer, 0, len);//将缓冲区里面的内容输出到浏览器 }
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { /** * 浏览器能接收(Accept)的数据类型有: * application/x-ms-application, * image/jpeg, * application/xaml+xml, * image/gif, * image/pjpeg, * application/x-ms-xbap, * application/vnd.ms-excel, * application/vnd.ms-powerpoint, * application/msword, */ response.setHeader("content-type", "image/jpeg");//使用content-type响应头指定发送给浏览器的数据类型为"image/jpeg" //读取位于项目根目录下的img文件夹里面的WP_20131005_002.jpg这张图片,返回一个输入流 InputStream in = this.getServletContext().getResourceAsStream("/img/WP_20131005_002.jpg"); byte buffer[] = new byte[1024]; int len = 0; OutputStream out = response.getOutputStream();//得到输出流 while ((len = in.read(buffer)) > 0) {//读取输入流(in)里面的内容存储到缓冲区(buffer) out.write(buffer, 0, len);//将缓冲区里面的内容输出到浏览器 }
public class ServletDemo04 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { /** * 设置refresh响应头,让浏览器每隔3秒定时刷新 */ // response.setHeader("refresh", "3"); /** * 设置refresh响应头,让浏览器3秒后跳转到http://www.baidu.com */ response.setHeader("refresh", "3;url='http://www.baidu.com'"); response.getWriter().write("gacl"); } }
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { /** * 设置content-disposition响应头,让浏览器下载文件 */ response.setHeader("content-disposition", "attachment;filename=xxx.jpg"); InputStream in = this.getServletContext().getResourceAsStream("/img/1.jpg"); byte buffer[] = new byte[1024]; int len = 0; OutputStream out = response.getOutputStream(); while ((len = in.read(buffer)) > 0) { out.write(buffer, 0, len); } }