


下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、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.se
2、rvlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; /* * Package name : com.skcc.sample.action * File name : VerifyCodeServlet.java * Author : Administrator * Date : 2008-9-4 * Description : VerifyCodeServlet */ public class VerifyCode
3、Servlet extends HttpServlet /* * 验证码图片的宽度。 */ private int width = 60; /* * 验证码图片的高度。 */ private int height = 20; /* * 验证码字符个数 */ private int codeCount = 4; /* * xx */ private int xx = 0; /* * 字体高度 */ private int fontHeight; /* * codeY */ private int codeY; /* * codeSequence */ char codeSequence =
4、39;A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
5、9;X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' ; /* * 初始化验证图片属性 */ public void init( throws ServletException / 从web.xml中获取初始信息 / 宽度 String strWidth = this.getInitParameter("widt
6、h" / 高度 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.leng
7、th( != 0 height = Integer.parseInt(strHeight; if (strCodeCount != null && strCodeCount.length( != 0 codeCount = Integer.parseInt(strCodeCount; catch (NumberFormatException e e.printStackTrace(; xx = width / (codeCount + 1; fontHeight = height - 2; codeY = height - 4; /* * param req * param r
8、esp * throws ServletException * throws java.io.IOException */ protected void service(HttpServletRequest req, HttpServletResponse resp throws ServletException, java.io.IOException / 定义图像buffer BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB; Graphics2D gd = buffImg
9、.createGraphics(; / 创建一个随机数生成器类 Random random = new Random(; / 将图像填充为白色 gd.setColor(Color.WHITE; gd.fillRect(0, 0, width, height; / 创建字体,字体的大小应该根据图片的高度来定。 Font font = new Font("Fixedsys", Font.PLAIN, fontHeight; / 设置字体。 gd.setFont(font; / 画边框。 gd.setColor(Color.BLACK; gd.drawRect(0, 0, wid
10、th - 1, height - 1; / 随机产生160条干扰线,使图象中的认证码不易被其它程序探测到。 gd.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; gd.drawLine(x, y, x + xl, y + yl; / randomCode用于保存随机产生的验证码,以便用户登录后进行验证。 S
11、tringBuffer 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.nextI
12、nt(255; blue = random.nextInt(255; / 用随机产生的颜色将验证码绘制到图像中。 gd.setColor(new Color(red, green, blue; gd.drawString(strRand, (i + 1 * xx, codeY; / 将产生的四个随机数组合在一起。 randomCode.append(strRand; / 将四位数字的验证码保存到Session中。 HttpSession session = req.getSession(; session.setAttribute("validateCode", rando
13、mCode.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(
14、; ImageIO.write(buffImg, "jpeg", sos; sos.close(; 产生结果servlet import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.H ttpServletRequest; import javax.servlet.http.HttpServletResponse;
15、/* * Package name : com.skcc.sample.action * File name : ResultServlet.java * Author : Administrator * Date : 2008-9-4 * Description : ResultServlet */ public class ResultServlet extends HttpServlet /* * The doGet method of the servlet. * * This method is called when a form has its tag value method
16、equals to get. * * param request the request send by the client to the server * param response the response send by the server to the client * throws ServletException if an error occurred * throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse r
17、esponse throws ServletException, IOException doPost(request, response; /* * The doPost method of the servlet. * * This method is called when a form has its tag value method equals to post. * * param request the request send by the client to the server * param response the response send by the server
18、 to the client * throws ServletException if an error occurred * throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response throws ServletException, IOException response.setContentType("text/html;charset=utf-8" String validateC = (String request.getSession(.getAttribute("validateCode" String veryCode = request.getParameter("c" PrintWriter out = response.getWrite
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 企业智能化管理系统的构建与实践
- 智慧城市的社会治理与创新
- 家庭教育中心理激励方法探讨
- 提升在线教学质量实操与策略研究
- 19-木皮蓝变?有没有办法提前预防富深一个解决方案
- 全球视野下的葡萄酒产区特色:2025年品牌国际化路径研究报告
- 公交优先发展战略背景下2025年城市交通拥堵治理路径探索报告
- Chitosan-Cy7-5-MW-100000-生命科学试剂-MCE
- 北京市昌平二中学南校区2024年化学九年级第一学期期末质量跟踪监视试题含解析
- 2025届贵州黔西南州望谟三中学七上数学期末质量检测试题含解析
- 培训物业客服部礼仪礼节
- 北京海淀区一零一中学2025年八年级英语第二学期期末复习检测模拟试题含答案
- 2025年广东省高考生物试题(含答案解析)
- 院感知识手卫生培训内容
- 2025年 广州市能源融资租赁有限公司招聘考试笔试试题附答案
- 章程规范业务管理制度
- QGDW11914-2018电力监控系统网络安全监测装置技术规范
- 新生儿洗澡及皮肤护理
- 保鲜库建设项目可行性研究报告(可编辑)
- 餐饮废弃物管理制度
- 2025-2030年中国天然气发电机行业市场现状供需分析及投资评估规划分析研究报告
评论
0/150
提交评论