JavaWeb中怎么利用Session實現(xiàn)一次性驗證碼功能,針對這個問題,這篇文章詳細介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
創(chuàng)新互聯(lián)公司2013年成立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項目成都網(wǎng)站建設(shè)、做網(wǎng)站網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元永濟做網(wǎng)站,已為上家服務(wù),為永濟各地企業(yè)和個人服務(wù),聯(lián)系電話:18982081108表單
<form action="loginServlet" method="post"> 請輸入驗證碼:<input type="text" name="code" /> <img src="getCodeServlet" /><br /> <button type="submit">提交</button></form>
載入頁面時,會自動請求getCodeServlet,獲取圖片(驗證碼)。
getCodeServlet,產(chǎn)生驗證碼
@WebServlet("/getCodeServlet")public class GetCodeServlet extends HttpServlet { //驗證碼的寬、高 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); //隨機繪制100個干擾點 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); //干擾點的顏色也可以隨機,隨機產(chǎn)生red,green,blue即可 //g.setColor(new Color(red,green,blue)); } } //繪制驗證碼 private void drawCode(Graphics g,char[] code){ g.setColor(Color.BLACK); //字體、樣式(多個時豎線分隔)、字號 g.setFont(new Font("serif",Font.ITALIC|Font.BOLD,18)); //在不同位置繪制驗證碼字符,參數(shù):要繪制的String、橫、縱坐標。+""是為了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); } //隨機產(chǎn)生4位驗證碼 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(); //將驗證碼放到session域中。session對象要在提交響應(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("驗證碼正確"); } else { response.getWriter().write("驗證碼錯誤"); } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); }}
上面的處理方式要區(qū)分驗證碼的大小寫。
不區(qū)分大小寫:
//先轉(zhuǎn)換為全大寫|全小寫,再判斷 trueCode=trueCode.toLowerCase(); code=code.toLowerCase(); //trueCode=trueCode.toUpperCase(); //code=trueCode.toUpperCase();
關(guān)于JavaWeb中怎么利用Session實現(xiàn)一次性驗證碼功能問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識。
當前名稱:JavaWeb中怎么利用Session實現(xiàn)一次性驗證碼功能-創(chuàng)新互聯(lián)
本文地址:http://bm7419.com/article22/cdeojc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)、全網(wǎng)營銷推廣、網(wǎng)站改版、網(wǎng)站內(nèi)鏈、域名注冊、云服務(wù)器
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容