java实现动态验证码源代码——jsp页面
生活随笔
收集整理的這篇文章主要介紹了
java实现动态验证码源代码——jsp页面
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
??? 我們通常在其他網站注冊或者登陸的時候會發現一個很高大上的驗證碼,那么怎么用java代碼實現呢?下面我給大家貼一下我寫的方法:
?? 此文章是寫的前臺jsp頁面的代碼:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <%@ taglib uri="/struts-tags" prefix="s" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%String tag=request.getParameter("tag");request.setAttribute("tag",tag);%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head><base href="<%=basePath%>"><title>用戶登錄</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--><link href="<%=basePath%>css/common.css" rel="stylesheet" type="text/css" /><link href="<%=basePath%>css/style.css" rel="stylesheet" type="text/css" /><script type="text/javascript" src="js/jquery-1.8.3.js"></script><script type="text/javascript">$(function(){if(${tag == 'nologin'}){alert("對不起,您還沒有登錄,請先進行登錄");}});</script><script type="text/javascript" >$(function(){//注冊$("#register").click(function(){location.href="register.jsp";});//驗證碼$("#changeCode").click(function(event){$("#validateCode").attr("src","Number.jsp?ran"+Math.random());event.preventDefault();return false; });//判斷驗證碼輸入是否正確$("#passwords").blur(function(){$.post("doNumrand.jsp","inputCode="+$("#passwords").val(),function(result){if($.trim(result)=="true"){$("#cl").html("<span style='color:green'>驗證碼正確</span>"); }else{$("#cl").html("<span style='color:red'>驗證碼錯誤</span>"); }});}); });</script><script type="text/javascript"><%String msg = request.getParameter("msg");if("registerSuccess".equals(msg)){out.print("alert('注冊成功,系統將自動跳轉登錄頁');");}if("loginError".equals(msg)){out.print("alert('用戶名密碼錯誤,請重新登錄');");}if("validateCodeError".equals(msg)){out.print("alert('驗證碼錯誤,請重新輸入');");}%> </script></head><body> <div class="wrap"> <!-- main begin--><div class="main"><div class="sidebar"><p><img src="images/img1.jpg" width="443" height="314" alt="" /></p></div><div class="sidebarg"><form action="login.action" method="post"><div class="login"><span style="color:red"><s:property value="msg"/></span><dl><dt class="blues">用戶登陸</dt><dd><label for="name">用戶名:</label><input type="text" name="auctionuser.username" class="inputh" value="${username}" id="name"/></dd><dd><label for="password">密 碼:</label><input type="password" name="auctionuser.userpassword" class="inputh" value="${userpassword}" id="password"/></dd><dd><label class="lf" for="passwords">驗證碼:</label><input type="text" name="inputCode" class="inputh inputs lf" value="" id="passwords"/><span class="wordp lf"><img id="validateCode" src="Number.jsp" width="96" height="27" alt="" /></span><span class="blues lf"><a id="changeCode" href="javascript:void(0);" title="">看不清</a>????<a id="cl"></a></span></dd><dd><input name="" type="checkbox" id="rem_u" /><span class="rem_u">下次自動登錄</span></dd><dd class="buttom"><input name="" type="submit" value="登 錄" class="spbg buttombg f14 lf" /><input id="register" name="" type="button" value="注 冊" class="spbg buttombg f14 lf" /><span class="blues lf"><a href="" title="">忘記密碼?</a></span><div class="cl"></div></dd></dl></div></form></div><div class="cl"></div></div> <!-- main end--><!-- footer begin--> </div><!--footer end--> </body> </html>??? 在這里主要是使用的ajax的方法,當鼠標離開輸入驗證碼的文本框時,進行驗證是否輸入正確!!
總結
以上是生活随笔為你收集整理的java实现动态验证码源代码——jsp页面的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 华硕公布 Hyper M.2 x16 G
- 下一篇: java实现动态验证码源代码——绘制验证