JSP Servlet實現驗證碼

來源:互聯網
上載者:User
  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" />

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.