字节流响应
@WebServlet(name = "ServletDemo18", value = "/ServletDemo18")
public class ServletDemo18 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置客户端解码格式
response.setContentType("text/html;charset=UTF-8");
//获取字节输出流对象
ServletOutputStream os = response.getOutputStream();
String str = "张邵";
//设置流编码格式
os.write(str.getBytes(StandardCharsets.UTF_8));
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
}
字符流响应
@WebServlet(name = "ServletDemo19", value = "/ServletDemo19")
public class ServletDemo19 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter pw = response.getWriter();
String str = "张邵";
pw.write(str);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
}
响应图片
@WebServlet(name = "ServletDemo20", value = "/ServletDemo20")
public class ServletDemo20 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//创建字节输入流对象,关联图片路径
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(getServletContext().getRealPath("/img/img.png")));
//获取输出流
ServletOutputStream os = response.getOutputStream();
//读写数据
byte[] arr = new byte[1024];
int len;
while ((len = bis.read(arr)) != -1){
os.write(arr,0,len);
}
//释放资源
bis.close();
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
}
设置缓存(了解)
@WebServlet(name = "ServletDemo21", value = "/ServletDemo21")
public class ServletDemo21 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置缓存1小时
response.setDateHeader("Expires",System.currentTimeMillis() + 1*60*60*1000);
response.setContentType("text/heml;charset=UTF-8");
response.getWriter().write("张邵");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
}
设置定时刷新
@WebServlet(name = "ServletDemo22", value = "/ServletDemo22")
public class ServletDemo22 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
response.getWriter().write("3秒后跳转页面");
response.setHeader("Refresh","3;URL=/regist.html");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
}
重定向
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//重定向
response.sendRedirect(request.getContextPath() + "/ServletDemo24");
//response.sendRedirect("https://www.baidu.com");
}
重定向会丢失请求域
浏览器地址会发生变化,有两次请求发生
文件下载
@WebServlet(name = "ServletDemo24", value = "/ServletDemo24")
public class ServletDemo24 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//创建字节输入流对象,关联读取的文件
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(getServletContext().getRealPath("/img/img.png")));
//设置响应头支持的类型
/*
Context-Type 支持的类型
application/octet-stream 应用的类型为字节流
*/
response.setHeader("Context-Type","application/octet-stream");
/*
Content-Disposition 处理的形式
attachment;filename=img.png 附件形式处理 下载的文件名
*/
response.setHeader("Content-Disposition","attachment;filename=img.png");
//字节输出流对象
ServletOutputStream os = response.getOutputStream();
//循环读写
byte[] arr = new byte[1024];
int len;
while ((len = bis.read(arr)) != -1){
os.write(arr,0,len);
}
//释放资源
bis.close();
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
}