随机生成登录时的验证码图片
生成随机验证码的代码:
1 package servlet; 2 3 import java.awt.Color; 4 import java.awt.Font; 5 import java.awt.Graphics; 6 import java.awt.Graphics2D; 7 import java.awt.image.BufferedImage; 8 import java.io.IOException; 9 import java.io.OutputStream; 10 import java.io.PrintWriter; 11 import java.util.Random; 12 13 import javax.imageio.ImageIO; 14 import javax.servlet.ServletException; 15 import javax.servlet.http.HttpServlet; 16 import javax.servlet.http.HttpServletRequest; 17 import javax.servlet.http.HttpServletResponse; 18 19 public class servletDemo2 extends HttpServlet { 20 21 public static final int WIDTH=120; 22 public static final int HEIGHT=50; 23 24 public void doGet(HttpServletRequest request, HttpServletResponse response) 25 throws ServletException, IOException { 26 27 BufferedImage image=new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); 28 Graphics g=image.getGraphics(); 29 //1、设置背景颜色 30 setBackGround(g); 31 //2、设置边框 32 setBorder(g); 33 //3、画干扰线 34 drawRandomLine(g); 35 //4、写随机数 36 drawRandomNum((Graphics2D)g); 37 //5、图形写给浏览器 38 response.setContentType("image/jpeg"); 39 //发头控制浏览器不要缓存 40 response.setDateHeader("expries", -1); 41 response.setHeader("Cache-Control", "no-cache"); 42 response.setHeader("Pragma", "no-cache"); 43 //图形写给浏览器 44 ImageIO.write(image, "jpg", response.getOutputStream()); 45 46 47 48 } 49 50 private void setBackGround(Graphics g) { 51 g.setColor(Color.WHITE); 52 g.fillRect(0, 0, WIDTH, HEIGHT); 53 54 55 } 56 private void setBorder(Graphics g) { 57 g.setColor(Color.BLUE); 58 g.drawRect(1, 1, WIDTH-2, HEIGHT-2); 59 60 } 61 private void drawRandomLine(Graphics g) { 62 g.setColor(Color.GREEN); 63 for(int i=0;i<10;i++) 64 { 65 int x1=new Random().nextInt(WIDTH); 66 int y1=new Random().nextInt(HEIGHT); 67 int x2=new Random().nextInt(WIDTH); 68 int y2=new Random().nextInt(HEIGHT); 69 g.drawLine(x1, y1, x2, y2); 70 71 } 72 73 } 74 private void drawRandomNum(Graphics2D g) { 75 76 g.setColor(Color.RED); 77 g.setFont(new Font("楷体",Font.BOLD,20)); 78 String base="\u7684\u4e00\u662f\u4e86\u6211\u4e0d\u4eba\u5728\u4ed6\u6709\u8fd9\u4e2a\u4e0a\u4eec\u6765"; 79 int x=5; 80 for(int i=0;i<4;i++) 81 { 82 int degree=new Random().nextInt()%30; 83 String ch=base.charAt(new Random().nextInt(base.length()))+""; 84 g.rotate(degree*3.14/180, x, 25); 85 g.drawString(ch, x, 25); 86 g.rotate(-degree*3.14/180, x, 25); 87 x=x+30; 88 } 89 } 90 91 92 public void doPost(HttpServletRequest request, HttpServletResponse response) 93 throws ServletException, IOException { 94 95 doGet(request,response); 96 } 97 98 }
在下面的html页面中可以调用此验证码图片:
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <title>regist.html</title> 5 <script type="text/javascript"> 6 function changeImage(img) 7 { 8 img.src=img.src+"?"+new Date().getTime(); 9 } 10 </script> 11 12 </head> 13 14 <body> 15 <form action=""> 16 用户名:<input type="text" name="username"></br> 17 密码: <input type="password" name="password"></br> 18 验证码:<input type="text" name="checkcode"></br> 19 <img src="/day_05/servlet/servletDemo2" onclick="changeImage(this)" alt="换一张" style="cursor:hand"></br> 20 <input type="submit" value="注册"> 21 22 23 </form> 24 </body> 25 </html>