2012-03-09 86 views
1

我是JSP web開發的初學者。我有一個登錄(Login.jsp)頁面爲我的webapp有標準的登錄控制(例如,用戶名,密碼文本框,記住我複選框和登錄按鈕)我想達到的是當用戶提供憑證,詳細信息,將調用Authenticate.jsp,其中憑據與數據庫匹配,如果它們正確,則會創建該用戶的會話並且憑據不正確。我將用戶重定向回用response.sendRedirect("./Login.jsp?status=fail");在JSP中實現登錄頁面的優雅方式

在的Login.jsp到登錄頁面,所述status參數始終檢查,並且當它給出值fail我有<div>具有消息User Name or Password Incorrect!文本框的下方,是在這樣的故障顯示給用戶。通過URL發送這些參數是否安全(爲了給定的目的)?什麼是更合適或標準的方式來實現這樣的事情?另外,當用戶第一次訪問登錄頁面時,尋找status參數將拋出NullPointerException。我有完整的Login.jsp代碼來檢查參數如下。

try 
    { 
     String stat = request.getParameter("Status"); 
     if(stat.equalsIgnoreCase("fail")) 
      msg = "Invalid Username or Password!"; 
     else 
      msg = " "; 
    } 
    catch(NullPointerException e) 
    { 
     msg = " "; 
    } 
    catch(Exception e) 
    { 
     out.println(e); 
    } 

msg字符串對象正如我前面提到的寫於<div>。因此,如果找不到status參數,則認爲用戶第一次訪問該頁面,因此不會顯示錯誤憑據的消息。

我感覺這可能不是如何在實際系​​統中實現的方式。

糾正我。

謝謝。

注意:在網絡上搜索同一個查詢時,他們中的大多數在登錄失敗時都有不同的JSP頁面,而我只想保留單個頁面。

回答

1

在進行專業開發時,您使用安全框架會更好。 Apache Shiro & Spring Security非常適合您的項目實施安全。

在你的代碼
+0

我經歷了Apache Shiro的概述,我一定會試試看。但是我正在尋找一種可以在沒有任何第三方框架的情況下使用的解決方案,只需要使用Java EE API就可以完成其他任務。 – Kushal 2012-03-09 14:51:17

+0

如果你沒有使用框架,你最終會爲真實世界的應用程序做很多工作 – 2012-03-09 14:53:33

1

這裏String stat = request.getParameter("Status");

stat永遠是NULL當過Login.jsp被訪問。這不是實現這個的正確方法。

總是建議實施MVC模式,您有JSP用於演示和查看目的。 Servlets重定向請求和Beans作爲模型。

下面是一些更多的解釋...

1)分離的擔憂(SoC)技術。讓你的視圖(jsp)處理渲染,Controller(servlet)處理應用程序的流程,並在另一組類中處理所有的處理。這將是MVC的基礎。以下是簡單佈局的維基百科定義。

Model - The model is a collection of Java classes that form a software application 
intended to store, and optionally separate, data. A single front end class that can 
communicate with any user interface (for example: a console, a graphical user interface, 
or a web application). 


View - The view is represented by a Java Server Page, 
with data being transported to the page in the HttpServletRequest or HttpSession. 

Controller - The Controller servlet communicates with the front end of the model and loads the HttpServletRequest or HttpSession with appropriate data, 
before forwarding the HttpServletRequest and Response to the JSP using a RequestDispatcher. 

您應該創建一個該被從Login.jsp所謂的Servlet,然後你從請求參數,然後檢查credentials.Then執行像驗證憑證後,在保持會話的任務。如果輸入錯誤的憑證,則從servlet重定向到Login.jsp,如果用戶通過身份驗證,則返回其他頁面。

+0

你可以提供源代碼,以便我可以詳細瞭解應該如何實現它? – Kushal 2012-03-09 14:46:58