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

こ雲淡風輕ζ 提交于 2019-12-22 06:59:10

问题


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:


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.



来源:https://stackoverflow.com/questions/5042983/how-can-i-get-j-username-on-my-index-jsp-after-successful-authentication-with-j

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!