- 什么是Responese对象?
- 怎么使用Response对象?
- 重定向
- ServletContext对象
- 文件下载
1. 什么是Responese对象?
Web服务器收到客户端的http请求,会针对每一次请求,分别创建一个用于代表请求的request对象、和代表响应的response对象。
服务器-->客户端 的返回的响应就是response对象。
2. 怎么使用Response对象?
2.1 设置响应内容
@WebServlet("/failServlet")
public class FailServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//设置编码
resp.setContentType("text/html;charset=utf-8");
//获取存放数据
//String data =(String) req.getAttribute("shareData");
//设置响应内容
resp.getWriter().write("登录失败,用户名或密码错误");
}
}
3. 重定向
重定向之前页面设置
@WebServlet("/demo")
public class ServletDemo extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.sendRedirect("/JavaWEB/servletDemo2");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req,resp);
}
}
重定向完成后页面设置
@WebServlet("/servletDemo2")
public class ServletDemo2 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置响应内容
response.getWriter().write("欢迎,demo2");
}
}
效果展示

处理中文乱码设置
//设置响应编码格式
response.setContentType("text/html;charset=utf-8");

4.ServletConetext对象
4.1 什么是ServletConetext对象?
ServletContext用来存放全局变量,每个Java虚拟机每个Web项目只有一个ServletContext,这个ServletContext是由Web服务器创建的。所以ServletContext的作用范围是整个应用。
4.2 怎么使用ServletConetext对象?
示例代码
ServletDemo1
@WebServlet("/demo1")
public class ServletDemo1 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//设置响应编码
resp.setContentType("text/html;charset=utf-8");
//设置servletContext对象
ServletContext servletContext = this.getServletContext();
//共享数据
servletContext.setAttribute("code","999");
//设置提示信息
resp.getWriter().write("demo1的code信息已经写入");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req,resp);
}
ServletDemo2
@WebServlet("/demo2")
public class ServletDemo2 extends HttpServlet {
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//设置编码格式
resp.setContentType("text/html;charset=utf-8");
ServletContext servletContext = this.getServletContext();
String code = (String)servletContext.getAttribute("code");
//设置响应内容
resp.getWriter().write("demo中的code信息为:"+code);
}
}
运行结果


4.3 ServletConetext有什么功能?
- 多个Servlet通过ServletContext对象实现数据共享
- 实现Servlet的请求转发
- 利用ServletContext对象读取资源文件
String getRealPath(String path)
String b = context.getRealPath("/b.txt");//web目录下资源访问
System.out.println(b);
String c = context.getRealPath("/WEB-INF/c.txt");//WEB-INF目录下的资源访问
System.out.println(c);
String a = context.getRealPath("/WEB-INF/classes/a.txt");//src目录下的资源访问
System.out.println(a);
5.文件下载
文件下载需求:
- 页面显示超链接
- 点击超链接后弹出下载提示框
- 完成图片文件下载
分析:
- 超链接指向的资源如果能够被浏览器解析,则在浏览器中展示,如果不能解析,则弹出下载提示框。不满足需求
- 任何资源都必须弹出下载提示框
- 使用响应头设置资源的打开方式:
content-disposition:attachment;filename=xxx
步骤:
- 定义页面,编辑超链接href属性,指向Servlet,传递资源名称filename
- 定义Servlet:
- 获取文件名称
- 使用字节输入流加载文件进内存
- 指定response的响应头: content-disposition:attachment;filename=xxx
- 将数据写出到response输出流
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>youbao三岁</title>
</head>
<body>
<a href="/Web/download?filename=2019.jpg">图片</a>
<a href="/Web/download?filename=2018.mp4">视频</a>
</body>
</html>
@WebServlet("/download")
public class DownloadServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req, resp);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1.获取请求参数,文件名称
String filename = req.getParameter("filename");
//2.使用字节输入流加载文件进内存
//2.1找到文件服务器路径
ServletContext servletContext = this.getServletContext();
String realPath = servletContext.getRealPath("/WEB-INF/classes/" + filename);
//2.2用字节流关联
FileInputStream fis = new FileInputStream(realPath);
//3.设置response的响应头
//3.1设置响应头类型:content-type
String mimeType = servletContext.getMimeType(filename);//获取文件的mime类型
resp.setHeader("content-type",mimeType);
//3.2设置响应头打开方式:content-disposition
resp.setHeader("content-disposition","attachment;filename="+filename);
//4.将输入流的数据写出到输出流中
ServletOutputStream sos = resp.getOutputStream();
byte[] buff = new byte[1024 * 8];
int len = 0;
while((len = fis.read(buff)) != -1){
sos.write(buff,0,len);
}
fis.close();
}
}
网友评论