4 回答

TA貢獻(xiàn)1850條經(jīng)驗(yàn) 獲得超11個(gè)贊
一.本程序要實(shí)現(xiàn)的功能
第一次登錄頁(yè)面時(shí),若在表單中選擇了“記住密碼”,則下次登錄網(wǎng)站時(shí)不用在填寫表單,這里用“success.jsp”表示要打開的頁(yè)面。若本地Cookie文件中已經(jīng)保存了驗(yàn)證信息,則會(huì)顯示已登錄,否則會(huì)顯示沒登錄。
具體如下所示:
① .填寫表單,選擇記住密碼“一天“,代碼文件為:login.jsp
② .按下提交后,客戶鍛頁(yè)面跳到check.jsp,實(shí)際上在服務(wù)端已經(jīng)跳到了“success.jsp”頁(yè)面。
③ .新打開一個(gè)瀏覽器,然后直接打開“success.jsp”頁(yè)面,此時(shí)提示已登錄。說明Cookie起到了自動(dòng)登錄的作用。
若第①步中,在記住密碼項(xiàng)中選擇“不保存”則登錄“success.jsp”頁(yè)面
二. 對(duì)應(yīng)代碼
login.jsp
[java] view plaincopy
<%@ page contentType="text/html;charset=GBK"%>
<mce:script language="javaScript"><!--
function validate(f){
if(!(/^/w{5,15}$/.test(f.userId.value))){
alert("用戶id必須為5-15位!");
f.userId.focus();
return false;
}
if(!(/^/w{5,15}$/.test(f.password.value))){
alert("密碼必須為5-15位!");
f.password.focus();
return false;
}
return true;
}
// --></mce:script>
<form action="check.jsp" method="post" onSubmit="return validate(this)">
<table border="0">
<tr>
<td>登錄程序</td>
</tr>
<tr>
<td>
用戶id:
</td>
<td>
<input type="text" name="userid">
</td>
</tr>
<tr>
<td>
密 碼:
</td>
<td>
<input type="password" name="password">
</td>
</tr>
<tr>
<td>
記住密碼?
</td>
<td>
<select name="savetime">
<option value="0" selected>不保存</option>
<option value=<%=24*60*60%>>一天</option>
<option value=<%=24*60*60*7%>>一周</option>
</select>
</td>
</tr>
<tr>
<td> <input type="submit" value="確認(rèn)"></td>
<td> <input type="reset" value="重置"></td>
</tr>
</table>
</form>
check.jsp
[c-sharp] view plaincopy
<%@ page import="java.sql.*"%>
<%!
public static final String DBDRIVER = "org.gjt.mm.mysql.Driver" ;
public static final String DBURL = "jdbc:mysql://localhost:3306/jspDemo" ;
public static final String DBUSER = "root" ;
public static final String DBPASS = "root" ;
%>
<%
Connection conn = null ;
PreparedStatement pstmt = null ;
ResultSet rs = null ;
boolean flag = false ; // 表示登陸成功或失敗的標(biāo)記
%>
<%
String userid = request.getParameter("userid") ; // 接收表單參數(shù)
String password = request.getParameter("password") ; // 接收表單參數(shù)
String savetime=request.getParameter("savetime");
System.out.println(savetime);
try{
Class.forName(DBDRIVER) ;
conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS) ;
String sql = "SELECT id FROM tuser WHERE id=? AND password=?" ;
pstmt = conn.prepareStatement(sql) ;
pstmt.setString(1,userid) ;
pstmt.setString(2,password) ;
rs = pstmt.executeQuery() ;
if(rs.next()){
// 如果有內(nèi)容,則此處執(zhí)行,表示查詢出來,合法用戶
flag = true ;
session.setAttribute("userid",userid);
//將驗(yàn)證信息保存到Cookie
Cookie cid=new Cookie("userid",userid);
Cookie cpass=new Cookie("password",password);
cid.setMaxAge(Integer.parseInt(savetime));
cpass.setMaxAge(Integer.parseInt(savetime));
response.addCookie(cid);
response.addCookie(cpass);
}
}catch(Exception e){
}finally{
try{
conn.close() ; // 連接一關(guān)閉,所有的操作都將關(guān)閉
}catch(Exception e){
e.printStackTrace();
}
}
%>
<%
if(flag){ // 登陸成功,應(yīng)該跳轉(zhuǎn)到success.jsp
%>
<jsp:forward page="success.jsp"/>
<%
}else{ // 登陸失敗,跳轉(zhuǎn)到failure.jsp
%>
<jsp:forward page="failure.jsp"/>
<%
}
%>
CookieCheck.jsp
[java] view plaincopy
<%@ page contentType="text/html;charset=GBK"%>
<%@ page import="java.sql.*"%>
<%!
public static final String DBDRIVER = "org.gjt.mm.mysql.Driver" ;
public static final String DBURL = "jdbc:mysql://localhost:3306/jspDemo" ;
public static final String DBUSER = "root" ;
public static final String DBPASS = "root" ;
%>
<%
Connection conn = null ;
PreparedStatement pstmt = null ;
ResultSet rs = null ;
boolean flag = false ; // 表示登陸成功或失敗的標(biāo)記
%>
<%
if(session.getAttribute("userid")==null){
Cookie[] c=request.getCookies();
String userid=null;
String password=null;
if(c!=null){
for(int i=0;i<c.length;i++){
if("userid".equals(c[i].getName())){
userid = c[i].getValue() ; // 接收Cookie信息
}
if("password".equals(c[i].getName())){
password = c[i].getValue() ; // 接收Cookie信息
}
}
if(userid!=null&&password!=null){
try{
Class.forName(DBDRIVER) ;
conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS) ;
String sql = "SELECT id FROM tuser WHERE id=? AND password=?" ;
pstmt = conn.prepareStatement(sql) ;
pstmt.setString(1,userid) ;
pstmt.setString(2,password) ;
rs = pstmt.executeQuery() ;
if(rs.next()){
// 如果有內(nèi)容,則此處執(zhí)行,表示查詢出來,合法用戶
flag = true ;
session.setAttribute("userid",userid);
}
}catch(Exception e){
}finally{
try{
conn.close() ; // 連接一關(guān)閉,所有的操作都將關(guān)閉
}catch(Exception e){
e.printStackTrace();
}
}
}
}
}
%>
succss.jsp
[java] view plaincopy
<%@ page contentType="text/html;charset=GBK"%>
<jsp:include page="CookieCheck.jsp"/>
<%
if(session.getAttribute("userid")!=null){
%>
<h1>登陸成功,歡迎光臨!</h1>
<%
}else{
%>
<h1> 您還未登錄!</h1>
<%
}
%>
failue.jsp
[xhtml] view plaincopy
<%@ page contentType="text/html;charset=GBK"%>
<h1>登陸失敗,請(qǐng)重新<a href="login.html" mce_href="login.html">登陸</a></h1>

TA貢獻(xiàn)1786條經(jīng)驗(yàn) 獲得超13個(gè)贊
添加回答
舉報(bào)