我正在使用servlet生成验证码图像。我的代码可以正常使用谷歌浏览器,但验证码不会在FF / IE中重新加载。
代码是
public class 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";
});
});
请建议解决方案
答案 0 :(得分:0)
这可能是一个缓存问题,但也许以下方法也有效:
$(document).ready(function() {
$("#captchaRef").click(function() {
$('#captchaImg').attr('src', '').attr('src', '/captcha-image.jpg');
});
});
答案 1 :(得分:0)
正如亚历克斯所说,这可能是一个浏览器缓存问题
将此添加到页面标题
<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">