【Java寒假打卡】JavaWeb-响应对象response
概述
状态码
字节流响应消息
package com.hfut.servlet;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet
public class Servlet2 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=UTF-8");
ServletOutputStream os = resp.getOutputStream();
String str = "你好";
os.write(str.getBytes("UTF-8"));
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
}
字符流响应消息
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=UTF-8");
PrintWriter writer = resp.getWriter();
String str = "你好";
writer.write(str);
}
响应图片-字节流
- 创建字节输出流对象,关联读取的图片路径
- 通过响应对象获取字节输出流对象
- 循环读取和写出图片
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String realPath = getServletContext().getRealPath("/img/vue.png");
System.out.println(realPath);
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(realPath));
ServletOutputStream os = resp.getOutputStream();
byte[] arr = new byte[1024];
int len;
while((len = bis.read(arr)) != -1){
os.write(arr,0,len);
}
}
设置缓存时间
- 缓存:对于不经常变化的数据,我们可以设置合理缓存时间,以避免浏览器频繁请求服务器。提高效率
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String news = "这是一条很火爆的新闻!";
resp.setDateHeader("Expires",System.currentTimeMillis() + 1 * 60 * 60 * 1000);
resp.setContentType("text/html;charset=UTF-8");
PrintWriter writer = resp.getWriter();
writer.write(news);
}
定时刷新技术
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String news = "您输入的用户名或者密码有误,3秒之后自动跳转到登录页面";
resp.setContentType("text/html;charset=UTF-8");
PrintWriter writer = resp.getWriter();
writer.write(news);
resp.setHeader("Refresh","3;URL=/test/register.html");
}
请求重定向
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("servlet2执行");
req.setAttribute("uesrname","zhangsan");
resp.sendRedirect(req.getContextPath() + "/servlet1");
}
文件下载
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String realPath = getServletContext().getRealPath("/img/vue.png");
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(realPath));
resp.setHeader("Content-Type","application/octet-stream");
resp.setHeader("Content-Dispostion","attachment;filename=vue.png");
ServletOutputStream outputStream = resp.getOutputStream();
byte[] arr = new byte[1024];
int len;
while((len = bis.read(arr)) != -1){
outputStream.write(arr,0,len);
}
bis.close();
}