JavaWeb中怎么利用Session實(shí)現(xiàn)一次性驗(yàn)證碼功能,針對(duì)這個(gè)問(wèn)題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問(wèn)題的小伙伴找到更簡(jiǎn)單易行的方法。
創(chuàng)新互聯(lián)公司2013年成立,是專(zhuān)業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都網(wǎng)站建設(shè)、做網(wǎng)站網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元永濟(jì)做網(wǎng)站,已為上家服務(wù),為永濟(jì)各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話(huà):18982081108表單
載入頁(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í)豎線(xiàn)分隔)、字號(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)證碼的大小寫(xiě)。
不區(qū)分大小寫(xiě):
//先轉(zhuǎn)換為全大寫(xiě)|全小寫(xiě),再判斷 trueCode=trueCode.toLowerCase(); code=code.toLowerCase(); //trueCode=trueCode.toUpperCase(); //code=trueCode.toUpperCase();
關(guān)于JavaWeb中怎么利用Session實(shí)現(xiàn)一次性驗(yàn)證碼功能問(wèn)題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒(méi)有解開(kāi),可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識(shí)。