




已阅读5页,还剩2页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
实现的思路:首先,需要创建一个Servlet。该Servlet通过字节型响应给客户端返回一个图片,该图片是通过JDK中Java 2D的类库来生成一个图片。图片的生成是依靠一个随机数来完成,然后将这个随机数写成图片格式。最后在Session将这个随机的字符串的状态保持住,以便在用户填写后进行对比。其次,在需要加入验证码的JSP页面中,通过引入该图片。最后,单用户填写完验证码后,提交到某一个Servlet中。在这个Servlet中,通过request.getParameter()方法获取用户添加的验证码,然后取出后与Session中生成的验证码进行对比,如果对比成功就表示通过,否则返回该页面给用户提示验证码错误的信息。参考代码:1、 用来生成验证码图片的Servlet:package mon;import java.awt.Color;import java.awt.Font;import java.awt.Graphics2D;import java.awt.image.BufferedImage;import java.util.Random;import javax.imageio.ImageIO;import javax.servlet.ServletException;import javax.servlet.ServletOutputStream;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;public class ValidateCodeServlet extends HttpServlet / 验证码图片的宽度。private int width = 60;/ 验证码图片的高度。private int height = 20;/ 验证码字符个数private int codeCount = 4;private int x = 0;/ 字体高度private int fontHeight;private int codeY;char codeSequence = A, B, C, D, E, F, G, H, I, J,K, L, M, N, O, P, Q, R, S, T, U, V, W,X, Y, Z, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ;/* * 初始化验证图片属性 */public void init() throws ServletException / 从web.xml中获取初始信息/ 宽度String strWidth = this.getInitParameter(width);/ 高度String strHeight = this.getInitParameter(height);/ 字符个数String strCodeCount = this.getInitParameter(codeCount);/ 将配置的信息转换成数值try if (strWidth != null & strWidth.length() != 0) width = Integer.parseInt(strWidth);if (strHeight != null & strHeight.length() != 0) height = Integer.parseInt(strHeight);if (strCodeCount != null & strCodeCount.length() != 0) codeCount = Integer.parseInt(strCodeCount); catch (NumberFormatException e) x = width / (codeCount + 1);fontHeight = height - 2;codeY = height - 4;protected void service(HttpServletRequest req, HttpServletResponse resp)throws ServletException, java.io.IOException / 定义图像bufferBufferedImage buffImg = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);Graphics2D g = buffImg.createGraphics();/ 创建一个随机数生成器类Random random = new Random();/ 将图像填充为白色g.setColor(Color.WHITE);g.fillRect(0, 0, width, height);/ 创建字体,字体的大小应该根据图片的高度来定。Font font = new Font(Fixedsys, Font.PLAIN, fontHeight);/ 设置字体。g.setFont(font);/ 画边框。g.setColor(Color.BLACK);g.drawRect(0, 0, width - 1, height - 1);/ 随机产生160条干扰线,使图象中的认证码不易被其它程序探测到。g.setColor(Color.BLACK);for (int i = 0; i 160; i+) int x = random.nextInt(width);int y = random.nextInt(height);int xl = random.nextInt(12);int yl = random.nextInt(12);g.drawLine(x, y, x + xl, y + yl);/ randomCode用于保存随机产生的验证码,以便用户登录后进行验证。StringBuffer randomCode = new StringBuffer();int red = 0, green = 0, blue = 0;/ 随机产生codeCount数字的验证码。for (int i = 0; i codeCount; i+) / 得到随机产生的验证码数字。String strRand = String.valueOf(codeSequencerandom.nextInt(36);/ 产生随机的颜色分量来构造颜色值,这样输出的每位数字的颜色值都将不同。red = random.nextInt(255);green = random.nextInt(255);blue = random.nextInt(255);/ 用随机产生的颜色将验证码绘制到图像中。g.setColor(new Color(red, green, blue);g.drawString(strRand, (i + 1) * x, codeY);/ 将产生的四个随机数组合在一起。randomCode.append(strRand);/ 将四位数字的验证码保存到Session中。HttpSession session = req.getSession();session.setAttribute(Constants.VALIDATE_CODE_KEY, randomCode.toString();/ 禁止图像缓存。resp.setHeader(Pragma, no-cache);resp.setHeader(Cache-Control, no-cache);resp.setDateHeader(Expires, 0);resp.setContentType(image/jpeg);/ 将图像输出到Servlet输出流中。ServletOutputStream sos = resp.getOutputStream();ImageIO.write(buffImg, jpeg, sos);sos.close();2、 使用动态图片验证码的JSP页面:base href=My JSP index.jsp starting page!-0)%User:Pwd:Code:3、 用来处理用户提交的Servlet:package com.bluedot.web.servlet;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import mon.Constants;public class Login extends HttpServlet public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException request.setCharacterEncoding(utf-8);String userName = request.getParameter(userName);String userPwd = request.getParameter(userPWD);String validateCode = request.getParameter(validateCode);String validateCodeInSession = (String) request.getSession().getAttribute(Constants.VALIDATE_CODE_KEY);if (validateCodeInSession = null | validateCode = null| !validateCode.equalsIgnoreCase(validateCodeInSession) request.setAttribute(msg, 验证码错误);request.getSession().removeAttribute(Constants.VALIDATE_CODE_KEY);request.getRequestDispatcher(index.jsp).forward(request, response); else / 调用后台业务层的方法进行用户名和密码的校验,在这里直接进行假校验!if (admin.equals(userName) & admin.equals(userPwd) request.getSession().removeAttribute(Constants.VALIDATE_CODE_KEY);response.sendRedirect(ok.jsp); else request.getSession().removeAttribute(Constants.VALIDATE_CO
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025江西吉安市青原区司法局招聘2人考试备考试题及答案解析
- 2025甘肃中铁西北科学研究院有限公司评估中心招聘考试备考试题及答案解析
- 2025年康复医疗服务体系与康复医疗设备产业协同发展报告
- 2025年数控机床智能化升级技术路径与产业变革报告
- 2025年消费金融用户画像精准营销策略与行业动态报告
- 合肥市二手房市场价格的多维度解析与影响因素探究
- 合成孔径雷达快速时域成像算法:原理、优化与应用探索
- 合作学习:开启高中生物教学的新征程
- 2025年足彩胜负彩过滤服务协议书范本3篇
- 教师招聘之《幼儿教师招聘》能力检测试卷附答案详解(黄金题型)
- Unit2-The-fun-they-had市公开课一等奖省赛课微课金奖课件
- 2024年河北省石家庄市轨道交通有限责任公司招聘笔试参考题库含答案解析
- 110kV变电站及110kV输电线路运维投标技术方案(第一部分)
- 项目时间安排
- 智慧生态茶园技术方案
- 常用食物含铜量表-献给有需要的人
- 健身教练增肌减脂知识讲座
- 《踝关节康复训练》课件
- 进修汇报材料课件
- (初级)游泳救生员理论考试题库(新版)
- 名校教学设计:综合与实践-哪个城市夏天更热2
评论
0/150
提交评论