* Location : 服务端用来告诉浏览器,请求需要重定向.(必须结合状态吗302使用)
Server : 服务端用来告诉浏览器,服务器的类型
* Content-Encoding: 服务端用来告诉浏览器,服务器对数据采用的编码(gzip、deflate)
Content-Length: 服务端用来告诉浏览器,响应正文的长度
Content-Language: 服务端用来告诉浏览器,zh-cn服务发送的文本的语言
*Content-Type: 服务端用来告诉浏览器,你要用哪个码表来解析二进制数据
* Last-Modified : 服务端用来告诉浏览器,访问资源的最后修改时间
*Refresh:服务端用来告诉浏览器,多长时间刷新一次
*Content-Disposition: 服务端用来告诉浏览器,以下载文件的方式打开文件
* Expires: -1 : 服务端用来告诉浏览器,不要缓存 Cache-Control:no-cache (1.1) Pragma: no-cache (1.0)
***** Set-Cookie: 服务端用来告诉浏览器,要存储的内容
doGet(HttpServletRequest request, HttpServletResponse response):处理页面上的get请求
HttpServletRequest request:代表了一个请求,从客户端浏览器发送过来的请求信息
当请求到达服务端的时候,tomcat会自动创建该对象,把所有的请求信息包装到了request对象中,就可以通过该对象拿到各种信息
HttpServletResponse response:代表了服务端向客户端的一个响应。
doPost(HttpServletRequest request, HttpServletResponse response):处理页面上的post请求
ServletDeom1.java:
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;
//演示响应头location
public class ServletDemo1 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("ServletDemo1");
response.setStatus(302) ;
//设置响应头
response.setHeader("Location", "/day07_00_httpresponse/servlet/ServletDemo2") ;//请求重定向
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
ServletDemo2.java:
package com.heiam.response;
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 ServletDemo2 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("过来了啊");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
把项目部署到服务器上后,在浏览器中敲入:
http://localhost:8080/day07_00_httpresponse/servlet/ServletDemo1就访问到了项目了
在控制台中输出了对应的内容
服务端用来告诉浏览器,服务器对数据采用的编码(gzip、deflate)
ServletDemo3.java:
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.zip.GZIPOutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//演示响应头Content-Encoding编码格式
public class ServletDemo3 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String s = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" ;
// s.length()
//压缩的时候是一个字节(压缩流中需要的参数是字节数组),先把字符串变成一个字节
byte[] bs = s.getBytes() ;//转换为字节数组的原因是往压缩流里写的数据要求是字节数组
System.out.println("压缩前的长度:" + bs.length);
//创建一个字节数组输出流:因为可以从字节数组输出流中取出我们需要的数据
ByteArrayOutputStream baos = new ByteArrayOutputStream() ;
//希望用Gzip压缩一下然后发送
//创建一个压缩流,包装了一下输出流
GZIPOutputStream gzip = new GZIPOutputStream(baos) ;
//将数据进行压缩
gzip.write(bs) ;
gzip.close() ;
//从压缩流中拿出数据
bs = baos.toByteArray() ; //压缩后的数据
System.out.println("压缩后的长度: " + bs.length);
//通知浏览器发送的数据格式是gzip
response.setHeader("Content-Encoding", "gzip") ;
response.getOutputStream().write(bs) ;
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
服务端用来告诉浏览器,你要用哪个码表来解析二进制数据
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;
//演示Content_type响应头
public class ServletDemo4 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String s = "一个人的武林!!" ;
//通知浏览器的Content-Type
response.setHeader("Content-Type", "text/html;charset=UTF-8") ;
response.getWriter().write(s) ;
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
服务端用来告诉浏览器,多长时间刷新一次
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//演示Refresh响应头
public class ServletDemo5 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//隔一段时间刷新自己
// response.setHeader("Refresh", "2") ;
// response.getWriter().write("aa" + new Random().nextInt()) ;
//隔2秒时间刷新到其他资源
response.setHeader("Refresh", "2;url=/day07_00_httpresponse/servlet/ServletDemo2") ;
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
服务端用来告诉浏览器,以下载文件的方式打开文件
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//演示响应头Content-Disposition
public class ServletDemo6 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//拿到图片的路径
String path = this.getServletContext().getRealPath("/WEB-INF/classes/1.gif") ;
System.out.println(path);
//创建一个输入流对象
FileInputStream is = new FileInputStream(path) ;
byte[] bs = new byte[1024] ;
int b = 0 ;
//创建一个输出流对象
ServletOutputStream os = response.getOutputStream() ;
//通知浏览器不要直接显示,而要下载下来再显示
response.setHeader("Content-Disposition", "attachment;filename=1.gif") ;
//读取数据发送数据
while((b = is.read(bs)) != -1){
os.write(bs,0,b) ;//该流是由response对象产生的,不需要关闭,服务器会自动关闭
}
is.close() ;
os.close() ;
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
服务端用来告诉浏览器,不要缓存 Cache-Control:no-cache (1.1) Pragma: no-cache (1.0)
因为不清楚客户端使用的是什么版本的协议,所以都写上
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;
//演示响应头Expires
public class ServletDemo7 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//通知浏览器不要缓存
response.setHeader("Expires", "-1") ;
response.setHeader("Cache-control", "no-cache") ;
response.setHeader("Pragma", "no-cache") ;
response.getWriter().write("hello world!") ;
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}