JavaWeb 使用Session實(shí)現(xiàn)一次性驗(yàn)證碼功能
表單
<form action="loginServlet" method="post"> 請(qǐng)輸入驗(yàn)證碼:<input type="text" name="code" /> <img src="getCodeServlet" /><br /> <button type="submit">提交</button> </form>
載入頁(yè)面時(shí),會(huì)自動(dòng)請(qǐng)求getCodeServlet,獲取圖片(驗(yàn)證碼)。
getCodeServlet,產(chǎn)生驗(yàn)證碼
@WebServlet("/getCodeServlet") public class GetCodeServlet extends HttpServlet { //驗(yàn)證碼的寬、高 private static int WIDTH=80; private static int HEIGHT=25; //繪制背景 private void drawBg(Graphics g){ //rgb g.setColor(new Color(128, 128, 128)); //繪制矩形。x,y,wigth,height g.fillRect(0,0,WIDTH,HEIGHT); //隨機(jī)繪制100個(gè)干擾點(diǎn) Random random=new Random(); for (int i=0;i<100;i++){ //產(chǎn)生(0,1)上的小數(shù),*WIDTH|HEIGHT,再取整也行 int x=random.nextInt(WIDTH); int y=random.nextInt(HEIGHT); g.drawOval(x,y,1,1); //干擾點(diǎn)的顏色也可以隨機(jī),隨機(jī)產(chǎn)生red,green,blue即可 //g.setColor(new Color(red,green,blue)); } } //繪制驗(yàn)證碼 private void drawCode(Graphics g,char[] code){ g.setColor(Color.BLACK); //字體、樣式(多個(gè)時(shí)豎線分隔)、字號(hào) g.setFont(new Font("serif",Font.ITALIC|Font.BOLD,18)); //在不同位置繪制驗(yàn)證碼字符,參數(shù):要繪制的String、橫、縱坐標(biāo)。+""是為了char轉(zhuǎn)String。 g.drawString(code[0]+"",1,17); g.drawString(code[1]+"",16,15); g.drawString(code[2]+"",31,18); g.drawString(code[3]+"",46,16); } //隨機(jī)產(chǎn)生4位驗(yàn)證碼 private char[] getCode(){ String chars="0123456789QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm"; char[] code=new char[4]; Random random=new Random(); for (int i=0;i<4;i++){ //[0,62) int index= random.nextInt(62); code[i]=chars.charAt(index); } return code; } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException { HttpSession session = request.getSession(); ServletOutputStream sos = response.getOutputStream(); response.setContentType("image/jpeg"); //設(shè)置瀏覽器不緩存此圖片 response.setHeader("Pragma","No-cache"); response.setHeader("Cache-Control","no-cache"); response.setDateHeader("Expires",0); //創(chuàng)建內(nèi)存圖片 BufferedImage bufferedImage = new BufferedImage(WIDTH, HEIGHT, TYPE_INT_RGB); Graphics g= bufferedImage.getGraphics(); char[] code=getCode(); //將驗(yàn)證碼放到session域中。session對(duì)象要在提交響應(yīng)之前獲得 session.setAttribute("code",new String(code)); drawBg(g); drawCode(g,code); g.dispose(); //將圖片輸出到瀏覽器 ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(bufferedImage,"JPEG",baos); baos.writeTo(sos); baos.close(); sos.close(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { doPost(request,response); } }
loginServlet,處理表單
@WebServlet("/loginServlet") public class LoginServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); HttpSession session = request.getSession(); String trueCode= (String) session.getAttribute("code"); String code=request.getParameter("code"); if (code.equals(trueCode)){ response.getWriter().write("驗(yàn)證碼正確"); } else { response.getWriter().write("驗(yàn)證碼錯(cuò)誤"); } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } }
上面的處理方式要區(qū)分驗(yàn)證碼的大小寫。
不區(qū)分大小寫:
//先轉(zhuǎn)換為全大寫|全小寫,再判斷 trueCode=trueCode.toLowerCase(); code=code.toLowerCase(); //trueCode=trueCode.toUpperCase(); //code=trueCode.toUpperCase();
總結(jié)
以上所述是小編給大家介紹的JavaWeb 使用Session實(shí)現(xiàn)一次性驗(yàn)證碼功能,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
相關(guān)文章
SpringBoot整合EasyExcel進(jìn)行大數(shù)據(jù)處理的方法詳解
EasyExcel是一個(gè)基于Java的簡(jiǎn)單、省內(nèi)存的讀寫Excel的開源項(xiàng)目。在盡可能節(jié)約內(nèi)存的情況下支持讀寫百M(fèi)的Excel。本文將在SpringBoot中整合EasyExcel進(jìn)行大數(shù)據(jù)處理,感興趣的可以了解一下2022-05-05java環(huán)境配好后jar文件打開命令框閃退(無(wú)打開方式,無(wú)反應(yīng))解決辦法
在Java開發(fā)中我們經(jīng)常會(huì)遇到運(yùn)行Jar包時(shí)閃退的問題,下面這篇文章主要給大家介紹了關(guān)于java環(huán)境配好后jar文件打開命令框閃退(無(wú)打開方式,無(wú)反應(yīng))的解決辦法,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2024-04-04SpringBoot實(shí)現(xiàn)Read Through模式的操作過程
Read Through模式通常是指一種緩存策略,其中當(dāng)應(yīng)用程序嘗試讀取數(shù)據(jù)時(shí),緩存系統(tǒng)首先被檢查以查看數(shù)據(jù)是否已經(jīng)存在于緩存中,這篇文章主要介紹了SpringBoot實(shí)現(xiàn)Read Through模式,需要的朋友可以參考下2024-07-07java Spring Boot 配置redis pom文件操作
這篇文章主要介紹了java Spring Boot 配置redis pom文件操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2020-07-07Springboot中MyBatisplus使用IPage和Page分頁(yè)的實(shí)例代碼
這篇文章主要介紹了Springboot中MyBatisplus使用IPage和Page分頁(yè),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12Springboot自動(dòng)配置原理及DataSource的應(yīng)用方式
這篇文章主要介紹了Springboot自動(dòng)配置原理及DataSource的應(yīng)用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07使用mybatisplus接收mysql字段為json類型的數(shù)據(jù)方式
這篇文章主要介紹了使用mybatisplus接收mysql字段為json類型的數(shù)據(jù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04