资源描述:
《jsp验证码实现代码》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、首先是生成验证码的jsp文件image.jsp<%@pagecontentType="image/jpeg"import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*"pageEncoding="UTF-8"%><%!ColorgetRandColor(intfc,intbc){Randomrandom=newRandom();if(fc>255)fc=255;if(bc>255)bc=255;intr=fc+random.nextInt(bc-fc);intg=fc+random.nextIn
2、t(bc-fc);intb=fc+random.nextInt(bc-fc);returnnewColor(r,g,b);}%><%out.clear();//这句针对resin服务器,如果是tomacat可以不要这句response.setHeader("Pragma","No-cache");response.setHeader("Cache-Control","no-cache");response.setDateHeader("Expires",0);intwidth=60,height=20;BufferedImageimage=newBuffered
3、Image(width,height,BufferedImage.TYPE_INT_RGB);Graphicsg=image.getGraphics();Randomrandom=newRandom();g.setColor(getRandColor(200,250));g.fillRect(0,0,width,height);g.setFont(newFont("TimesNewRoman",Font.PLAIN,18));g.setColor(getRandColor(160,200));for(inti=0;i<155;i++){intx=random.n
4、extInt(width);inty=random.nextInt(height);intxl=random.nextInt(12);intyl=random.nextInt(12);g.drawLine(x,y,x+xl,y+yl);}StringsRand="";for(inti=0;i<4;i++){Stringrand=String.valueOf(random.nextInt(10));sRand+=rand;g.setColor(newColor(20+random.nextInt(110),20+random.nextInt(110),20+ran
5、dom.nextInt(110)));g.drawString(rand,13*i+6,16);}//将认证码存入SESSIONsession.setAttribute("rand",sRand);g.dispose();ImageIO.write(image,"JPEG",response.getOutputStream());%>引用和刷新验证码代码检测验证码<%Stringrand=(String)session.getAttribute("rand");Stringinput=request.getParameter("rand");if(!rand.equals(input)){out.print("");}%>通过以上代码可以实现5位任意数字的验证码验证。