サーブレットを使用してキャプチャ イメージを生成しています。私のコードは Google Chrome で正常に動作しますが、キャプチャが FF/IE でリロードされません。
コードは
public class CaptchaServlet extends 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 でキャプチャをリロードします。クロムでは正常に動作しますが、FF/IE では動作しません
$(document).ready(function() {
$("#captchaRef").click(function() {
document.getElementById('captchaImg').src="/captcha-image.jpg";
});
});
解決策を提案してください