登錄頁面加入驗證碼

登陸頁面加入驗證碼,這個小栗子奮鬥用到了兩個頁面,0-9十個數字圖片(例如下面的8150)大家可以替換自己喜歡的圖片

1. index.jsp:


<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>    
    <title>登錄頁面</title>
	<script type="text/javascript">
		function loginCheck() {			
		
			if(loginForm.userName.value == "") {
				alert("用戶名不能爲空,請輸入用戶名!");
				loginForm.userName.focus();
				return false;
			}
			if(loginForm.userPassword.value == "") {
				alert("密碼不能爲空,請輸入密碼!");
				loginForm.userPassword.focus();
				return false;
			}
			if(loginForm.verification2.value == "") {
				alert("驗證碼不能爲空,請輸入驗證碼!");
				loginForm.verification2.focus();
				return false;
			}
			if(loginForm.verification2.value != loginForm.verification1.value) {
				alert("驗證碼錯誤!");
				loginForm.verification2.focus();
				return false;
			}
			
			return true;
			
		}
	</script>
  </head> 
  
  <body>
  	<form name="loginForm" action="check.jsp" method="post" οnsubmit="return loginCheck();">
  		<table align="center">
  			<tr align="center">
  				<td colspan="2"><h1>歡迎登錄</h1></td>
  			</tr>
  			<tr>
  				<td>用戶名:</td>
  				<td><input name="userName" type="text"/></td>
  			</tr>
  			<tr>
  				<td>密  碼:</td>
  				<td><input name="userPassword" type="password"/></td>
  			</tr>
  			<%//獲取隨機數
    			int num1 = (int)(Math.random() * 10);
    			int num2 = (int)(Math.random() * 10);
    			int num3 = (int)(Math.random() * 10);
    			int num4 = (int)(Math.random() * 10);
    			
    			String num = num1 + "" + num2 + num3 + num4;
    			
    		 %>
    		<span style="color:#339999;background-color: rgb(255, 255, 255);"> <!-- 寫一個隱藏域,用來進行驗證比較 --></span>
    		 <tr>
    		 	<td><input name="verification1" type="hidden" value="<%=num %>"/></td>
    		 </tr>
    		 <tr>
    		 	<td>驗證碼:</td>
    		 	 <td>
    		 	 	<input name="verification2" type="text" style="width: 60px;">
    		 	 	<img src="image/<%=num1 %>.png" />
    				<img src="image/<%=num2 %>.png" />
    				<img src="image/<%=num3 %>.png" />
    				<img src="image/<%=num4 %>.png" />
    		 	 </td>
    		 </tr>
    		 <tr>
    		 	<td colspan="2">
    		 		<input type="submit" value="提交">
    				<input type="reset" value="重置">
    		 	</td>
    		 </tr>
  		</table>
  	</form>
  </body>
</html>
2. check.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>    
    <title>檢查頁面</title>
  </head>
  
  <body>
    <%
    	String name = request.getParameter("userName");
    	String password = request.getParameter("userPassword");
    	String message;
    	
    	if(name.equals("li") && password.equals("123456")) {
    		message = "可以登錄系統!";
    	} else {
    		message = "用戶名或密碼錯誤!";
    	}
     %>
     <script type="text/javascript">
     	alert("<%=message%>");
     	window.location.href = 'login.jsp';
     </script>
  </body>
</html>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章