0

我正在使用 servlet 生成验证码图像。我的代码在谷歌浏览器上运行良好,但验证码没有在 FF/IE 中重新加载。

代码是

公共类 CaptchaServlet 扩展 HttpServlet {

 protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {


response.setContentType("image/jpg");
int iTotalChars = 6;
int iHeight = 40;
int iWidth = 150;
Font fntStyle1 = new Font("Arial", Font.BOLD, 30);
//Font fntStyle2 = new Font("Verdana", Font.BOLD, 20);
Random randChars = new Random();
String sImageCode = (Long.toString(Math.abs(randChars.nextLong()), 36)).substring(0, iTotalChars);
BufferedImage biImage = new BufferedImage(iWidth, iHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D g2dImage = (Graphics2D) biImage.getGraphics();
int iCircle = 15;
for (int i = 0; i < iCircle; i++) {
    g2dImage.setColor(new Color(randChars.nextInt(255), randChars.nextInt(255), randChars.nextInt(255)));
    int iRadius = (int) (Math.random() * iHeight / 2.0);
    int iX = (int) (Math.random() * iWidth - iRadius);
    int iY = (int) (Math.random() * iHeight - iRadius);
}
g2dImage.setFont(fntStyle1);
for (int i = 0; i < iTotalChars; i++) {
    g2dImage.setColor(new Color(randChars.nextInt(255), randChars.nextInt(255), randChars.nextInt(255)));
    if (i % 2 == 0) {
        g2dImage.drawString(sImageCode.substring(i, i + 1), 25 * i, 24);
    } else {
        g2dImage.drawString(sImageCode.substring(i, i + 1), 25 * i, 35);
    }
}

OutputStream osImage = response.getOutputStream();
ImageIO.write(biImage, "jpeg", osImage);

g2dImage.dispose();

HttpSession session = request.getSession();
session.setAttribute("dns_security_code", sImageCode);


}

protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    processRequest(request, response);
}


}

web.xml 代码

 <servlet>
    <servlet-name>CaptchaServlet</servlet-name>
    <servlet-class>com.rush49.web.controllers.CaptchaServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>CaptchaServlet</servlet-name>
    <url-pattern>/captcha-image.jpg</url-pattern>
</servlet-mapping>

jsp代码

<div class="captcha" align="center">
 <table>

<tr>
     <td>
  <span><img alt="" id="captchaImg" src="/captcha-image.jpg" width="150"/></span>
       <span><a id="captchaRef">
   <img id="refresh" src="../images/refresh.png" width="50" height="50"></img>
       </a></span>
    </td>

</tr>
<tr>
<td>Enter the above code</td>
</tr>
<tr>
 <td><input id="captchaText" name="captchaText" class="register_form_input_box" type="text" value=""/></td>  
     </tr>
</table>

jquery 重新加载验证码。它在 chrome 中运行良好,但在 FF/IE 中运行良好

 $(document).ready(function() {
    $("#captchaRef").click(function() {
       document.getElementById('captchaImg').src="/captcha-image.jpg";
     });
 });

请提出解决方案

4

2 回答 2

0

这可能是一个缓存问题,但也许以下方法也有效:

$(document).ready(function() {
    $("#captchaRef").click(function() {
       $('#captchaImg').attr('src', '').attr('src', '/captcha-image.jpg');
     });
 });
于 2013-02-26T16:37:40.653 回答
0

正如亚历克斯所说,这可能是浏览器缓存问题

将此添加到您的页面标题标签

<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
于 2013-02-27T09:04:14.253 回答