Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get login attributes from a servlet/jsp

Lately I've been working on implementing security for my web application, running on a Glassfish v3. I successfully managed to secure some resources by setting a basic authentication up like following:

<login-config>
<auth-method>BASIC</auth-method>
<realm-name>vcards-admin</realm-name>
</login-config>

Now I was wondering how to get the user name introduced on the login prompt to fecth the actual data of the user. I thought there could be a session attribute to get that piece of data, but I don't know which one it is.

Am I wrong about the session attribute? Is there any other way to access that login information?

Thanks in advance.

like image 812
mdelolmo Avatar asked May 12 '11 10:05

mdelolmo


People also ask

How to get values from servlet in JSP?

1) First create data at the server side and pass it to a JSP. Here a list of student objects in a servlet will be created and pass it to a JSP using setAttribute(). 2) Next, the JSP will retrieve the sent data using getAttribute(). 3) Finally, the JSP will display the data retrieved, in a tabular form.

How to pass list from JSP to servlet?

ArrayList lst = new ArrayList(); lst. add("test1"); lst. add("test2"); session. setAttribute("list", lst);

In which of the following cases the request getAttribute () will be helpful?

getAttribute() is for server-side usage only - you fill the request with attributes that you can use within the same request. For example - you set an attribute in a servlet, and read it from a JSP. Can be used for any object, not just string.


1 Answers

It's available by HttpServletRequest#getUserPrincipal() or its shorthand HttpServletRequest#getRemoteUser():

String name = request.getUserPrincipal().getName();
// Or
String name = request.getRemoteUser();

Equivalently in JSP EL:

${pageContext.request.userPrincipal.name}
<!-- or -->
${pageContext.request.remoteUser}
like image 85
BalusC Avatar answered Oct 13 '22 00:10

BalusC