靜態的登錄界面的設計login.htm,代碼如下:
<html>
<head>
<title>系統登錄</title>
<style type="text/css">...
<!--
.style1 {...}{
font-size: 18px;
font-weight: bold;
}
.style2 {...}{font-size: 24px}
.style5 {...}{font-size: 16px}
-->
</style>
</head>
<body bgcolor="papayawhip" width="300" height="300">
<center>
<table border="2" bordercolor="black" bgcolor="lightgreen">
<tbody>
<tr>
<td><div align="center" class="style1 style2">系 統 登 錄
</div></td>
</tr>
<form action="login.jsp" method="post">
<tr>
<td height="28"><span class="style5">用戶名</span> <input type="text" name="uid" maxlength="20" ></td></tr><br>
<tr>
<td><span class="style5">密 碼</span> <input type="password" name="upwd" maxlength="20" ></td></tr><br>
<center>
<tr><td><div align="center">
<input type="submit" value="登錄" >
<input type="reset" value="取消">
</div></td></tr>
</center>
</form>
</tbody>
</table>
</center>
</body>
</html>
將登錄用戶輸入的信息提交到login.jsp頁面機型處理,這里為了方便,不執行數據庫的訪問操作,直接使用sky2098作為登錄用戶名和密碼,但在實際中是要從數據庫中讀取的,該jsp頁面代碼實現如下:
<%[email protected] page contenttype="text/html;charset=gb2312"%>
<%...
if(request.getparameter("uid").equals("sky2098")&&request.getparameter("upwd").equals("sky2098")){
session.setattribute("login","ok");
session.setmaxinactiveinterval(-1);
%>
<jsp:forward page="main.jsp"/>
<%...
}else{
out.println("用戶名或密碼輸入錯誤!");
}
%>
如果登錄成功,則設定login的值為ok,提交到下一步驗證頁面,則進入main.jsp頁面,否則,如果輸入的用戶名和密碼不合法就打印錯誤信息,main.jsp頁面代碼如下:
<%[email protected] page contenttype="text/html;charset=gb2312"%>
<%[email protected] include file="checkvalid.jsp" %>
<html>
<head>
<title>~welcome to my homepage~</title>
</head>
<body>
<center>
~welcome to my homepage~
</center>
</body>
</html>
這個頁面使用<% @ include file="checkvalid.jsp" %>包含了一個jsp頁面checkvalid.jsp為了驗證輸入信息的合法性:
<%...
if(session.getattribute("login")==null||!session.getattribute("login").equals("ok")){
response.sendredirect("login.htm");
}
%>
如果輸入信息有誤,則回到登錄頁面,重新輸入登錄信息。
測試登錄功能。
啟動tomcat服務器,在ie地址欄中鍵入url為:
http://localhost:8080/sky2098/login-advanced/login.htm
新聞熱點
疑難解答