Jsp servlet驗(yàn)證碼工具類分享
昨晚在csdn看到一位前輩寫一個(gè)ajax+servlet+jsp驗(yàn)證,頓時(shí)心血來(lái)潮,在閱讀前輩的代碼下我親手體驗(yàn)一下,做了一個(gè)驗(yàn)證碼生成工具類,以供大家做個(gè)參考。
1、添加veriycodeutils類生成驗(yàn)證碼圖像
package com.servlet; import java.awt.color; import java.awt.font; import java.awt.graphics2d; import java.awt.image.bufferedimage; import java.io.outputstream; import java.util.random; import javax.imageio.imageio; /** * * @author hubiao * 驗(yàn)證碼生成器 * 用到api * bufferedimage 創(chuàng)建一個(gè)圖像 * graphics2d 繪制 * fillrect(x,y,width,height);背景 * font()字體 * drawrect();邊框 * drawline();線 * drwastring:圖像數(shù)據(jù) * imageio 生成圖像 */ public class veriycodeutils { /** * @param output 保存驗(yàn)證圖像的流 * @return 驗(yàn)證碼 */ public static string newveriycode(outputstream output) { int width = 90; int height = 40; int codecount = 5; char[] codesequence = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' }; //創(chuàng)建圖像對(duì)象,8位rgb bufferedimage buffered = new bufferedimage(width, height, bufferedimage.type_int_rgb); //通過(guò)crapahices來(lái)繪制圖像到bufferedimage中 graphics2d gra = buffered.creategraphics(); //設(shè)置圖片背景:白色 gra.setcolor(color.white); gra.fillrect(0, 0, width, height); //設(shè)置字體,字體大小根據(jù)圖片高度決定 gra.setfont(new font("fixedsys",font.plain,height-2)); //設(shè)置邊框:黑色,1cm gra.setcolor(color.black); gra.drawrect(0, 0, width-1, height-1); //生成10條黑色干擾線 gra.setcolor(color.black); random ran = new random(); for(int i = 0; i < 70;i++) { int x = ran.nextint(255); int y = ran.nextint(255); int x1 = ran.nextint(255); int y1 = ran.nextint(255); gra.drawline(x, y,x+x1, y+y1);//畫(huà)直線 } //生成驗(yàn)證碼 stringbuffer sb = new stringbuffer(); int r = 0,g = 0,b = 0; for(int i = 0; i < codecount; i++) { string strrand = string.valueof(codesequence[ran.nextint(codesequence.length)]); //對(duì)每位驗(yàn)證碼都生成不同的顏色,增加識(shí)別系統(tǒng)難度 r = ran.nextint(255); g = ran.nextint(255); b = ran.nextint(255); gra.setcolor(new color(r, g, b)); gra.drawstring(strrand, (i+1)*13, height-4); sb.append(strrand); } try { imageio.write(buffered, "jpeg", output); } catch (exception e) { throw new runtimeexception(e); } return sb.tostring(); } }
2、servlet使用驗(yàn)證碼
protected void dopost(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception { //禁止圖像緩存 resp.setheader("pragma", "no-cache"); resp.setheader("cache-control", "no-cache"); resp.setdateheader("expires", 0); resp.setcontenttype("image/jpeg"); //生成驗(yàn)證碼圖像 string veriycode = veriycodeutils.newveriycode(resp.getoutputstream()); //將驗(yàn)證碼保存到session中 httpsession session = req.getsession(); session.setattribute("validatecode", veriycode); }
3、jsp頁(yè)面使用驗(yàn)證碼
</head> <script type="text/javascript"> function createcode() { var t = new date().gettime();//防止頁(yè)面緩存,使用時(shí)間搓 var srcimg = document.getelementbyid("srcimg"); srcimg.src="/imgveifyweb/vity.do?"+t; } </script> <body> <h1>${requestscope.code}</h1> <img id="srcimg" src="<c:url value="/vity.do"></c:url>" /> #這里使用直接讓img訪問(wèn)servlet,通過(guò)response響應(yīng)一個(gè)圖像流 <a href="##" rel="external nofollow" id="codeid" onclick="createcode()">換一張</a> <form action="<c:url value="/hello.do"></c:url>" method="post"> <input type="text" name="codevify"/> <input type="submit" value="提交"/> </form> </body>
4、校驗(yàn)驗(yàn)證碼
protected void dopost(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception { httpsession session = req.getsession(); object validatecode = session.getattribute("validatecode"); system.out.println(validatecode); string codevify = req.getparameter("codevify"); if(codevify==null || codevify.equals("")) { req.setattribute("code","驗(yàn)證證不能為空"); req.getrequestdispatcher("/index.jsp").forward(req, resp); return; }else if(!validatecode.tostring().equalsignorecase(codevify)) { req.setattribute("code","驗(yàn)證證錯(cuò)誤"); req.getrequestdispatcher("/index.jsp").forward(req, resp); return; } system.out.println("下面開(kāi)始 做其他業(yè)務(wù)操作...."); }
校驗(yàn)圖如下:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持碩編程。
相關(guān)文章
- jsp+servlet實(shí)現(xiàn)文件上傳與下載功能
- EJB3.0部署消息驅(qū)動(dòng)Bean拋javax.naming.NameNotFoundException異常
- 在JSP中使用formatNumber控制要顯示的小數(shù)位數(shù)方法
- 秒殺系統(tǒng)Web層設(shè)計(jì)的實(shí)現(xiàn)方法
- 將properties文件的配置設(shè)置為整個(gè)Web應(yīng)用的全局變量實(shí)現(xiàn)方法
- JSP使用過(guò)濾器防止Xss漏洞
- 在JSP頁(yè)面中動(dòng)態(tài)生成圖片驗(yàn)證碼的方法實(shí)例
- 詳解JSP 內(nèi)置對(duì)象request常見(jiàn)用法
- 使用IDEA編寫jsp時(shí)EL表達(dá)式不起作用的問(wèn)題及解決方法
- jsp實(shí)現(xiàn)局部刷新頁(yè)面、異步加載頁(yè)面的方法
- Jsp中request的3個(gè)基礎(chǔ)實(shí)踐
- JavaServlet的文件上傳和下載實(shí)現(xiàn)方法
- JSP頁(yè)面的靜態(tài)包含和動(dòng)態(tài)包含使用方法