標籤:lan sep codec .com date htm 實現 ons ase
科技優家 2017-06-12 12:10
昨天在網上看到了一篇關於驗證碼的文章,很不錯,但是有些不盡人意的地方,比如沒有考慮到前端傳過來的驗證碼如果是小寫話,那麼做驗證的時候就會出現錯誤,
因為java是嚴格區分大小寫,還有就是驗證碼會重疊在一起的情況沒有考慮到,再者驗證碼看不清重新整理的功能沒有做出來
在此我做下修改:
首先需要匯入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;//驗證碼的個數 24 int lineCount=9;//幹擾線的個數 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" onclick="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是讓它忽略大小寫,這樣在前端做驗證的時候就不會因為大小寫問題引發異常) 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 }
最終顯示結果:
用java來實現驗證碼功能