首页 > 其他 > 详细

利用servlet技术实现验证码功能

时间:2015-10-04 22:20:23      阅读:344      评论:0      收藏:0      [点我收藏+]
 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>  
 2   
 3 <%  
 4     String action = request.getParameter("action");  
 5     String safecodeText = request.getParameter("safecodeTest");  
 6     if("action".equals(action)){  
 7         String safecode = (String)session.getAttribute("safecode");  
 8         if(safecode.equals(safecodeText)){  
 9             out.print("验证码正确!");  
10         }else{  
11             out.print("验证码错误!请重新输入!");  
12         }  
13     }  
14 %>  
15 <html>  
16   <head>  
17     <title>验证码测试</title>  
18   </head>  
19     
20   <body>  
21     <form action="servlet/demo" method="post">  
22     <input type="hidden" name="action" value="action"/>  
23         <img alt="验证码" src="servlet/demo">  
24         <input type="text" name="safecodeTest">  
25         <input type="submit" value="go">  
26     </form>  
27   </body>  
28 </html> 
 1 import java.io.*;  
 2   
 3 import javax.servlet.*;  
 4 import javax.servlet.http.*;  
 5 import java.util.Random;  
 6 import java.awt.*;  
 7 import java.awt.image.*;  
 8 import javax.imageio.*;  
 9 
10   
11 public class demo extends HttpServlet {  
12     //产生随即的字体  
13     private Font getFont() {  
14         Random random = new Random();  
15         Font font[] = new Font[5];  
16         font[0] = new Font("Ravie", Font.PLAIN, 24);  
17         font[1] = new Font("Antique Olive Compact", Font.PLAIN, 24);  
18         font[2] = new Font("Forte", Font.PLAIN, 24);  
19         font[3] = new Font("Wide Latin", Font.PLAIN, 24);  
20         font[4] = new Font("Gill Sans Ultra Bold", Font.PLAIN, 24);  
21         return font[random.nextInt(5)];  
22     }  
23   
24     protected void doGet(HttpServletRequest req, HttpServletResponse resp)  
25             throws ServletException, IOException {  
26         // 设置响应头 Content-type类型  
27         resp.setContentType("image/jpeg");  
28         // 以下三句是用于设置页面不缓存  
29         resp.setHeader("Pragma", "No-cache");  
30         resp.setHeader("Cache-Control", "No-cache");  
31         resp.setDateHeader("Expires", 0);  
32   
33         OutputStream os = resp.getOutputStream();  
34         int width = 83, height = 30;  
35         // 建立指定宽、高和BufferedImage对象  
36         BufferedImage image = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);  
37   
38         Graphics g = image.getGraphics(); // 该画笔画在image上  
39         Color c = g.getColor(); // 保存当前画笔的颜色,用完画笔后要回复现场  
40         g.fillRect(0, 0, width, height);  
41           
42   
43         char[] ch = "abcdefghjkmnpqrstuvwxyz23456789".toCharArray(); // 随即产生的字符串 不包括 i l(小写L) o(小写O) 1(数字1)0(数字0)  
44         int length = ch.length; // 随即字符串的长度  
45         String sRand = ""; // 保存随即产生的字符串  
46         Random random = new Random();  
47         for (int i = 0; i < 4; i++) {  
48             // 设置字体  
49             g.setFont(getFont());  
50             // 随即生成0-9的数字  
51             String rand = new Character(ch[random.nextInt(length)]).toString();  
52             sRand += rand;  
53             // 设置随机颜色  
54             g.setColor(new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255)));  
55             g.drawString(rand, 20 * i + 6, 25);  
56         }  
57         //产生随即干扰点  
58         for (int i = 0; i < 20; i++) {  
59             int x1 = random.nextInt(width);  
60             int y1 = random.nextInt(height);  
61             g.drawOval(x1, y1, 2, 2);  
62         }  
63         g.setColor(c); // 将画笔的颜色再设置回去  
64         g.dispose();  
65   
66         //将验证码记录到session  
67         req.getSession().setAttribute("safecode", sRand);  
68         // 输出图像到页面  
69         ImageIO.write(image, "JPEG", os);  
70   
71     }  
72   
73     protected void doPost(HttpServletRequest req, HttpServletResponse resp)  
74             throws ServletException, IOException {  
75         doGet(req, resp);  
76     }  
77   
78 }  
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
 3   <display-name>JavaWebDemo</display-name>
 4   <servlet>
 5     <description>This is the description of my J2EE component</description>
 6     <display-name>This is the display name of my J2EE component</display-name>
 7     <servlet-name>demo</servlet-name>
 8     <servlet-class>demo</servlet-class>
 9   </servlet>
10 
11   <servlet-mapping>
12     <servlet-name>demo</servlet-name>
13     <url-pattern>/servlet/demo</url-pattern>
14   </servlet-mapping>
15  
16   <welcome-file-list>
17     <welcome-file>index.html</welcome-file>
18     <welcome-file>index.htm</welcome-file>
19     <welcome-file>index.jsp</welcome-file>
20     <welcome-file>default.html</welcome-file>
21     <welcome-file>default.htm</welcome-file>
22     <welcome-file>default.jsp</welcome-file>
23   </welcome-file-list>
24 </web-app>

 

利用servlet技术实现验证码功能

原文:http://www.cnblogs.com/rain-tl/p/4855020.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!