java驗證碼代碼_第1頁
java驗證碼代碼_第2頁
java驗證碼代碼_第3頁
java驗證碼代碼_第4頁
java驗證碼代碼_第5頁
免費預(yù)覽已結(jié)束,剩余7頁可下載查看

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、java 驗證碼代碼(轉(zhuǎn))產(chǎn)生圖片 servletimportjava.awt.Color;importjava.awt.Font;importjava.awt.Graphics2D;importjava.awt.image.Bufferedlmage;importjava.util.Random;importjavax.imageio.ImagelO;importjavax.servlet.ServletException;importjavax.servlet.ServletOutputStream;importjavax.servlet.http.HttpServlet;importja

2、vax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjavax.servlet.http.HttpSession;/* Packagename:com.skcc.sample.actionFilename:VerifyCodeServlet.java* Author:AdministratorDate:2008-9-4Description:VerifyCodeServlet*/publicclassVerifyCodeServletextendsHttpServlet/*

3、/*驗證碼圖片的寬度。*/privateintwidth=60;/*驗證碼圖片的高度。*/privateintheight=20;/*驗證碼字符個數(shù)privatestaticfinallongserialVersionUID=1L;J*/private/*xx*/private/*intcodeCount=4;intxx=0;*字體高度*/privateintfontHeight/*codeY*/privateintcodeY/*codeSequence*/char codeSequenceABC,DEFG,HW,K,L,M,N:,O,P,Q,R,S,T,UX,Y,Z,0,1,2,3,4,5,

4、6,7IV89;/*初始化驗證圖片屬性*/publicvoidinit()throwsServletException/從web.xml中獲取初始信息寬度StringstrWidth=/高度StringstrHeight=/字符個數(shù)StringstrCodeCount=thisthis.getInitParameter(.getInitParameter(this.getInitParameter(widthheight/將配置的信息轉(zhuǎn)換成數(shù)值tryif(strWidth!=null&strWidth.length()!=0)width=Integer.parseInt(strWidt

5、h);););codeCount);null&strCodeCount.length()!=0)parseInt(strCodeCount);)catch(NumberFormatExceptione)e.printStackTrace();xx=width/(codeCount+1);codeY=height-4;Graphics2Dgd=buffImg.createGraphics();/創(chuàng)建一個隨機數(shù)生成器類/創(chuàng)建字體,字體的大小應(yīng)該根據(jù)圖片的高度來定。/設(shè)置字體。gd.setFont(font);)if(strHeight!=null&strHeight.length(

6、)!=0)height=Integer.parseInt(strHeight);Fontfont=newFont(Fixedsys,Font.PLAIN,fontHeight);)if(strCodeCount!=codeCount=Integer.fontHeightheight-2;/*paramparamreq*/throwsthrowsrespServletExceptionjava.io.IOExceptionprotectedresp)voidservice(HttpServletRequestreq,HttpServletResponsethrowsServletExceptio

7、n,java.io.IOException/定義圖像 bufferBufferedImagebuffImg=BufferedImage.newBufferedImage(TYPE_INT_RGB);widthheightRandomrandom=newRandom();/將圖像填充為白色gd.setColor(Color.gd.fillRect(0,0,WHITE);width,height);BLACK);width-1,height-1);/隨機產(chǎn)生 160 條干擾線,使圖象中的認證碼不易被其它程序探測到。gd.setColor(Color.BLACK);for(inti=0;i160;i

8、+)intx=random.nextInt(width);inty=random.nextInt(height);intxl=random.nextInt(12);intyl=random.nextInt(12);gd.drawLine(x,y,x+xl,y+yl);)/randomCode 用于保存隨機產(chǎn)生的驗證碼,以便用戶登錄后進行驗證。StringBufferrandomCode=newStringBuffer();intred=0,green=0,blue=0;/隨機產(chǎn)生 codeCount 數(shù)字的驗證碼。for(inti=0;icodeCount;i+)/得到隨機產(chǎn)生的驗證碼數(shù)字。S

