1: 利用Response向浏览器输出中文:
private void test1(HttpServletResponse response) throws IOException { String data1 = "aaaa"; String data2 = "中文"; //向浏览器输出中文方法1: 利用 OutputStream 和 PrintWriter 输出 //字节流可是输出任何东西, 注意response.getOutputStream(); 与 response.getWriter(); 不能同时存在 /* OutputStream out = response.getOutputStream(); out.write(data1.getBytes()); out.write(data2.getBytes()); */ //字符流会出现中文乱码问题 //解决方法 response.setCharacterEncoding("UTF-8"); //设置response所查的码表 PrintWriter pw = response.getWriter(); pw.write(data1); pw.write(data2); } //向浏览器输出中文 private void test2(HttpServletResponse response) throws IOException, UnsupportedEncodingException { String data = "中文"; OutputStream out = response.getOutputStream(); out.write("<meta http-equiv='content-type' content='text/html; charset=UTF-8'>".getBytes()); //控制浏览器以什么码表解析 out.write(data.getBytes("UTF-8")); // 将数据以UTF-8码表所对应的编码发给浏览器 }
2: 实现web资源的下载:
//利用response实现文件下载 private void teest3(HttpServletResponse response) throws UnsupportedEncodingException, FileNotFoundException, IOException { String path = this.getServletContext().getRealPath("/download/美女.jpg"); String filename = path.substring(path.lastIndexOf("\") + 1); System.out.println(path); System.out.println("filename = " + filename); //response.setHeader("content-disposition", "attachment; filename=" + filename); //中文的要进行URL编码 response.setHeader("content-disposition", "attachment; filename=" + URLEncoder.encode(filename, "UTF-8")); InputStream in = new FileInputStream(path); OutputStream out = response.getOutputStream(); int len = 0; byte[] buf = new byte[1024]; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } }
3: 实现登陆注册时,验证码的生成与验证:
package cn.response.com; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.IOException; import java.io.PrintWriter; import java.util.Random; import javax.imageio.ImageIO; import javax.jms.Session; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; public class ResDemo2 extends HttpServlet { private int WIDTH = 120; private int HEIGHT = 25; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { BufferedImage bufimg = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); Graphics g = bufimg.getGraphics(); HttpSession session = request.getSession(); //1:设置背景色 setBackGround(g); //2:设置边框 setBorder(g); //3:画干扰线 drawRandomLine(g); //4:写随机数 drawRandomNum(session,(Graphics2D)g); //5图形写给浏览器 //设置浏览器不要缓存 response.setHeader("expires", "-1"); response.setHeader("Cache-Control", "no-cache"); response.setHeader("Pragma", "no-cache"); response.setContentType("image/jpeg"); ImageIO.write(bufimg, "jpg", response.getOutputStream()); } private void setBackGround(Graphics g) { g.setColor(Color.WHITE); g.fillRect(0, 0, WIDTH, HEIGHT); } private void setBorder(Graphics g) { g.setColor(Color.BLUE); g.drawRect(1, 1, WIDTH - 2, HEIGHT - 2); } private void drawRandomLine(Graphics g) { g.setColor(Color.GREEN); for (int i = 0; i < 8; ++i) { int x1 = new Random().nextInt(WIDTH); int y1 = new Random().nextInt(HEIGHT); int x2 = new Random().nextInt(WIDTH); int y2 = new Random().nextInt(HEIGHT); g.drawLine(x1, y1, x2, y2); } } //常见中文的编码 String base = "u7684u4e00u4e86u662fu6211u4e0du5728u4ebau4eecu6709u6765u4ed6u8fd9u4e0au7740u4e2au5730u5230u5927u91ccu8bf4u5c31u53bbu5b50u5f97u4e5fu548cu90a3u8981u4e0bu770bu5929u65f6u8fc7u51fau5c0fu4e48u8d77u4f60u90fdu628au597du8fd8u591au6ca1u4e3au53c8u53efu5bb6u5b66u53eau4ee5u4e3bu4f1au6837u5e74u60f3u751fu540cu8001u4e2du5341u4eceu81eau9762u524du5934u9053u5b83u540eu7136u8d70u5f88u50cfu89c1u4e24u7528u5979u56fdu52a8u8fdbu6210u56deu4ec0u8fb9u4f5cu5bf9u5f00u800cu5df1u4e9bu73b0u5c71u6c11u5019u7ecfu53d1u5de5u5411u4e8bu547du7ed9u957fu6c34u51e0u4e49u4e09u58f0u4e8eu9ad8u624bu77e5u7406u773cu5fd7u70b9u5fc3u6218u4e8cu95eeu4f46u8eabu65b9u5b9eu5403u505au53ebu5f53u4f4fu542cu9769u6253u5462u771fu5168u624du56dbu5df2u6240u654cu4e4bu6700u5149u4ea7u60c5u8defu5206u603bu6761u767du8bddu4e1cu5e2du6b21u4eb2u5982u88abu82b1u53e3u653eu513fu5e38u6c14u4e94u7b2cu4f7fu5199u519bu5427u6587u8fd0u518du679cu600eu5b9au8bb8u5febu660eu884cu56e0u522bu98deu5916u6811u7269u6d3bu90e8u95e8u65e0u5f80u8239u671bu65b0u5e26u961fu5148u529bu5b8cu5374u7ad9u4ee3u5458u673au66f4u4e5du60a8u6bcfu98ceu7ea7u8ddfu7b11u554au5b69u4e07u5c11u76f4u610fu591cu6bd4u9636u8fdeu8f66u91cdu4fbfu6597u9a6cu54eau5316u592au6307u53d8u793eu4f3cu58ebu8005u5e72u77f3u6ee1u65e5u51b3u767eu539fu62ffu7fa4u7a76u5404u516du672cu601du89e3u7acbu6cb3u6751u516bu96beu65e9u8bbau5417u6839u5171u8ba9u76f8u7814u4ecau5176u4e66u5750u63a5u5e94u5173u4fe1u89c9u6b65u53cdu5904u8bb0u5c06u5343u627eu4e89u9886u6216u5e08u7ed3u5757u8dd1u8c01u8349u8d8au5b57u52a0u811au7d27u7231u7b49u4e60u9635u6015u6708u9752u534au706bu6cd5u9898u5efau8d76u4f4du5531u6d77u4e03u5973u4efbu4ef6u611fu51c6u5f20u56e2u5c4bu79bbu8272u8138u7247u79d1u5012u775bu5229u4e16u521au4e14u7531u9001u5207u661fu5bfcu665au8868u591fu6574u8ba4u54cdu96eau6d41u672au573au8be5u5e76u5e95u6df1u523bu5e73u4f1fu5fd9u63d0u786eu8fd1u4eaeu8f7bu8bb2u519cu53e4u9ed1u544au754cu62c9u540du5440u571fu6e05u9633u7167u529eu53f2u6539u5386u8f6cu753bu9020u5634u6b64u6cbbu5317u5fc5u670du96e8u7a7fu5185u8bc6u9a8cu4f20u4e1au83dcu722cu7761u5174u5f62u91cfu54b1u89c2u82e6u4f53u4f17u901au51b2u5408u7834u53cbu5ea6u672fu996du516cu65c1u623fu6781u5357u67aau8bfbu6c99u5c81u7ebfu91ceu575au7a7au6536u7b97u81f3u653fu57ceu52b3u843du94b1u7279u56f4u5f1fu80dcu6559u70edu5c55u5305u6b4cu7c7bu6e10u5f3au6570u4e61u547cu6027u97f3u7b54u54e5u9645u65e7u795eu5ea7u7ae0u5e2eu5566u53d7u7cfbu4ee4u8df3u975eu4f55u725bu53d6u5165u5cb8u6562u6389u5ffdu79cdu88c5u9876u6025u6797u505cu606fu53e5u533au8863u822cu62a5u53f6u538bu6162u53d4u80ccu7ec6"; private void drawRandomNum(HttpSession session, Graphics2D g) { g.setColor(Color.red); g.setFont(new Font("宋体",Font.BOLD, 20)); int x = 5; String checkCode = ""; for (int i = 0; i < 4; ++i) { String ch = base.charAt(new Random().nextInt(base.length())) + ""; checkCode += ch; double degree = new Random().nextInt()%30; g.rotate(degree*Math.PI/180, x, 20); g.drawString(ch, x, 20); g.rotate(-degree*Math.PI/180, x, 20); x += 30; } session.setAttribute("checkCode", checkCode); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
登陆界面:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>登陆</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> <script type="text/javascript"> function change(img) { img.src=img.src+"?"+new Date().getTime(); } </script> </head> <body> <form action="/Test/ResDemo3"> 用户名: <input type="text" name = "usrename"> <br/> 密码: <input type="password" name = "password"> <br/> 验证码:<input type="text" name="checkCode" > <img src="/Test/ResDemo2" onclick="change(this)" alt="换一张" style="cursor: hand"> <br/> <input type="submit" value="登陆"> <br/> </form> </body> </html>
验证:
package cn.response.com; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; public class ResDemo3 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(); System.out.println(session.getAttribute("checkCode")); String a = (String) session.getAttribute("checkCode"); String b = (String) request.getParameter("checkCode"); String tb = new String(b.getBytes("ISO-8859-1"), "UTF-8"); if (a.equals(tb)) { response.sendRedirect("/Test/index.jsp"); } else { System.out.println("error!"); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
4:发送Http头,控制浏览器定时刷新
private void Test2(HttpServletResponse response) throws IOException { /*response.setHeader("refresh", "3;url='/Test/index.jsp'"); //防止中文乱码, 设置编码 response.setCharacterEncoding("UTF-8"); response.setContentType("text/html; charset=UTF-8"); response.getWriter().write("注册成功3秒后自动跳到主页");*/ //利用<meta>标签来模拟响应头 response.getWriter().write("<meta http-equiv='refresh' content='3; url=/Test/index.jsp'>"); } //refresh 实现每个3秒访问一次服务器,取回新数据, 可以用作OJ比赛排名时的Rank private void Test1(HttpServletResponse response) throws IOException { int data = new Random().nextInt(); response.setHeader("refresh", "3"); response.getWriter().write(data + ""); }
5: 禁止缓存,控制缓存
禁止缓存:
//设置浏览器不要缓存 response.setHeader("expires", "-1"); response.setHeader("Cache-Control", "no-cache"); response.setHeader("Pragma", "no-cache");
设置缓存:
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setDateHeader("expires", System.currentTimeMillis() + 1000*3600); // 设置缓存多久 String data = "aaaa"; response.getWriter().write(data); }
6:请求重定向
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //重定向与转发 /* * 重定向,浏览器会向服务器发送两次请求, 生成2个request ,response * 浏览器地址栏会发生变化 * * 因为要向服务器发送两次请求, 所以重定向尽量不要用 * * 主要用在: 登陆, 和购物车实现 */ /*response.setStatus(302); response.setHeader("location", "/Test/index.jsp"); */ //这一句等于上边两句话的执行 response.sendRedirect("/Test/index.jsp"); }