簡單的JSP登錄驗證注冊界面
*基於eclipse+apache tomcat 8.5 編寫的簡單認證判斷登錄和顯示信息。
**完成簡單邏輯判斷,對結果輸出
***JSP基礎練習登錄
登陸界面
index.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="UTF-8"> 7 <title>登陸界面</title> 8 9 </head> 10 <body> 11 <h2 style="text-align:lift;">注冊新用戶</h2> 12 <hr> 13 <form action="dealwith.jsp"> 14 <table align="center" border="1"> 15 <tr> 16 <td style="background-color: gray;">用戶名:</td> 17 <td><input type="text" name="user" > *【檢測用戶】</td> 18 </tr> 19 <tr> 20 <td style="background-color: gray;">密碼:</td> 21 <td><input type="password" name="password">*【需6-20內的字符】</td> 22 </tr> 23 <tr> 24 <td style="background-color: gray;">確認密碼:</td> 25 <td><input type="password" name="password_again">*</td> 26 </tr> 27 <tr> 28 <td style="background-color: gray;">性別:</td> 29 <td><input type="radio" value="男" checked="checked" name="boy">男<input type="radio" value="女s" >女</td> 30 </tr> 31 <tr> 32 <td style="background-color: gray;">學歷:</td> 33 <td><select name="xueli"> 34 <option >大專</option> 35 <option >本科</option> 36 </select></td> 37 </tr> 38 <tr> 39 <td style="background-color: gray;"></td> 40 <td style="background-color: gray;"><input style="margin-left: 20%;" type="submit" value="注冊"><input style="margin-left: 20%;" type="reset" value="重置"></td> 41 </tr> 42 </table> 43 </form> 44 </body> 45 <footer align="center">@ST</footer> 46 </html>
在第一個的基礎上編寫邏輯dealwith.jsp頁面,如果用戶名或密碼為空,則使用forward動作標記跳轉到error.jsp頁面,頁面輸出“請輸入用戶名與密碼,謝謝!”如果用戶名和密碼均不為空,則使用forward動作標記跳轉到success.jsp頁面,把前面用戶輸入的信息顯示出來。
判斷界面
dealwith.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="UTF-8"> 7 <title>登錄處理</title> 8 </head> 9 <body> 10 <% 11 request.setCharacterEncoding("UTF-8"); 12 String name=request.getParameter("user"); 13 String password=request.getParameter("password"); 14 String password2=request.getParameter("password_again"); 15 %> 16 <% if( "" == name || "" == password) 17 { 18 %> 19 <jsp:forward page="error.jsp"></jsp:forward> 20 <% 21 } 22 else 23 { 24 %> 25 <jsp:forward page="success.jsp"></jsp:forward> 26 <% 27 } 28 %> 29 </body> 30 </html>
判定錯誤
error.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="UTF-8"> 7 <title>錯誤</title> 8 </head> 9 <body> 10 <h1 align="center">請輸入用戶名與密碼,謝謝!</h1> 11 <h2 align="center"><a href="index.jsp">點擊重新進入登錄頁</a></h2> 12 </body> 13 </html>
判定成功
success.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="UTF-8"> 7 <title>成功!</title> 8 </head> 9 <body> 10 <h1 align="center">用戶:<%=request.getParameter("user")%>恭喜你登錄成功!!!</h1><br> 11 <h2 align="center">當前用戶:<%=request.getParameter("user")%><br> 12 密碼:<%=request.getParameter("password")%><br> 13 性別:<%=request.getParameter("boy")%><br> 14 學歷:<%=request.getParameter("xueli")%> </h2> 15 <h3 align="center"><a href="index.jsp">點擊重新進入登錄頁</a></h3> 16 </body> 17 </html>
最后要說:1.鏈接頁面修改名字要記得在跳轉處同步修改
2.index.jsp中的name需要命名,否則后面顯示不出來