基于SSH2框架下的 用户注册 验证码功能

jsp页面:

             <tr>
                <th width="320" >验证码:</th>
                 <td width="741" >
                  <s:textfield key="registerInfoBean.checkNumber" cssClass="inputtext" data-rule-required="true" />
                 <img id="chkNumber" src="registerChkNumber.action" />
                </td>
              </tr>


js:

   //验证码
    $("#chkNumber").click(function(){
        var timenow = new Date().getTime();  
        $(this).attr("src", "registerChkNumber.action?d="+timenow);
        
        
    });

struts.xml文件配置;

        <action name="registerChkNumber" class="registerAction" method="getChkNumber">
           <result type="stream">  
                <param name="contentType">image/jpeg</param>  
                <param name="inputtName">inputStream</param>  
            </result>

      </action>


Action处理:

  

   //输出流
      private ByteArrayInputStream inputStream;
        /**
          * 验证码获取
          *
          */      
      public String getChkNumber(){          
            this.setInputStream(this.registerService.getChkNumber());
            return SUCCESS;  
         }




Service实现类方法:

    /**
     * 获取验证码
     */
    public ByteArrayInputStream  getChkNumber(){
         int width = 85, height = 25;  
            BufferedImage image = new  BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);  
            Graphics g = image.getGraphics();  
            Random random = new Random();  
            g.setColor(getRandColor(200, 255));  
            g.fillRect(0, 0, width, height);  
              
            g.setFont(new Font("Times New Roman", Font.PLAIN, 18));  
            g.setColor(getRandColor(160, 200));  
            for(int i = 0; i < 155; i++)  
            {  
                int x1 = random.nextInt(width);  
                int y1 = random.nextInt(height);  
                int x2 = random.nextInt(12);  
                int y2 = random.nextInt(12);  
                g.drawLine(x1, y1, x2, y2);  
            }  
              
            String sRand = "";  
            for(int i = 0; i < 6; i++)  
            {  
                String rand = getRandomChar();  
                sRand += rand;  
                g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));  
                g.drawString(rand, 13*i+6, 16);  
            }
            //存进session,用于验证  
            ActionContext.getContext().getSession().put("randomImg", sRand.toLowerCase());
            g.dispose();  
            ByteArrayOutputStream output = new ByteArrayOutputStream();  
            ImageOutputStream imageOut;
            try {
                imageOut = ImageIO.createImageOutputStream(output);
                ImageIO.write(image, "JPEG", imageOut);  
                imageOut.close();
            } catch (IOException e) {
                e.printStackTrace();
            }  
            ByteArrayInputStream input = new ByteArrayInputStream(output.toByteArray());
            return input;
      }
    
    //颜色
    public Color getRandColor(int fc, int bc){
        Random random = new Random();  
        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 String getRandomChar(){
        int rand = (int)Math.round(Math.random() * 2);  
        long itmp = 0;  
        char ctmp = '\u0000';  
        switch (rand)  
        {  
             case 1:  
                  itmp = Math.round(Math.random() * 25 + 65);  
                  ctmp = (char)itmp;  
                  return String.valueOf(ctmp);  
             case 2:  
                  itmp = Math.round(Math.random() * 25 + 97);  
                  ctmp = (char)itmp;  
                  return String.valueOf(ctmp);  
             default :  
                  itmp = Math.round(Math.random() * 9);  
                
        }  
        return String.valueOf(itmp);  
    }




验证提交验证码是否正确

        //判断验证码是否正确
        if(registerInfo.getCheckNumber().toLowerCase().equals(
                ActionContext.getContext().getSession().get("randomImg"))){

             //   正确后的业务处理代码

}





  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值