Jquery 刷新验证码图像,这是来自servlet的响应

Jquery 刷新验证码图像,这是来自servlet的响应,jquery,jsp,servlets,Jquery,Jsp,Servlets,我正在使用servlet生成验证码图像。我的代码在google chrome上运行良好,但在FF/IE中不会重新加载验证码 代码是 公共类CaptchaServlet扩展了HttpServlet{ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setConte

我正在使用servlet生成验证码图像。我的代码在google chrome上运行良好,但在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);
}


}

请提出解决方案

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

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

正如Alex所说,这可能是一个浏览器缓存问题

将此添加到页眉标记

<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">


也许您需要允许在IE中执行脚本。很抱歉,我不了解这一点,我也有同样的问题
 $(document).ready(function() {
    $("#captchaRef").click(function() {
       document.getElementById('captchaImg').src="/captcha-image.jpg";
     });
 });
$(document).ready(function() {
    $("#captchaRef").click(function() {
       $('#captchaImg').attr('src', '').attr('src', '/captcha-image.jpg');
     });
 });
<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">