Ashish
Ashish

Reputation: 87

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.

Upvotes: 0

Views: 12350

Answers (3)

Rajith Pemabandu
Rajith Pemabandu

Reputation: 614

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");

Upvotes: 0

UMichaeli
UMichaeli

Reputation: 26

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 :)

Upvotes: 1

Leon
Leon

Reputation: 198

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

Upvotes: 0

Related Questions