jsp验证码实现代码

jsp验证码实现代码

ID:34723963

大小:64.68 KB

页数:3页

时间:2019-03-10

jsp验证码实现代码_第1页
jsp验证码实现代码_第2页
jsp验证码实现代码_第3页
资源描述:

《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位任意数字的验证码验证。

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。