9、tringstrRand=String.valueOf(codeSequencerandom.nextInt(36);/產(chǎn)生隨機的顏色分量來構(gòu)造顏色值,這樣輸出的每位數(shù)字的顏色值都將不同red=random.nextInt(255);green=random.nextInt(255);blue=random.nextInt(255);/用隨機產(chǎn)生的顏色將驗證碼繪制到圖像中。gd.setColor(newColor(red,green,blue);gd.drawString(strRand,(i+1)*xx,codeY);/將產(chǎn)生的四個隨機數(shù)組合在一起。randomCode.append(str

10、Rand);)/將四位數(shù)字的驗證碼保存到 Session 中。HttpSessionsession=req.getSession();session.setAttribute(validateCode,randomCode.toString();/禁.止圖像緩存。resp.setHeader(Pragma,no-cache);resp.setHeader(Cache-Control,no-cache);resp.setDateHeader(Expires,0);/畫邊框。gd.setColor(Color.gd.drawRect(0,0,/* ThedoGetmethodoftheservle

11、t.* Thismethodiscalledwhenaformhasitstagvaluemethodequalstoget.* paramrequest* therequestsendbytheclienttotheserver* paramresponse* theresponsesendbytheservertotheclient* throwsServletExceptionresp.setContentType(image/jpeg);/將圖像輸出到 Servlet 輸出流中。ServletOutputStreamsos=resp.getOutputStream();ImageIO.

12、write(buffImg,jpeg,sos);sos.close();產(chǎn)生結(jié)果 servletimportjava.io.IOException;importjava.io.PrintWriter;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;/*Packagename:com.skcc.sample.actionF

13、ilename:ResultServlet.javaAuthor:*AdministratorDate:2008-9-4Description:ResultServlet*/publicclassResultServletextendsHttpServlet/*/privatestaticfinallongserialVersionUID=1L;* ifanerroroccurred* throwslOException* ifanerroroccurred* /publicvoiddoGet(HttpServletRequestrequest,HttpServletResponserespo

14、nse)throwsServletException,IOExceptiondoPost(request,response);/* ThedoPostmethodoftheservlet.* Thismethodiscalledwhenaformhasitstagvaluemethodequalsto*post.* paramrequesttherequestsendbytheclienttotheserver* paramresponsetheresponsesendbytheservertotheclient* throwsServletExceptionifanerroroccurred

15、* throwslOException* ifanerroroccurred* /publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,lOExceptionresponse.setContentType(text/html;charset=utf-8);StringvalidateC=(String)request.getSession().getAttribute(validateCode);StringveryCode=request.getParamet

16、er(veryCode);PrintWriterout=response.getWriter();if(veryCode=null|.equals(veryCode)out.println(驗證碼為空);elseif(validateC.equals(veryCode)out.println(驗證碼正確);elseout.println(驗證碼錯誤);)js 文件functionchangeImg()varimgSrc=$(#imgObj);varsrc=imgSrc.attr(src);imgSrc.attr(src,chgUrl(src);)/時間戳/為了使每次生成圖片不一致,即不讓瀏覽器

17、讀緩存,所以需要加上時間戳functionchgUrl(url)vartimestamp=(newDate().valueOf();url=url.substring(0,17);&)=0)?atamp=+timestamp;?timestamp=+timestamp;returnurl;functionisRightCode()varcode=$(#veryCode).attr(value);code=c=+code;$.ajax(type:POST,url:resultServlet,data:code,success:callback);functioncallback(data

18、)$(#info).html(data);頁面 verifyCode.jspout.flush();out.close();if(url.indexOf(url=url+elseurl=url+%pagelanguage=javapageEncodingPUBLIC-/W3C/DTDHTML4.01Transitional/EN/TR/html4/loose.dtdmetahttp-equiv=Content-Typecontent=text/html;charset=UTF-8scriptscripttestverifycodesrc=js/verifyCode.jssrc=js/jquery.js/script/titleinput/換一張input=isRightCode()/最后記得配好 web.xmlservlet-nameservlet-nameservlet-classservlet-mappingservlet-nameurl-patte

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論