struts1 登陆验证码实现
2013-03-21 09:10
344 查看
这两天在由于项目需要做了个验证码登陆,在网上找了找,思路大志是在后台生成验证码图片,以图片流的形式传到前台,仅供参考~~
后台生成验证码图片:
前台使用html img标签接收图片~~
后台生成验证码图片:
response.setHeader("Pragma","No-cache"); response.setHeader("Cache-Control","no-cache"); response.setDateHeader("Expires", 0); int width=60, height=20; BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); OutputStream os=response.getOutputStream(); Graphics g = image.getGraphics(); Random random = new Random(); g.setColor(getRandColor(200,250)); g.fillRect(0, 0, width, height); g.setColor(new Color(255, 255, 255)); for (int i=0;i<155;i++) { int x = random.nextInt(width); int y = random.nextInt(height); int xl = random.nextInt(40); int yl = random.nextInt(40); g.drawLine(x,y,x+xl,y+yl); } String sRand=""; for (int i=0;i<4;i++){ String rand=String.valueOf(random.nextInt(10)); sRand+=rand; g.setColor(new Color(0, 0, 0)); g.drawString(rand,14 * i,18); } //赋值验证码 CHECK_IMAGE = sRand; g.dispose(); ImageIO.write(image, "jpeg",os); request.setAttribute("image", image); response.flushBuffer(); os.flush(); os.close(); os=null; return null; }catch(IllegalStateException e){ System.out.println(e.getMessage()); e.printStackTrace(); }
前台使用html img标签接收图片~~
<img src="index.do?command=toLoginpicture" id="Verify" name="Verify" style="cursor:hand;" alt="看不清,换一张" onclick="changePic()"/><span style="color:red">${checkimage}</span>
相关文章推荐
- Spring3整合Struts2+Hibernate 实现注册、数据库验证登陆
- [转载]C# winform登陆框验证码的实现
- 一步一步实现web程序信息管理系统之三----登陆业务逻辑实现(验证码功能+参数获取)
- Struts拦截器实现拦截未登陆用户实例解析
- Java Web开发过程中登陆模块的验证码的实现方式总结
- PHP实现登陆表单提交CSRF及验证码
- struts2+Hibernate实现用户登陆功能
- struts入门:struts实现用户登陆
- python实现带验证码网站的自动登陆实现代码
- 简单的登陆注册的实现+验证码和表单验证
- 实现网页随机登陆验证码功能
- HttpClient 抓包实现验证码登陆,网站是教务系统
- struts2+Hibernate实现用户登陆功能
- 在struts中实现验证码
- 登陆验证码-----实现
- 用PHP实现登陆验证码(类似条行码状)
- Struts2 实现验证码 登陆
- 使用PHP实现登陆验证码
- PHP实现登陆验证码
- Struts2连接数据库实现登陆(验证码)、注册