只要你有一台电脑或者手机,都能关注图老师为大家精心推荐的基于HttpServletResponse 相关常用方法的应用,手机电脑控们准备好了吗?一起看过来吧!
【 tulaoshi.com - 编程语言 】
public void filedownload(HttpServletResponse response) throws Exception {
  ServletContext context = this.getServletContext();
  String path = context.getRealPath("/download/awf.jpg");
  String filename = path.substring(path.lastIndexOf("") + 1);
  // 如果下载文件为中文文件,则文件名需要经过url编码;
  response.setHeader("Content-disposition", "attachment;filename="+ URLEncoder.encode(filename, "UTF-8"));
  InputStream in = new FileInputStream(path);
  int len = 0;
  byte[] buffer = new byte[1024];
  OutputStream out = response.getOutputStream();
  while ((len = in.read(buffer))  0) {
  out.write(buffer, 0, len);
  }
  in.close();
  out.close();
}
BeanUtils使用:
BeanUtils.pupulate(bean,MapInstance);//用map装载bean,map中存有bean属性对应的key以及key对应的值;
BeanUtils.copyProperties(bean,MapInstance);//将map拷贝到bean中;
转发是一次请求,使用的是相同的response和request;
页面跳转:
1String message = "meta http-equiv='refresh' content='3;url=/webTwo/index.jsp'a href='webTwo/index.jsp'AAAA/a";
  this.getServletContext().setAttribute("message", message);
  this.getServletContext().getRequestDispatcher("/message.jsp").forward(request, response);//将消息带到message页面进行显示;
2response.setHeader("refresh", "3;url='/webTwo/index.jsp'");
  response.getWriter().write("恭喜登录成功,如果没有中转,请点击超链接a href='webTwo/index.jsp'AAAA/a");
程序编码:
// 程序以什么码表输出,就一定要控制浏览器以什么码表打开;
  // 用html中的meta技术模拟http响应头,来控制浏览器的行为;
  // out.write("meta http-equiv='content-type' content='text/html;charset=UTF-8'".getBytes());
  
  response.setCharacterEncoding("UTF-8");// 设置response使用的码表,控制response以什么码表向浏览器写出数据;
  response.setHeader("Content-type", "text/html;charset=UTF-8");// 指定浏览器以什么码表打开数据;
  // 相当上面两句话:
  // response.setContentType("text/html;charset=UTF-8");
Response.setDateHeader("expires",System.currentTimeMillis() + 1000*3600);//设置session有效时间10分钟;
Response.getWriter().write(data); Response.getWriter() --  return PrintWriter;
Response.setHeader("refresh","3");
来源:http://www.tulaoshi.com/n/20160219/1593750.html
看过《基于HttpServletResponse 相关常用方法的应用》的人还看了以下文章 更多>>