java生成验证码,可刷新

本文详细介绍了如何在Java中使用Struts框架实现验证码的生成与验证过程,包括使用相关标签库,生成随机字母数字字符串,并在页面上显示验证码图片。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

code.jsp

Html代码 收藏代码
  1. <%@pagelanguage="java"contentType="text/html;charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <!DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
  4. <%@tagliburi="http://struts.apache.org/tags-bean"prefix="bean"%>
  5. <%@tagliburi="http://struts.apache.org/tags-html"prefix="html"%>
  6. <%@tagliburi="http://struts.apache.org/tags-logic"prefix="logic"%>
  7. <%@tagliburi="http://struts.apache.org/tags-tiles"prefix="tiles"%>
  8. <html>
  9. <head>
  10. <http-equivhttp-equiv="Content-Type"content="text/html;charset=UTF-8">
  11. <title>验证码</title>
  12. </head>
  13. <body>
  14. <html:imgpage="/code.do"border="0"这里写单击事件onclick="this.src='/lookctrl/code.do'"alt="请输入此验证码,如看不清请点击刷新。"style="cursor:pointer"/>
  15. </body>
  16. </html>

CodeAction.java

Java代码 收藏代码
  1. packagestruts.action;
  2. importjava.awt.Color;
  3. importjava.awt.Font;
  4. importjava.awt.Graphics;
  5. importjava.awt.image.BufferedImage;
  6. importjava.util.Random;
  7. importjavax.imageio.ImageIO;
  8. importjavax.servlet.ServletOutputStream;
  9. importjavax.servlet.http.HttpServletRequest;
  10. importjavax.servlet.http.HttpServletResponse;
  11. importjavax.servlet.http.HttpSession;
  12. importorg.apache.struts.action.Action;
  13. importorg.apache.struts.action.ActionForm;
  14. importorg.apache.struts.action.ActionForward;
  15. importorg.apache.struts.action.ActionMapping;
  16. importorg.apache.commons.lang.RandomStringUtils;
  17. /**
  18. *MyEclipseStrutsCreationdate:08-2-2008
  19. *
  20. *XDocletdefinition:
  21. *
  22. *@struts.actionvalidate="true"
  23. */
  24. publicclassCodeActionextendsAction{
  25. /*
  26. *GeneratedMethods
  27. */
  28. /**
  29. *Methodexecute
  30. *
  31. *@parammapping
  32. *@paramform
  33. *@paramrequest
  34. *@paramresponse
  35. *@returnActionForward
  36. */
  37. publicActionForwardexecute(ActionMappingmapping,ActionFormform,
  38. HttpServletRequestrequest,HttpServletResponseresponse){
  39. try{
  40. intwidth=50;
  41. intheight=18;
  42. //取得一个4位随机字母数字字符串
  43. Strings=RandomStringUtils.random(4,true,true);//这个s的值就是页面验证码上显示的值
  44. //保存入session,用于与用户的输入进行比较.
  45. //注意比较完之后清除session.
  46. HttpSessionsession=request.getSession(true);
  47. session.setAttribute("validateCode",s);
  48. response.setContentType("images/jpeg");
  49. response.setHeader("Pragma","No-cache");
  50. response.setHeader("Cache-Control","no-cache");
  51. response.setDateHeader("Expires",0);
  52. ServletOutputStreamout=response.getOutputStream();
  53. BufferedImageimage=newBufferedImage(width,height,
  54. BufferedImage.TYPE_INT_RGB);
  55. Graphicsg=image.getGraphics();
  56. //设定背景色
  57. g.setColor(getRandColor(200,250));
  58. g.fillRect(0,0,width,height);
  59. //设定字体
  60. FontmFont=newFont("TimesNewRoman",Font.BOLD,18);//设置字体
  61. g.setFont(mFont);
  62. //画边框
  63. //g.setColor(Color.BLACK);
  64. //g.drawRect(0,0,width-1,height-1);
  65. //随机产生干扰线,使图象中的认证码不易被其它程序探测到
  66. g.setColor(getRandColor(160,200));
  67. //生成随机类
  68. Randomrandom=newRandom();
  69. for(inti=0;i<155;i++){
  70. intx2=random.nextInt(width);
  71. inty2=random.nextInt(height);
  72. intx3=random.nextInt(12);
  73. inty3=random.nextInt(12);
  74. g.drawLine(x2,y2,x2+x3,y2+y3);
  75. }
  76. //将认证码显示到图象中
  77. g.setColor(newColor(20+random.nextInt(110),20+random
  78. .nextInt(110),20+random.nextInt(110)));
  79. g.drawString(s,2,16);
  80. //图象生效
  81. g.dispose();
  82. //输出图象到页面
  83. ImageIO.write((BufferedImage)image,"JPEG",out);
  84. out.close();
  85. }catch(Exceptione){
  86. e.printStackTrace();
  87. }
  88. returnnull;
  89. }
  90. privateColorgetRandColor(intfc,intbc){//给定范围获得随机颜色
  91. Randomrandom=newRandom();
  92. if(fc>255)
  93. fc=255;
  94. if(bc>255)
  95. bc=255;
  96. intr=fc+random.nextInt(bc-fc);
  97. intg=fc+random.nextInt(bc-fc);
  98. intb=fc+random.nextInt(bc-fc);
  99. returnnewColor(r,g,b);
  100. }
  101. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值