0
私のJSPページでcaptchaを生成するサーブレットがありますが、このサーブレットからnullがあり、無効なcaptchaエラーが発生することが多くありますが、私のキャプチャサーブレットサーブレットはCaptchaのヌルストリームを返します
以下public class CaptchaServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
int width = 200;
int height = 42;
char data[] = RandomStringUtils.randomNumeric(4).toLowerCase().toCharArray();
BufferedImage bufferedImage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = bufferedImage.createGraphics();
Font font = new Font("Georgia", Font.BOLD, 18);
g2d.setFont(font);
RenderingHints rh = new RenderingHints(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
rh.put(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHints(rh);
GradientPaint gp = new GradientPaint(0, 0,new Color(144, 46, 54), 0, height/2, new Color(144, 46, 54), true);
g2d.setPaint(gp);
g2d.fillRect(0, 0, width, height);
g2d.setColor(Color.white);
Random r = new Random();
String captcha = String.copyValueOf(data);
System.out.println("In captcha Servlet :- "+captcha);
request.getSession().setAttribute("captcha", captcha);
int x = 0;
int y = 0;
for (int i=0; i<data.length; i++) {
x += 35 + (Math.abs(r.nextInt()) % 5);
y = 20 + Math.abs(r.nextInt()) % 5;
g2d.drawChars(data, i, 1, x, y);
}
g2d.dispose();
response.setContentType("image/png");
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(bufferedImage, "png", os);
byte[] bytes = os.toByteArray();
ServletOutputStream out = response.getOutputStream();
out.write(bytes);
os.close();
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
}
は
String code = request.getParameter("code");
String captcha = (String) session.getAttribute("captcha");
if (captcha != null && code != null) {
if (captcha.equals(code)) {
isValid = true;
}
}
のように別のサーブレットのコードを入力し、私のJSPコードは、私がキャプチャコードとユーザーを確認する
<div id="captcha_div">
<img id="captcha_img" src="<%=appContext%>/CaptchaServlet"
style="float: left; width: 70%; margin-top: 2%;">
<div style="padding-top: 5%; margin-left: 73%">
<button class="refresh" onclick="javascript:regenImage();"type="button">
</button></div></div>
です
私はめったにヌルを取得しません。私が再現する状況は、ログインページが10,20分間に合っているようなものです。captchaの値はnullです。これにより、無効なcapchaエラーが発生します。または私がコードで間違っていたことありがとうございました...
ありがとうございます –