Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 640
Monica Avatar asked Feb 18 '11 15:02

Monica


1 Answers

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.

like image 118
BalusC Avatar answered Oct 15 '22 01:10

BalusC