网上图书商城项目学习笔记-006验证码功能
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了网上图书商城项目学习笔记-006验证码功能相关的知识,希望对你有一定的参考价值。
一、使用方法
1.jsp中
1 <tr> 2 <td> </td> 3 <td> 4 <span class="verifyCodeImg"><img id="vCode" width="100" src="<c:url value=‘/VerifyCodeServlet‘/>" /></span> 5 </td> 6 <td><a href="javascript: _changeVerifyCode()">换一张</a></td> 7 </tr> 8 <tr>
2.web.xml中配置servlet
1 <!-- 生成验证码 --> 2 <servlet> 3 <servlet-name>VerifyCodeServlet</servlet-name> 4 <servlet-class>cn.itcast.vcode.servlet.VerifyCodeServlet</servlet-class> 5 </servlet> 6 <servlet-mapping> 7 <servlet-name>VerifyCodeServlet</servlet-name> 8 <url-pattern>/VerifyCodeServlet</url-pattern> 9 </servlet-mapping>
二、码代码
1.VerifyCodeServlet.java
1 package cn.itcast.vcode.servlet; 2 3 import java.awt.image.BufferedImage; 4 import java.io.IOException; 5 6 import javax.servlet.ServletException; 7 import javax.servlet.http.HttpServlet; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse; 10 11 import cn.itcast.vcode.utils.VerifyCode; 12 13 @SuppressWarnings("serial") 14 public class VerifyCodeServlet extends HttpServlet { 15 public void doGet(HttpServletRequest request, HttpServletResponse response) 16 throws ServletException, IOException { 17 VerifyCode vc = new VerifyCode(); 18 BufferedImage image = vc.getImage();//获取一次性验证码图片 19 // 该方法必须在getImage()方法之后来调用 20 // System.out.println(vc.getText());//获取图片上的文本 21 VerifyCode.output(image, response.getOutputStream());//把图片写到指定流中 22 23 // 把文本保存到session中,为LoginServlet验证做准备 24 request.getSession().setAttribute("vCode", vc.getText()); 25 } 26 }
2.VerifyCode.java
1 package cn.itcast.vcode.utils; 2 3 import java.awt.BasicStroke; 4 import java.awt.Color; 5 import java.awt.Font; 6 import java.awt.Graphics2D; 7 import java.awt.image.BufferedImage; 8 import java.io.IOException; 9 import java.io.OutputStream; 10 import java.util.Random; 11 12 import javax.imageio.ImageIO; 13 14 public class VerifyCode { 15 private int w = 70; 16 private int h = 35; 17 private Random r = new Random(); 18 // {"宋体", "华文楷体", "黑体", "华文新魏", "华文隶书", "微软雅黑", "楷体_GB2312"} 19 private String[] fontNames = {"宋体", "华文楷体", "黑体", "微软雅黑", "楷体_GB2312"}; 20 private String codes = "23456789abcdefghjkmnopqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ"; 21 private Color bgColor = new Color(255, 255, 255); 22 private String text ; 23 24 private Color randomColor () { 25 int red = r.nextInt(150); 26 int green = r.nextInt(150); 27 int blue = r.nextInt(150); 28 return new Color(red, green, blue); 29 } 30 31 private Font randomFont () { 32 int index = r.nextInt(fontNames.length); 33 String fontName = fontNames[index]; 34 int style = r.nextInt(4); 35 int size = r.nextInt(5) + 24; 36 return new Font(fontName, style, size); 37 } 38 39 private void drawLine (BufferedImage image) { 40 int num = 3; 41 Graphics2D g2 = (Graphics2D)image.getGraphics(); 42 for(int i = 0; i < num; i++) { 43 int x1 = r.nextInt(w); 44 int y1 = r.nextInt(h); 45 int x2 = r.nextInt(w); 46 int y2 = r.nextInt(h); 47 g2.setStroke(new BasicStroke(1.5F)); 48 g2.setColor(Color.BLUE); 49 g2.drawLine(x1, y1, x2, y2); 50 } 51 } 52 53 private char randomChar () { 54 int index = r.nextInt(codes.length()); 55 return codes.charAt(index); 56 } 57 58 private BufferedImage createImage () { 59 BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); 60 Graphics2D g2 = (Graphics2D)image.getGraphics(); 61 g2.setColor(this.bgColor); 62 g2.fillRect(0, 0, w, h); 63 return image; 64 } 65 66 public BufferedImage getImage () { 67 BufferedImage image = createImage(); 68 Graphics2D g2 = (Graphics2D)image.getGraphics(); 69 StringBuilder sb = new StringBuilder(); 70 // 向图片中画4个字符 71 for(int i = 0; i < 4; i++) { 72 String s = randomChar() + ""; 73 sb.append(s); 74 float x = i * 1.0F * w / 4; 75 g2.setFont(randomFont()); 76 g2.setColor(randomColor()); 77 g2.drawString(s, x, h-5); 78 } 79 this.text = sb.toString(); 80 drawLine(image); 81 return image; 82 } 83 84 public String getText () { 85 return text; 86 } 87 88 public static void output (BufferedImage image, OutputStream out) 89 throws IOException { 90 ImageIO.write(image, "JPEG", out); 91 } 92 }
以上是关于网上图书商城项目学习笔记-006验证码功能的主要内容,如果未能解决你的问题,请参考以下文章