How can I get j_username on my index.jsp after successful authentication with j_security_check?

3.5k views Asked by At

I'm using j_security_check on a login.jsp. The server is GlassFish Server 3. It all works, when the user is authenticated it then opens index.jsp. My problem is I need to get j_username in my index.jsp, but I couldn't find a way of doing it. All solutions I found are in Java and I need something that works with my jsp.

Any ideas? Thank you very much in advance!

1

There are 1 answers

0
BalusC On

The involved request is in JSP EL available by PageContext#getRequest(). The logged-in user is available by HttpServletRequest#getUserPrincipal(). The username is in turn available by Principal#getName().

So,

<p>Welcome, <c:out value="${pageContext.request.userPrincipal.name}" /></p>

should do.

Using <c:out> is by the way not necessary, but useful for the case that the username could contain special HTML characters which could malform the HTML output like <, >, " and so on (which is a source for XSS attacks). The <c:out> just escapes them so that they get displayed literally instead of being interpreted as part of HTML markup.