下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、本例是用一个servlet做一个验证码1validatecode.Java(用于生成验证码)package control;import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.image.BufferedImage;import java.io.IOException;import java.util.Random;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import
2、javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.sun.image.codec.jpeg.JPEGCodec;import com.sun.image.codec.jpeg.JPEGImageEncoder;public class validatecode extends HttpServlet public Color getRandColor(int fc, int bc) /给定范围获得随机颜色 Random random = new Rando
3、m(); if (fc > 255) fc = 255; if (bc > 255) bc = 255; int r = fc + random.nextInt(bc - fc); int g = fc + random.nextInt(bc - fc); int b = fc + random.nextInt(bc - fc); return new Color(r, g, b); public void init() throws ServletException public void doPost(HttpServletRequest request, HttpServle
4、tResponse response)throws ServletException, IOException response.setHeader("Pragma", "No-cache"); response.setHeader("Cache-Control", "no-cache"); response.setDateHeader("Expires", 0); / 在内存中创建图象 int width = 60, height = 20; BufferedImage image = new
5、 BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);/对访问的图片色调为RGB格式 / 获取图形上下文 Graphics g = image.getGraphics(); /生成随机类 Random random = new Random(); / 设定背景色 g.setColor(getRandColor(200, 250); g.fillRect(0, 0, width, height); /填满 /设定字体 g.setFont(new Font("Times New Roman", Font.PLAIN,
6、 18); /画边框 /g.setColor(new Color(); /g.drawRect(0,0,width-1,height-1); / 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到 g.setColor(getRandColor(160, 200); for (int i = 0; i < 155; i+) int x = random.nextInt(width); int y = random.nextInt(height); int xl = random.nextInt(12); int yl = random.nextInt(12); g.drawLi
7、ne(x, y, x + xl, y + yl); / 取随机产生的认证码(4位数字) String sRand = "" for (int i = 0; i < 4; i+) String rand = String.valueOf(random.nextInt(10); sRand += rand; / 将认证码显示到图象中 g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110); /调用函数出来的颜色相同,可能是因为种子太接
8、近,所以只能直接生成 g.drawString(rand, 13 * i + 6, 16); / 将认证码存入session request.getSession().setAttribute("rand", sRand); / 图象生效 g.dispose(); / 输出图象到页面 /ImageIO.write(image, "JPEG", response.getOutputStream(); JPEGImageEncoder encode = JPEGCodec.createJPEGEncoder(response. getOutputStream
9、(); encode.encode(image); /Clean up resources public void destroy() protected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException this.doPost(req, resp);2servlet配置web.xml<servlet> <description>This is the description of my J2EE component</d
10、escription> <display-name>This is the display name of my J2EE component</display-name> <servlet-name>validatecode</servlet-name> <servlet-class>control.validatecode</servlet-class> </servlet><servlet-mapping> <servlet-name>validatecode</servlet-name> <url-pa
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 培训机构复工制度
- 人力情报培训制度
- 制定幼儿园培训制度
- 大唐培训需求制度
- 刑侦教育培训制度
- 办公室安全教育培训制度
- 校外培训材料编写制度
- 储备库人员培训考核制度
- 单位培训股管理制度
- 学校教师培训制度
- 宅基地兄弟赠与协议书
- 影视文学剧本分析其文体特征
- (正式版)JTT 1218.6-2024 城市轨道交通运营设备维修与更新技术规范 第6部分:站台门
- 2023年美国专利法中文
- 内科质控会议管理制度
- 电气防火防爆培训课件
- 彝族文化和幼儿园课程结合的研究获奖科研报告
- 空调安装免责协议
- 湖北省襄樊市樊城区2023-2024学年数学四年级第一学期期末质量检测试题含答案
- 新北师大版八年级数学下册导学案(全册)
- cimatron紫藤教程系列gpp2运行逻辑及block说明
评论
0/150
提交评论