关于用户登录时,要填写验证码!可是这个验证码的代码是如何写的呢?
请各位高手指点指点!先谢了!~

解决方案 »

  1.   


    参考我们编写的jsp教程:http://family168.com/tutorial/jsp/html/jsp-ch-12.html#jsp-ch-12-01
      

  2.   

    写一个生产验证码的aciton或者servlet
    随机数—画随机数到图片--存储字符串到session通过比对客户输入的字符串和存在session中的字符串换一张采用ajax即可
      

  3.   

    <tr>
    <td valign="top" class="w1">
    验证码:
    </td>
    <td>
    <img class="yzm_img" id='imgVcode' src="createImage.do"/>
    <input name="txtVerifyCode" type="text" id="txtVerifyCode"
    class="yzm_input" onblur="" />
    <div class="text_left t1">
    <p class="t1">
    <span id="vcodeValidMsg">请输入图片中的四个字母。</span><a href="#" onclick="javascript:$('imgVcode').src='createImage.do?'+new Date().getTime()">看不清楚?换个图片</a>
    </p>
    </div>
    </td>
    </tr>
      

  4.   

    public ActionForward execute(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response)
    throws Exception {

    // 设置响应类型
    //response.setContentType("text/html");
    response.setContentType("image/jpeg");
    // 图片的内存映像 点阵信息
    BufferedImage image = new BufferedImage(80, 20,
    BufferedImage.TYPE_INT_RGB);

    Random r = new Random();
    //  //获得画笔对象
    Graphics g = image.getGraphics();
    //setColor设置当前画笔的颜色
    g.setColor(new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255)));

    g.fillRect(0, 0, 80, 20);
    g.setColor(new Color(255,255,255));
    g.setColor(new Color(0, 0, 0));
    String number = String.valueOf(r.nextInt(99999));

    HttpSession session = request.getSession();
    session.setAttribute("txtVerifyCode", number);

    g.drawString(number, 15, 15); // 压缩成jpeg格式
    OutputStream os = response.getOutputStream();
    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os); // 把BufferedImage对象中的图像信息编码后
    // 向创建该对象(encoder)时指定的输出流输出
    encoder.encode(image);
    return null;
    }