JavaWeb15.3【response:案例-服务器输出字符数据到浏览器(中文乱码问题)、服务器输出字节数据到浏览器、验证码生成并输出到浏览器】
1 package com.haifei.servlet; 2 3 import javax.servlet.ServletException; 4 import javax.servlet.annotation.WebServlet; 5 import javax.servlet.http.HttpServlet; 6 import javax.servlet.http.HttpServletRequest; 7 import javax.servlet.http.HttpServletResponse; 8 import java.io.IOException; 9 import java.io.PrintWriter; 10 11 /** 12 * Response对象案例-服务器输出字符数据到浏览器 13 */ 14 @WebServlet("/responseDemo4") 15 public class ResponseDemo4 extends HttpServlet { 16 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 17 /*//1 获取字符输出流 18 PrintWriter pw = response.getWriter(); 19 //response在一次响应完成后会被自动销毁,同时它获取的流getWriter()也会被自动关闭 20 //之前学流的时候:.write()后需要flush()才能写入;close()自带flush效果 21 //所以此处不需要在pw.write()手动对pw进行flush() 22 23 //2 输出数据 24 pw.write("<h1>hello response</h1>"); //即为页面上的响应体*/ 25 26 27 28 /* //获取流对象之前,流的默认编码为ISO-8859-1,手动设置为utf-8 29 response.setCharacterEncoding("utf-8"); 30 //告诉浏览器,服务器发送的消息体数据的编码。建议浏览器使用该编码解码 31 response.setHeader("content-type","text/html;charset=utf-8"); //content-type是一种常见的响应头*/ 32 //简单的形式:设置编码并告诉浏览器,防止中文乱码 33 response.setContentType("text/html;charset=utf-8"); 34 35 PrintWriter pw = response.getWriter(); 36 pw.write("<h1>你好啊 response</h1>"); 37 } 38 39 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 40 this.doPost(request, response); 41 } 42 }
1 package com.haifei.servlet; 2 3 import javax.servlet.ServletException; 4 import javax.servlet.ServletOutputStream; 5 import javax.servlet.annotation.WebServlet; 6 import javax.servlet.http.HttpServlet; 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletResponse; 9 import java.io.IOException; 10 11 /** 12 * Response对象案例-服务器输出字节数据到浏览器 13 */ 14 @WebServlet("/responseDemo5") 15 public class ResponseDemo5 extends HttpServlet { 16 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 17 response.setContentType("text/html;charset=utf-8"); //设置编码 18 //1 获取字节输出流 19 ServletOutputStream sos = response.getOutputStream(); 20 //2 输出数据 21 sos.write("你好啊hello".getBytes("utf-8")); 22 } 23 24 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 25 this.doPost(request, response); 26 } 27 }
1 package com.haifei.servlet; 2 3 import javax.imageio.ImageIO; 4 import javax.servlet.ServletException; 5 import javax.servlet.ServletOutputStream; 6 import javax.servlet.annotation.WebServlet; 7 import javax.servlet.http.HttpServlet; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse; 10 import java.awt.*; 11 import java.awt.image.BufferedImage; 12 import java.io.IOException; 13 import java.util.Random; 14 15 /** 16 * Response对象案例-验证码 17 */ 18 @WebServlet("/checkCodeServlet") 19 public class CheckCodeServlet extends HttpServlet { 20 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 21 int width = 100; 22 int height = 50; 23 24 //1 创建对象,再内存中画图(验证码图片对象) 25 BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 26 27 //2 美化图片 28 //2.1 填充背景色 29 Graphics g = image.getGraphics();//画笔对象 30 g.setColor(Color.PINK);//设置画笔颜色 31 g.fillRect(0,0,width,height); 32 //2.2画边框 33 g.setColor(Color.BLUE); 34 g.drawRect(0,0,width - 1,height - 1); 35 //2.3画验证码 36 String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghigklmnopqrstuvwxyz0123456789"; 37 Random ran = new Random(); 38 for (int i = 1; i <= 4; i++) { 39 //生成随机下标 40 int index = ran.nextInt(str.length()); 41 //获取随机字符 42 char ch = str.charAt(index); 43 //写验证码 44 g.drawString(ch+"",width/5*i,height/2); 45 } 46 //2.4画干扰线 47 g.setColor(Color.GREEN); 48 for (int i = 0; i < 10; i++) { //随机生成坐标点 49 int x1 = ran.nextInt(width); 50 int x2 = ran.nextInt(width); 51 int y1 = ran.nextInt(height); 52 int y2 = ran.nextInt(height); 53 g.drawLine(x1,y1,x2,y2); 54 } 55 56 //3 将图片输出到页面展示 57 ImageIO.write(image, "jpg", response.getOutputStream()); 58 } 59 60 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 61 this.doPost(request, response); 62 } 63 }
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Title</title> 6 7 <script> 8 /* 9 分析: 10 点击超链接或者图片,需要换一张 11 1.给超链接和图片绑定单击事件 12 2.重新设置图片的src属性值 13 */ 14 window.onload = function () { 15 //获取图片对象 16 var img = document.getElementById("checkCode"); 17 //绑定单击事件 18 img.onclick = function () { 19 // img.src = "/day15/checkCodeServlet"; //因为浏览器缓存而访问原路径时达不到效果 20 21 //+时间戳 22 var data = new Date().getTime(); 23 img.src = "/day15/checkCodeServlet?" + data; 24 }; 25 26 var a = document.getElementById("change"); 27 a.onclick = function () { 28 var data = new Date().getTime(); 29 img.src = "/day15/checkCodeServlet?" + data; 30 // alert("/day15/checkCodeServlet?" + data); 31 }; 32 } 33 </script> 34 </head> 35 <body> 36 <img id="checkCode" src="/day15/checkCodeServlet" /> 37 <a id="change" href="">看不清换一张?</a> 38 </body> 39 </html>