login.jspjavascript
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Login page</title> 8 <style type="text/css"> 9 body{ 10 color:#000; 11 font-size:12px; 12 margin:0px auto; 13 } 14 </style> 15 16 <script type="text/javascript"> 17 function check(form){ 18 //document.forms.form1.username.value取得form1中Username的值 並判斷是否爲空 19 if(""==document.forms.form1.username.value){ 20 //若是 爲""則彈出提示 21 alert("pls input username"); 22 //將輸入焦點定位到沒有輸入的地方 23 document.forms.form1.username.focus(); 24 //返回錯誤 25 return false; 26 } 27 if(""==document.forms.form1.password.value){ 28 alert("pls input password"); 29 document.forms.form1.password.focus(); 30 return false; 31 } 32 } 33 34 </script> 35 36 </head> 37 <body> 38 <form action="loginDetails.jsp" method="post" name="form1"> 39 <table border="1" cellspacing="1" cellpadding="1" bordercolor="silver" align="center"> 40 <tr> 41 <td colspan="2" align="center" bgcolor="#e8e8e8">用戶登陸</td> 42 </tr> 43 <tr> 44 <td>用戶名:</td> 45 <td><input type="text" name="username" /></td> 46 </tr> 47 <tr> 48 <td>密碼:</td> 49 <td><input type="password" name="password"/></td> 50 </tr> 51 <tr> 52 <!-- onclick="return check(this) 調用上面的Script進行驗證 --> 53 <td><input type="submit" name="submit" value="登陸" onclick="return check(this);"/> <input type="reset" name="reset" value="重置"/></td> 54 </table> 55 </form> 56 57 </body> 58 </html>
loginDetails.jspcss
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>The login details info page</title> 8 </head> 9 <body> 10 <% 11 String name=request.getParameter("username"); 12 String pass=request.getParameter("password"); 13 out.println("接收到的用戶名:"+name); 14 out.println("接收到的密碼:"+pass); 15 %> 16 17 </body> 18 </html>