最近在做網站的登錄功能,目前有這幾個文件:index.jsp(登錄頁面),userLogin.jsp(登錄驗證頁面,登錄成功跳到user.jsp,登錄失敗跳回index.jsp)與數據庫相連,user.jsp(用戶個人主頁)。想要達到以下目的:
1.輸入正確的用戶名和密碼,成功登錄。
2.輸入錯誤的用戶名或密碼,登錄失敗,跳回index.jsp提示登錄失敗,請重新登錄。
關於頁面的跳轉問題,在登錄成功時,要向user.jsp傳遞用戶名,登錄失敗時,要跳回原來的index.jsp頁面,並要告知index.jsp登錄失敗,提醒用戶重新登錄。我在userLogin.jsp中定義了一個變量 boolean flag=flase; 作為登錄成功或是失敗的標記。登錄失敗時將flag傳回給index.jsp頁面。
起初,我用到了jsp:forward,如下:
<jsp:forward page="index.jsp">
<jsp:param name="flag" value="<%=flag %>" />
</jsp:forward>
參數傳遞成功,頁面跳轉成功,但頁面樣式表無法加載,地址欄顯示位置仍在uerLogin.jsp,並未正真跳轉到index.jsp頁面。
之后,換用response.sendRedirect("index.jsp");只能實現界面跳轉,無法傳遞參數。於是在response.sendRedirect("index.jsp");之前加上session.setAttribute("flag",flag);用session保存登錄狀態,在index.jsp中使用session.getAttribute("flag");讀取登錄信息。
參考資料:http://blog.sina.com.cn/s/blog_4f925fc30100mt2e.html
http://blog.csdn.net/jxc0604/article/details/5685347