login.jsp <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</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"> --> <style type="text/css"> body{font-size:16px;} </style> <script type="text/javascript"> function mycheck(){ if(form1.userName.value==""){ alert("用戶名不能為空,請輸入用戶名"); form1.userName.focuse(); return; } if(form1.password.value==""){ alert("密碼不能為空,請輸入密碼"); form1.password.focus(); return; } if(form1.validationCode.value==""){ alert("驗證碼不能為空,請輸入驗證碼"); form1.validationCode.focus(); return; } if(form1.validationCode.value!=form1.validationCode1.value){ alert("請輸入正確的驗證碼"); form1.validationCode.focus(); return; } form1.submit1(); } </script> </head> <body> <form action="logincheck.jsp" name="form1" method="post"> 用戶名:<input type="text" name="userName" size="16"><br> 密 碼: <input type="password" name="password" size="18"><br> 驗證碼:<input type="text" name="validationCode" onkeyDown="if(event.keyCode==13){form1.submit.focus();}" size=6> <% int intmethod1 = (int)(((Math.random())*11)-1); int intmethod2 = (int)(((Math.random())*11)-1); int intmethod3 = (int)(((Math.random())*11)-1); int intmethod4 = (int)(((Math.random())*11)-1); //將的到的隨機數進行連接 String intsum = ""+intmethod1+intmethod2+intmethod3+intmethod4; %> <!--設置隱藏域驗證比較時使用 --> <input type="hidden" name="validationCode1" value="<%=intsum%>"> <input type="text" value="<%=intsum%>" size=5> <br> <input type="submit" name="submit1" value="登錄" onClick="mycheck()"> <input type="reset" value="重置"> </form> <br> </body> </html>
logincheck.jsp <%@page contentType="text/html" pageEncoding="UTF-8"%> <html> <head> <title>處理登錄頁面的數據</title> </head> <body bgcolor="pink"> <% //設置請求的編碼,永遠解決中文亂碼問題 request.setCharacterEncoding("UTF-8"); String name = request.getParameter("userName"); String password = request.getParameter("password"); if(request.getParameter("validationCode1").equals(request.getParameter("validationCode"))){ if(name.equals("chenan")&&password.equals("123456")){ session.setAttribute("userName", name); response.sendRedirect("main.jsp"); }else{ response.sendRedirect("login.jsp"); } }else{ response.sendRedirect("login.jsp"); } %> </body> </html>
main.jsp <%@page contentType="text/html" pageEncoding="UTF-8"%> <html> <head> <title>系統主頁面</title> </head> <body bgcolor="pink"> <% //獲取保存在session中的用戶名 String name=(String)session.getAttribute("userName"); %> 您好<%=name%>,歡迎您訪問!<br> <a href="exit.jsp">[退出系統]</a> </body> </html>
exit.jsp <%@page contentType="text/html" pageEncoding="UTF-8"%> <html> <head> <title>退出系統</title> </head> <body> <% session.invalidate();//銷毀session response.sendRedirect("login.jsp"); %> </body> </html>


浙公網安備 33010602011771號