aspose file tools*
The moose likes JSP and the fly likes Accesing request.getRemoteUser() using EL expressions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Accesing request.getRemoteUser() using EL expressions" Watch "Accesing request.getRemoteUser() using EL expressions" New topic
Author

Accesing request.getRemoteUser() using EL expressions

Krzysiek Hycnar
Ranch Hand

Joined: Jan 02, 2004
Posts: 74
Hi all,

Is there an elegant way to access information about username and possibly roles assigned to him using EL expressions??

In my JSPs I use conditionals to display different page contents to different users. The best solution to me would be something like this...

<c:choose>
<c:when test="${sessionScope.remoteUser==null}">
page content for user that has not logged in yet...
</c:when>
<c therwise>
Hello <c ut value="${sessionScope.remoteUser}"/> etc...
</c therwise>
</c:choose>

However the problem is that there is no sessionScope.remoteUser after the user has logged in (I know requestScope.remoteUser looks like it might work but it does not).

To make the above JSP work, a following scriptlet must be added before the JSTL conditionals:

<%
String user = request.getRemoteUser();
if(user!=null && request.getSession(false)!=null && session.getAttribute("remoteUser")==null)
session.setAttribute("remoteUser", user);

%>

How can I achieve my goal without that scriptlet??
Again the finnset solution would be if the code setting the remoteUser attribute ran only once, just after the user has successfuly loged in.

Is there a way to intercept the authentication procedure and smuggle some custom code there (for now I use form based authentication with JDBC Realm configured) ??

Thanx in advance for your suggestions.

Regards
Chris
Krzysiek Hycnar
Ranch Hand

Joined: Jan 02, 2004
Posts: 74
> (I know requestScope.remoteUser looks like it might work but it does not).
Of course it's not supposed to work!! It's not a parameter neither an attribute <ashamed>
Chris
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Probably unnecessary: I guess you figured out ${request.remoteUser} then?

- Peter
rick zorich
Greenhorn

Joined: Aug 31, 2003
Posts: 16
Did you try ?
${pageContext.request.remoteUser}
Krzysiek Hycnar
Ranch Hand

Joined: Jan 02, 2004
Posts: 74
${pageContext.request.remoteUser} works!!
Thanx Rick
Chris

PS
Where can I read more about JSTL and EL expressions??
rick zorich
Greenhorn

Joined: Aug 31, 2003
Posts: 16
I got that out of "JSTL in Action" by Shawn Bayern.
But it only covers JSTL 1.0.
Does anybody have a book for JSTL 1.1 ?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61413
    
  67

When I was learning JSTL, I kept a copy of this book handy. You can read about it in our JavaRanch Bunkhouse.

Once you understand how the EL interprets references, creating constructs like the above become quite second nature.
[ August 29, 2004: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
rick zorich
Greenhorn

Joined: Aug 31, 2003
Posts: 16
Bear, that book is a year old already.
What about JSTL 1.1 ?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61413
    
  67

Well, you could always do what I do and go straight to the specification. There's nothing quite like a peek into the horse's mouth.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Accesing request.getRemoteUser() using EL expressions