Captcha validation using JavaScript in JSP -
i have captcha validation code , set captcha string session , in validating script function null.
i'm using javascript inside jsp page.
<script> function validatecaptcha(){ var v='<%=session.getattribute("captcha")%>' alert (v); } </script> <form method="post" onsubmit="validatecaptcha()"> <input type="text" name="code"> <img src="<c:url value="captcha.jsp"></c:url>"> <input type="submit" value="submit"> </form> </body> </html>
i have captcha.jsp generate captcha cannot session value in javascript.
<%int width = 150; int height = 50; char data[][] = { { '4', 'e', 'g', 'v', 'o', 'f', 'x' }, { 'd', 't', 't', '5', 'o', '7', 'x' }, { '5', 'e', '7', 'c', '6', 'p', 't' }, { 'l', 'j', '5', 't', 'x','6' ,'g'}, { '9', 'f', '5', 't', 'x','e' ,'7'}, { 'f', '5', 't', 'b', 'b', 's', 'd' }, { 'u', 'b', 'u', 'n', 't', 'u' }, { 'j', 'e', 'e','a','y','1','2','5','6', } }; bufferedimage bufferedimage = new bufferedimage(width, height, bufferedimage.type_int_rgb); graphics2d g2d = bufferedimage.creategraphics(); font font = new font("arial", 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, color.red, 0, height/2, color.black, true); g2d.setpaint(gp); g2d.fillrect(0, 0, width, height); g2d.setcolor(new color(255, 153, 0)); random r = new random(); int index = math.abs(r.nextint()) % 5; string captcha = string.copyvalueof(data[index]); request.getsession().setattribute("captcha", captcha ); int x = 0; int y = 0; (int i=0; i<data[index].length; i++) { x += 10 + (math.abs(r.nextint()) % 15); y = 20 + math.abs(r.nextint()) % 20; g2d.drawchars(data[index], i, 1, x, y); } g2d.dispose(); response.setcontenttype("image/png"); outputstream os = response.getoutputstream(); imageio.write(bufferedimage, "png", os); os.close(); %>
Comments
Post a Comment