java web 实现验证码
1 1.编写数字、英文随机生成的 server 类,源码:
2 package com;
3
4 import java.awt.Color;
5 import java.awt.Font;
6 import java.awt.Graphics;
7 import java.awt.image.BufferedImage;
8 import java.io.ByteArrayOutputStream;
9 import java.io.IOException;
10 import java.io.PrintWriter;
11
12 import javax.imageio.ImageIO;
13 import javax.servlet.ServletException;
14 import javax.servlet.ServletOutputStream;
15 import javax.servlet.http.HttpServlet;
16 import javax.servlet.http.HttpServletRequest;
17 import javax.servlet.http.HttpServletResponse;
18 import javax.servlet.http.HttpSession;
19
20 public class logcheck extends HttpServlet {
21
22 public logcheck() {
23 super();
24 }
25
26
27 public void destroy() {
28 super.destroy();
29 }
30
31 public void doGet(HttpServletRequest request, HttpServletResponse response)
32 throws ServletException, IOException {
33
34 doPost(request, response);
35 }
36
37
38 /*实现的核心代码*/
39 public void doPost(HttpServletRequest request, HttpServletResponse response)
40 throws ServletException, IOException {
41
42 response.setContentType("image/jpeg");
43 HttpSession session=request.getSession();
44 int width=60;
45 int height=20;
46
47 //设置浏览器不要缓存此图片
48 response.setHeader("Pragma", "No-cache");
49 response.setHeader("Cache-Control", "no-cache");
50 response.setDateHeader("Expires", 0);
51
52 //创建内存图像并获得图形上下文
53 BufferedImage image=new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
54 Graphics g=image.getGraphics();
55
56 /*
57 * 产生随机验证码
58 * 定义验证码的字符表
59 */
60 String chars="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
61 char[] rands=new char[4];
62 for(int i=0;i<4;i++){
63 int rand=(int) (Math.random() *36);
64 rands[i]=chars.charAt(rand);
65 }
66
67 /*
68 * 产生图像
69 * 画背景
70 */
71 g.setColor(new Color(0xDCDCDC));
72 g.fillRect(0, 0, width, height);
73
74 /*
75 * 随机产生120个干扰点
76 */
77
78 for(int i=0;i<120;i++){
79 int x=(int)(Math.random()*width);
80 int y=(int)(Math.random()*height);
81 int red=(int)(Math.random()*255);
82 int green=(int)(Math.random()*255);
83 int blue=(int)(Math.random()*255);
84 g.setColor(new Color(red,green,blue));
85 g.drawOval(x, y, 1, 0);
86 }
87 g.setColor(Color.BLACK);
88 g.setFont(new Font(null, Font.ITALIC|Font.BOLD,18));
89
90 //在不同高度输出验证码的不同字符
91 g.drawString(""+rands[0], 1, 17);
92 g.drawString(""+rands[1], 16, 15);
93 g.drawString(""+rands[2], 31, 18);
94 g.drawString(""+rands[3], 46, 16);
95 g.dispose();
96
97 //将图像传到客户端
98 ServletOutputStream sos=response.getOutputStream();
99 ByteArrayOutputStream baos=new ByteArrayOutputStream();
100 ImageIO.write(image, "JPEG", baos);
101 byte[] buffer=baos.toByteArray();
102 response.setContentLength(buffer.length);
103 sos.write(buffer);
104 baos.close();
105 sos.close();
106
107 session.setAttribute("checkcode", new String(rands));
108 }
109
110
111 public void init() throws ServletException {
112 // Put your code here
113 }
114
115 }
116
117 2.用于显示验证码的页面:
118 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
119 <%
120 String path = request.getContextPath();
121 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
122 %>
123
124 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
125 <html>
126 <head>
127 <base href="<%=basePath%>">
128
129 <title>index</title>
130 <meta http-equiv="pragma" content="no-cache">
131 <meta http-equiv="cache-control" content="no-cache">
132 <meta http-equiv="expires" content="0">
133 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
134 <meta http-equiv="description" content="This is my page">
135 <!--
136 <link rel="stylesheet" type="text/css" href="styles.css">
137 -->
138 </head>
139
140 <body>
141 <form action="yanzheng" method="post">
142 <input type="text" name="name" size="5" maxlength="4">
143 <a href="index.jsp"><img border="0" src="logcheck"></a><br><br>
144      <input type="submit" value="提交">
145 </form>
146 </body>
147 </html>
148 3.用于检验输入的验证码是否正确:
149 package com;
150
151 import java.io.IOException;
152 import java.io.PrintWriter;
153
154 import javax.jms.Session;
155 import javax.servlet.ServletException;
156 import javax.servlet.http.HttpServlet;
157 import javax.servlet.http.HttpServletRequest;
158 import javax.servlet.http.HttpServletResponse;
159 import javax.servlet.http.HttpSession;
160
161 public class yanzheng extends HttpServlet {
162
163 public yanzheng() {
164 super();
165 }
166
167 public void destroy() {
168 super.destroy();
169 }
170
171 public void doGet(HttpServletRequest request, HttpServletResponse response)
172 throws ServletException, IOException {
173
174 doPost(request, response);
175 }
176 /*核心代码*/
177 public void doPost(HttpServletRequest request, HttpServletResponse response)
178 throws ServletException, IOException {
179
180 String info=null;
181 /*获取输入的值*/
182 String value1=request.getParameter("name");
183
184 /*获取图片的值*/
185 HttpSession session=request.getSession();
186 String value2=(String)session.getAttribute("checkcode");
187
188 /*对比两个值(字母不区分大小写)*/
189 if(value2.equalsIgnoreCase(value1)){
190 info="验证码输入正确";
191 }else{
192 info="验证码输入错误";
193 }
194 System.out.println(info);
195 request.setAttribute("info", info);
196 request.getRequestDispatcher("/login.jsp").forward(request, response);
197 }
198
199
200 public void init() throws ServletException {
201 // Put your code here
202 }
203
204 }
205 4.显示输入结构界面(输入验证码是否正确):
206 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
207 <%
208 String path = request.getContextPath();
209 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
210 %>
211
212 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
213 <html>
214 <head>
215 <base href="<%=basePath%>">
216
217 <title>My JSP 'login.jsp' starting page</title>
218
219 <meta http-equiv="pragma" content="no-cache">
220 <meta http-equiv="cache-control" content="no-cache">
221 <meta http-equiv="expires" content="0">
222 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
223 <meta http-equiv="description" content="This is my page">
224 <!--
225 <link rel="stylesheet" type="text/css" href="styles.css">
226 -->
227
228 </head>
229
230 <body>
231 <%=request.getAttribute("info") %>
232 </body>
233 </html>
234 5.项目结构、效果截图:

JAVA修炼塔,技术世界的探知与交流,欢迎你的加入-----群号:535296702

浙公网安备 33010602011771号