簡體   English   中英

如何在servlet中獲取當前用戶名和其他詳細信息?

[英]How do I get current user name and other details in servlet?

我有一個名為login.jsp的登錄頁面,供客戶登錄。

在checklogin.jsp頁面中,我正在其中檢查要輸入到客戶UI的用戶ID和密碼。

我想在我的servlet中為用戶提供用戶名和其他詳細信息。

如何在servlet頁面中獲取此詳細信息? JSP和Servlet是簡單的注冊表單。 我要填寫用戶名和詳細信息。

知道用戶數據后,將其存儲為屬性:例如:

<%=request.getSession().setAttribute("currentUser", username);%>

然后嘗試使用以下命令:

<%= request.getSession().getAttribute("currentUser"); %>

使用此代碼查看控制台頁面上的所有屬性:

<%=Session session = request.getSession();
Enumeration attributeNames = session.getAttributeNames();
while (attributeNames.hasMoreElements()) {
String name = attributeNames.nextElement();
String value = session.getAttribute(name);
System.out.println(name + "=" + value);}%>    

歡呼:)

假設您需要將詳細信息存儲到登錄servlet中的會話中。

//獲取會話,如果尚未創建會話,請添加參數true以創建會話。

HttpSession session = request.getSession(true);

session.addAttribute("userName", request.getParameter("userName"));
session.addAttribute("password", request.getParameter("password"));

使用此代碼可在checkLogin servlet中獲取詳細信息。

 // to get the username and password
    String userName = session.getAttribute("userName");
    String password = session.getAttribute("password");

您可以在jsp.get中從相關組件中獲取用戶名和密碼,然后在會話中進行設置。

<%
  String username = (String)request.getAttribute("username");
  String password = (String)request.getAttribute("password");
  session.setAttribute("UserName", username);
  session.setAttribute("Password", password);
%>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM