用途:用于设置响应消息
同样,ServletResponse是顶级父接口,因为我们常用Http协议,所以我们只介绍HttpServletReponse。
ServletResponse接口
| 继承
HttpServletResponse接口
| 实现
org.apache.catalina.connector.ResponseFacade 类(tomcat)
@WebServlet("/response1")
public class ResponseServlet1 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置响应状态码
response.setStatus(404);
//设置响应头
response.setHeader("Content-Type", "text/html");
response.setHeader("Server", "Tomcat 8.5");
//设置响应体
response.getWriter().println("File Not Found
");
}
}
@WebServlet("/refresh")
public class RefreshServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//refresh用法一共有两种
//用法一:值只有一个数字,表示每隔多少秒刷新当前页面,实现页面时时刻刻刷新当前的时间
//转成YYYY-MM-dd HH:mm:ss
String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
response.getWriter().println(time);
//response.setHeader("refresh", "1");
//用法二:值里面有一个数字,并且还有一个url,表示的是经过多少秒之后跳转至url,然后结束
//response.setHeader("refresh", "3;url=" + request.getContextPath() + "/1.html");
response.setHeader("refresh", "3;url=http://www.cskaoyan.com");
}
}
http://localhost/
@WebServlet("/redirect")
public class RedirectServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//response.setStatus(302);
//第一种写法
//response.setHeader("Location", request.getContextPath() + "/1.html");
//第二种写法
//response.setHeader("Location", "http://www.cskaoyan.com");
//我们上面是根据重定向的定义,自己去写了对应的实现
//服务器给我们提供了一个更为简便的API
//第三种写法
response.sendRedirect(request.getContextPath() + "/1.html");
}
}
//响应体设置一个编码格式,同时将编码格式告诉给浏览器,两边统一即可
//1.可以通过响应头告知
@WebServlet("/response1")
public class ResponseServlet1 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置响应状态码
response.setStatus(404);
//设置响应头
//response.setHeader("Content-Type", "text/html");
response.setHeader("Server", "Tomcat 8.5");
//设置响应体
//response.getWriter().println(new Date());
//response.setCharacterEncoding("gbk");
//因为该行代码由两层含义:1.设置响应体编码格式2.发送一个含有编码格式的响应头
response.setHeader("Content-Type", "text/html;charset=utf-8");
response.getWriter().println("File Not Found
");
response.getWriter().println("文件未找到
");
}
}
//2.发送一个含有编码格式的响应头
@WebServlet("/response1")
public class ResponseServlet1 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置响应状态码
response.setStatus(404);
//设置响应头
//response.setHeader("Content-Type", "text/html");
response.setHeader("Server", "Tomcat 8.5");
//设置响应体
//response.getWriter().println(new Date());
//response.setCharacterEncoding("gbk");
//因为该行代码由两层含义:1.设置响应体编码格式2.发送一个含有编码格式的响应头
//response.setHeader("Content-Type", "text/html;charset=utf-8");
response.setContentType("text/html;charset=utf-8");
response.getWriter().println("File Not Found
");
response.getWriter().println("文件未找到
");
}
}
//3.通过响应体告知
//3.1.设置编码格式 3.2.告知对方我使用的编码格式
@WebServlet("/response2")
public class ResponseServlet2 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置响应状态码
response.setStatus(404);
//设置响应头
response.setHeader("Server", "Tomcat 8.5");
//设置响应体
response.setCharacterEncoding("utf-8");
response.getWriter().println("\n" +
"\n" +
"\n" +
" \n" +
" Title \n" +
"\n" +
"");
response.getWriter().println("File Not Found
");
response.getWriter().println("文件未找到
");
response.getWriter().println("\n" +
"\n" +
"");
}
}
@WebServlet("/stream")
public class OutputStreamServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//可以设置,也可以不设置,如果不设置的话浏览器会自动帮我们设置好。
response.setContentType("image/jpg");
ServletOutputStream outputStream = response.getOutputStream();
//可以把这个当作FileOutputStram来看待,但是呢,行为应该是相同的,输出的目的地不同
//需要你将本地部署根目录下面的1.jpg 输出到客户端
//硬盘上面的文件,读取到内存中 FileInputStream来处理这一个过程
//部署根目录下面1.jpg的输入流拿到
//先拿到文件的绝对路径
String realPath = getServletContext().getRealPath("1.jpg");
FileInputStream fileInputStream = new FileInputStream(realPath);
//输出到response的响应体即可
int length = 0;
byte[] bytes = new byte[1024];
while ((length = fileInputStream.read(bytes)) != -1){
outputStream.write(bytes, 0, length);
}
fileInputStream.close();
//输出流可以关闭,也可以不关闭,如果不关闭,那么tomcat会在响应的时候关闭他们
outputStream.flush();
outputStream.close();
}
}
@WebServlet("/download")
public class DownloadServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置响应头
response.setHeader("Content-Disposition","attachment;filename=1.jpg");
response.setContentType("image/jpg");
ServletOutputStream outputStream = response.getOutputStream();
//可以把这个当作FileOutputStram来看待,但是行为应该是相同的,输出的目的地不同
//需要你将本地部署根目录下面的1.jpg 输出到客户端
//硬盘上面的文件,读取到内存种 FileInputStream来处理这一个过程
//部署根目录下面1.jpg的输入流拿到
//先拿到文件的绝对路径
String realPath = getServletContext().getRealPath("1.jpg");
FileInputStream fileInputStream = new FileInputStream(realPath);
//输出到response的响应体即可
int length = 0;
byte[] bytes = new byte[1024];
while ((length = fileInputStream.read(bytes)) != -1){
outputStream.write(bytes, 0, length);
}
fileInputStream.close();
//输出流可以关闭,也可以不关闭,如果不关闭,那么tomcat会在响应的时候关闭
outputStream.flush();
outputStream.close();
}
}