Servlet之验证码的实现和执行流程

来源:互联网 发布:csgo枪支数据 编辑:程序博客网 时间:2024/05/01 17:17

验证码是一个很常见的一个功能,验证码可以防止黑客通过某种手段无间隔的注册等等

验证码是通过服务器端生成的一张图片,传到客户端

 

具体实现步骤(流程):

1)由服务器端比如servlet,随机产生几个字符数字等等,把这几个字符拼接成一个字符串,存到session域中

 

2)以图片的方式传给客户端,验证码上的具体值是在服务器端验证的,如果具体值在客户端验证的话,恐怕不太方便比如第一次访问静态登录界面的话,去取session的值是为空的,除非你再次刷新整个页面

 

3)客户端对验证码,验证是否为空,输入的字符的长度是否达到标准,

 

4)把输入的验证码传到服务器端,服务端在session中取到值,做具体判断

 

5)根据输入对否,跳转到另一界面,如果输入错误,一般跳回当前界面,验证码改变又被刷新一次

 

 

具体实现的页面

产生验证码的 Response_3类  参考-------->点击打开链接

 

[html] view plaincopy
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  8. <html>  
  9.   <head>  
  10.     <base href="<%=basePath%>">  
  11.       
  12.     <title>My JSP 'ValidateLogin.jsp' starting page</title>  
  13.       
  14.     <meta http-equiv="pragma" content="no-cache">  
  15.     <meta http-equiv="cache-control" content="no-cache">  
  16.     <meta http-equiv="expires" content="0">      
  17.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  18.     <meta http-equiv="description" content="This is my page">  
  19.     <!-- 
  20.     <link rel="stylesheet" type="text/css" href="styles.css"> 
  21.     -->  
  22.   
  23.   </head>  
  24.   
  25.  <BODY style="background-color:66CCFF" >  
  26.  <table cellspacing="10"  
  27.  border=1 background="" align="center">  
  28.  <tr>  
  29.  <td>  
  30.   
  31.  <b  style="font-size:20;color:green;" >会员--登录</b>  
  32.  <hr width=220 align=left>  
  33.     
  34.   <form name=form1 Action="LoginServlet" method="get"  >  
  35.     
  36.   
  37.     
  38.   <br />  
  39.    用户名:  
  40.    <input type="text" name="usename" value=543806053@qq.com size=19 maxlength=16 />  
  41.    <br />  
  42.    <br />  
  43.    密  码:  
  44.    <input type="password" name="password" maxlength=16 />  
  45.    <br /><br />  
  46.    认证码: <input type="text" name="validateValue" size=19  onblur="checkVlidte(this)"/><br>  
  47.      
  48.    <img id="image" src="servlet/Response_3"/>   
  49.    <a href="javascript:changeimage()" >看不清?换一张</a>  
  50.    <br /><br />  
  51.   
  52.      
  53.    <br /><br />  
  54.             <input type="submit" value="登录"/>  
  55.      <input type="reset" value="重置" />  
  56.    <hr width=220 align=left>  
  57.   </form>  
  58.   </td>  
  59.   </tr>  
  60.   
  61.  </table>  
  62.   
  63.   
  64.   </body>  
  65.     
  66.   <script type="text/javascript">  
  67.    function validate_required(field,alerttxt)  
  68.    {  
  69.         with(field)  
  70.         {  
  71.                
  72.              /*if(value==null||value=="")  
  73.              {  
  74.                  alert(alerttxt);  
  75.                  return false ;  
  76.              }  
  77.              else     
  78.                  return true ;  
  79.              */  
  80.   
  81.              i = value.indexOf("@");  //邮箱认证  
  82.              j = value.lastIndexOf(".");  
  83.              if(i < 1 || j-i <2)  
  84.              {  
  85.                  alert(alerttxt) ;  
  86.                  return false ;  
  87.              }  
  88.              else  
  89.                  return true ;  
  90.         }  
  91.    }  
  92.      
  93.    function password_validate (_pass,alerttxt)  
  94.    {  
  95.        with(_pass)  
  96.        {  
  97.             if(value!="")  
  98.                return true ;  
  99.             else  
  100.             {   
  101.                alert(alerttxt) ;  
  102.                return false ;  
  103.             }  
  104.        }  
  105.    }  
  106.   
  107.   
  108.    function validate_form(thisform)  
  109.    {  
  110.         with(thisform)  
  111.         {  
  112.              if(validate_required(name1,"请输入有效的邮箱账号!") == false)  
  113.              {  
  114.                   name1.focus();  
  115.                   return false ;  
  116.              }  
  117.   
  118.              if(password_validate(pass,"请输入正确的密码!") == false)  
  119.              {  
  120.                   pass.focus();  
  121.                   return false ;  
  122.              }  
  123.                     
  124.         }  
  125.    }  
  126.   
  127.   </script>  
  128.   
  129.   <script type="text/javascript">  
  130.   
  131.    function proving()  
  132.    {  
  133.        if(form1.pass.value!="")  
  134.           window.open("http://www.baidu.com/","_self");  
  135.        else  
  136.           window.open("http://www.google.com/","_self");  
  137.   
  138.         
  139.    }  
  140.   
  141.   </script>  
  142.   
  143. <!--认证码按钮脚本-->  
  144.   <script type="text/javascript">  
  145.       function changeimage()  
  146.       {  
  147.          var img = document.getElementById("image") ;  
  148.          if(img)  
  149.            img.src=img.src+ "?" + new Date().getTime();  
  150.       }  
  151.   
  152.       function checkVlidte(object)  
  153.       {  
  154.             
  155.           if(object.value.length != 4 )  
  156.           {  
  157.              alert("验证码错误!");  
  158.   
  159.              //object.focus() ;//重新定位到该输入框  
  160.              //object.select() ;//把已有的内容选中  
  161.           }  
  162.       }  
  163.   
  164.   </script>  
  165.     
  166. </html>  


服务器端验证,这里只对验证码进行验证

[java] view plaincopy
  1. package cn.request;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.PrintWriter;  
  5.   
  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. public class LoginServlet extends HttpServlet {  
  12.   
  13.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  14.             throws ServletException, IOException {  
  15.           
  16.         this.doPost(request, response);  
  17.   
  18.   
  19.     }  
  20.   
  21.   
  22.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  23.             throws ServletException, IOException {  
  24.           
  25.         String usename = request.getParameter("usename");  
  26.         String password = request.getParameter("password");  
  27.         String validateValue = request.getParameter("validateValue");  
  28.           
  29.         String vv = (String)request.getSession().getAttribute("validateValue");  
  30.           
  31.         //把验证码全转为小写在进行比较  
  32.         if( (validateValue.toLowerCase()).equals(vv.toLowerCase()) )  
  33.         {  
  34.             response.sendRedirect("index.jsp");  
  35.         }  
  36.         else  
  37.         {  
  38.             request.getRequestDispatcher("ValidateLogin.jsp").forward(request, response);  
  39.         }  
  40.   
  41.   
  42.     }  
  43.   
  44. }  
0 0
原创粉丝点击