1

I have a JSF app on GlassFish. It's designed like so:

user.xhtml > UserHandler.java > UserService.java > UserDAO.java > database

I know that in the UserHandler, I can get the logged in user name using the FacesContext.

My question is, inside the UserService (which is decoupled from the JSF view) how can I get the name of the logged in user without using FacesContext?

I don't want to reference FacesContext from inside the service because it's possible in the future that the service will be accesses from a webservice or alternate view technology.

Alternately, I can pass in the name of the logged in user to each method in the Service, but I prefer not to do that either just because it seems a little kludgy.

Any suggestions are greatly appreciated! Rob

1 Answers1

0

javax.servlet.http.HttpServletRequest#getRemoteUser

Travis Stevens
  • 2,198
  • 2
  • 17
  • 25
  • Thanks, but how do I get a reference to the HttpServletRequest from inside my UserService? –  Feb 16 '11 at 13:04
  • I was thinking about this and how you didn't want to pass in the name of the logged in user each time. However, I think it is fairly standard to maintain a user object in session scope and pass that along to each method. Services that magically grab data tend to be harder to debug. I know that I would use dependency injection to instantiate the UserService to set it up for use in the context of the application, but then pass along everything that is needed in the context of a page request. If you have a lot of parameters that you are passing, bind those up into a data object. – Travis Stevens Feb 16 '11 at 16:17
  • In short, there is not a way to get the user name from within the UserService if you want the UserService to be decoubpled from the Faces context (which would imply decoupling from the HttpServletRequest). – Travis Stevens Feb 16 '11 at 16:24