JSP Servlet实现验证码
1 package net.cmono.javaweb.c3;
2
3 import java.awt.Color;
4 import java.awt.Font;
5 import java.awt.Graphics2D;
6 import java.awt.image.BufferedImage;
7 import java.io.IOException;
8 import java.io.PrintWriter;
9 import java.util.Random;
10
11 import javax.servlet.ServletException;
12 import javax.servlet.ServletOutputStream;
13 import javax.servlet.http.HttpServlet;
14 import javax.servlet.http.HttpServletRequest;
15 import javax.servlet.http.HttpServletResponse;
16
17 import com.sun.corba.se.impl.ior.NewObjectKeyTemplateBase;
18 import com.sun.image.codec.jpeg.JPEGCodec;
19 import com.sun.image.codec.jpeg.JPEGImageEncoder;
20
21 public class IdentityServlet extends HttpServlet {
22
23 /**
24 * Constructor of the object.
25 */
26 public IdentityServlet() {
27 super();
28 }
29
30 /**
31 * Destruction of the servlet. <br>
32 */
33 public void destroy() {
34 super.destroy(); // Just puts "destroy" string in log
35 // Put your code here
36 }
37
38 /**
39 * The doGet method of the servlet. <br>
40 *
41 * This method is called when a form has its tag value method equals to get.
42 *
43 * @param request the request send by the client to the server
44 * @param response the response send by the server to the client
45 * @throws ServletException if an error occurred
46 * @throws IOException if an error occurred
47 */
48 public void doGet(HttpServletRequest request, HttpServletResponse response)
49 throws ServletException, IOException {
50
51 response.setContentType("image/jpeg");
52
53 String randomString = getRandomString();
54 request.getSession(true).setAttribute("randomString",randomString);
55
56 int width = 100;
57 int height = 30;
58
59 Color color = getRandomColor();
60 Color reverse = getReverseColor(color);
61
62 BufferedImage bi = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
63
64 Graphics2D g = bi.createGraphics();
65 g.setFont(new Font(Font.SANS_SERIF,Font.BOLD,16));
66 g.setColor(color);
67 g.fillRect(0,0,width,height);
68 g.setColor(reverse);
69 g.drawString(randomString,18,20);
70
71 for(int i = 0,n = random.nextInt(100); i< n;i++)
72 {
73 g.drawRect(random.nextInt(width),random.nextInt(height),1,1);
74 }
75
76 ServletOutputStream out = response.getOutputStream();
77
78 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
79
80 encoder.encode(bi);
81
82 out.flush();
83
84 }
85
86 /**
87 * The doPost method of the servlet. <br>
88 *
89 * This method is called when a form has its tag value method equals to post.
90 *
91 * @param request the request send by the client to the server
92 * @param response the response send by the server to the client
93 * @throws ServletException if an error occurred
94 * @throws IOException if an error occurred
95 */
96 public void doPost(HttpServletRequest request, HttpServletResponse response)
97 throws ServletException, IOException {
98
99 }
100
101 /**
102 * Initialization of the servlet. <br>
103 *
104 * @throws ServletException if an error occurs
105 */
106 public void init() throws ServletException {
107 // Put your code here
108 }
109
110 public static final char[] CHARS = {'2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','J','K',
111 'L','M','N','P','Q','R','S','T','U','V','W','X','Y','Z'};
112
113 public static Random random = new Random();
114
115 public static String getRandomString()
116 {
117 StringBuffer buffer = new StringBuffer();
118 for(int i = 0;i < 6;i++)
119 {
120 buffer.append(CHARS[random.nextInt(CHARS.length)]);
121 }
122
123 return buffer.toString();
124 }
125
126 public static Color getRandomColor()
127 {
128 return new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255));
129 }
130
131 public static Color getReverseColor(Color c)
132 {
133 return new Color(255 - c.getRed(),255 - c.getGreen(),255-c.getBlue());
134 }
135
136 }
1 <script type="text/javascript">
2 function reloadImage()
3 {
4 document.getElementById('btn').disabled = true;
5 document.getElementById('identity').src='servlet/IndentityServlet?ts' + new Date().getTime();
6 }
7 </script>
8
9 <img src="servlet/IdentityServlet" id="identity" onload="btn.disabled = false;"
10
11 <input type="button" value="换张图片" onclick="reloadImage()" id="btn" />