简体   繁体   中英

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

I have a login page named login.jsp for the customer to login.

The checklogin.jsp page where I am checking the user id and password to enter into the customer UI.

I want to have user the username and other details in my servlet.

How can I get this details in servlet page? The JSP and servlet are simple registration form. I want username and details who is filling the form.

once you know the user data store it as an attribute: for ex:

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

then try use this command:

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

use this code to watch all attributes on console page:

<%=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);}%>    

cheers :)

suppose you need to store the details to the session in login servlet.

// get the session, add argument true to create a session if one is not yet created.

HttpSession session = request.getSession(true);

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

Use this code to get the details in the checkLogin servlet.

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

you can do something like this in your jsp.get username and password from related components and set them in session.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM