繁体   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