用java来实现验证码功能
生活随笔
收集整理的這篇文章主要介紹了
用java来实现验证码功能
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
?
科技優(yōu)家?2017-06-12 12:10昨天在網上看到了一篇關于驗證碼的文章,很不錯,但是有些不盡人意的地方,比如沒有考慮到前端傳過來的驗證碼如果是小寫的話,那么做驗證的時候就會出現(xiàn)錯誤,
因為java是嚴格區(qū)分大小寫的,還有就是驗證碼會重疊在一起的情況沒有考慮到,再者驗證碼看不清刷新的功能沒有做出來
在此我做下修改:
首先需要導入ValidateCode.jar包
1 package com.wqy.ValidateCode; 2 3 import java.io.IOException; 4 5 import javax.imageio.ImageIO; 6 import javax.servlet.ServletException; 7 import javax.servlet.http.HttpServlet; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse; 10 11 import cn.dsna.util.images.ValidateCode; 12 13 public class ValidateCode1 extends HttpServlet { 14 15 public void doGet(HttpServletRequest request, HttpServletResponse response) 16 throws ServletException, IOException { 17 //控制不要緩存 18 response.setHeader("Expires", "-1"); 19 response.setHeader("Cache-Control", "no-cache"); 20 response.setHeader("Pragma", "no-cache"); 21 int width=120;//寬度 22 int height=25;//高度 23 int codeCount=4;//驗證碼的個數(shù) 24 int lineCount=9;//干擾線的個數(shù) 25 //設置驗證碼 26 ValidateCode vc = new ValidateCode(width, height, codeCount, lineCount); 27 //ValidateCode(120,25,4,9); 28 String code = vc.getCode; 29 //將驗證碼存放到Attribute域中 30 request.getSession.setAttribute("code", code); 31 //輸出 32 ImageIO.write(vc.getBuffImg, "jpg", response.getOutputStream); 33 34 } 35 36 public void doPost(HttpServletRequest request, HttpServletResponse response) 37 throws ServletException, IOException { 38 doGet(request,response); 39 } 40 41 }下面是html代碼:
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%> <% String path = request.getContextPath; String basePath = request.getScheme+"://"+request.getServerName+":"+request.getServerPort+path+"/"; %> <html> <head> <title>驗證碼</title> </head> <body> <form action="${pageContext.request.contextPath}/servlet/LoginServlet" method="post"> 用戶名:<input type="text" name="username"/></br> 密碼:<input type="password" name="password"/></br> 驗證碼:<input type="text" name="captcha" size="4"> <img src="${pageContext.request.contextPath}/servlet/ValidateCode1" id="CreateCheckCode" οnclick="this.src=this.src+'?'" width="120" height="25"> <a href="javascript:CreateCheckCode.onclick">看不清,換一張</a></br> <input type="submit" value="登錄"> </form> </body> </html>后臺驗證的servlet代碼:
1 package com.wqy.ValidateCode; 2 3 import java.io.IOException; 4 5 import javax.servlet.ServletException; 6 import javax.servlet.http.HttpServlet; 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletResponse; 9 10 public class LoginServlet extends HttpServlet { 11 public void doGet(HttpServletRequest request, HttpServletResponse response) 12 throws ServletException, IOException { 13 //獲取前端傳來的驗證碼 14 String name = request.getParameter("captcha"); 15 //從Attribute域中獲取存放的驗證碼 16 String code = (String)request.getSession.getAttribute("code"); 17 //前后端驗證碼做比較。(驗證碼.toLowerCase是讓它忽略大小寫,這樣在前端做驗證的時候就不會因為大小寫問題引發(fā)異常) 18 if(code.toLowerCase.equals(name.toLowerCase)){ 19 System.out.println("驗證碼正確"); 20 } 21 } 22 23 public void doPost(HttpServletRequest request, HttpServletResponse response) 24 throws ServletException, IOException { 25 doGet(request, response); 26 } 27 28 }最終顯示結果:
轉載于:https://www.cnblogs.com/shierban/p/6994368.html
總結
以上是生活随笔為你收集整理的用java来实现验证码功能的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Hive Cilent数据操作
- 下一篇: 【bzoj2693】jzptab 莫比