国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > 互联网 > JSP验证码

JSP验证码

来源:程序员人生   发布时间:2014-12-07 10:24:37 阅读次数:3370次

1、login.jsp登陆界面

<%@ page language="java" contentType="text/html; charset=ISO⑻859⑴" pageEncoding="ISO⑻859⑴"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO⑻859⑴"> <title>Login Page</title> <script language="javascript"> function loadImage() { document.getElementById("randImage").src="image.jsp?"+Math.random(); } </script> </head> <body> <form action="validate.jsp" method="post"> <table cellspacing="1" cellpadding="3" border="0"> <tr> <td colspan="2">Please enter your verification code</td> </tr> <tr> <td><input type="text" name="vcode"/></td> <td><img src="image.jsp" id="randImage"/></td> </tr> <tr> <td colspan="2"><a href="javascript:loadImage()">Change an image</a></td> </tr> <tr> <td colspan="2"><input type="submit" value="Submit"/></td> </tr> </table> </form> </body> </html>

2、image.jsp生成验证码图片的jsp文件

<%@ page language="java"%> <%@ page import="java.awt.*,java.awt.image.*,java.util.*" %> <%@ page import="java.io.OutputStream,javax.imageio.*"%> <%! Color getRandColor(int fc,int bc){ if(fc>255){ fc=255; } if(bc>255){ bc=255; } Random random=new Random(); int r=fc+random.nextInt(bc-fc); int g=fc+random.nextInt(bc-fc); int b=fc+random.nextInt(bc-fc); return new Color(r,g,b); } %> <% //本地无缓存,每次自动刷新 response.addHeader("pragma", "No-cashe"); response.addHeader("cashe-control","no-cashe"); response.setDateHeader("expires",0); int width=60,height=20; BufferedImage bimg=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB); Graphics g=bimg.getGraphics(); g.setColor(getRandColor(200,250)); g.fillRect(0, 0, width, height); g.setColor(getRandColor(160,200)); Random rand=new Random(); for(int i=0;i<200;i++){ int x=rand.nextInt(width); int y=rand.nextInt(height); int w=rand.nextInt(12); int h=rand.nextInt(12); g.drawLine(x, y, x+w, y+h); } String srand=""; g.setFont(new Font("Times New Roman",Font.PLAIN,18)); for(int i=0;i<4;i++){ String num=String.valueOf(rand.nextInt(10)); srand+=num; g.setColor(new Color(20+rand.nextInt(110),20+rand.nextInt(110),20+rand.nextInt(110))); g.drawString(num, 13*i+6, 16); } session.setAttribute("srand", srand); OutputStream os=response.getOutputStream(); ImageIO.write(bimg, "jpeg", os); os.flush(); os.close(); os=null; g.dispose(); //这两句非常重要,如果没有会报错 out.clear(); out=pageContext.popBody(); %>

3、validate.jsp对验证码进行验证

<%@ page language="java" contentType="text/html; charset=ISO⑻859⑴" pageEncoding="ISO⑻859⑴"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO⑻859⑴"> <title>Validate Page</title> </head> <body> <% String str=(String)session.getAttribute("srand"); String incode=request.getParameter("vcode"); if(str.equals(incode)){ out.println("<font size="+3" color="#000">The verification is right!</font><br/>"); out.println("<font size="+4" color="#FF0000">Welcone to the page!</font><br/><hr/>"); }else{ out.println("<font size="+4" color="#FF0000">" +"Sorry the verification code is not right</font></br><hr>"); } out.println("<br/><a href="login.jsp">Back to the login page</a>"); %> </body> </html>

4、结果



生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生