• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Capture the name of the login user from the system in Eclipse

 
wadha alketbi
Ranch Hand
Posts: 53
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good morning,
I have question about how I can capture the login user from the system automatic when he/she login to the system. For example, I have a system that required from the user to login after that the user will add any record to the DB, in the DB there is column that save the username of the user so, the record will be created by that user. Instead of the user enter his/her name the system will capture it automatic.

Do you have any idea or link that will help me to do this task? I'm using JSF in Eclipse.

Thank you in advance.
 
M Khalid
Ranch Hand
Posts: 45
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good morning,

you may use JavaBeans or expression Language in JSF for storing Username during the session.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wadha alketbi wrote:Do you have any idea or link that will help me to do this task? I'm using JSF in Eclipse.

Have a look at the System Properties available on the Java platform.
 
Tim Holloway
Saloon Keeper
Posts: 18359
56
Android Eclipse IDE Linux
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eclipse is an IDE. If you are wishing to find the userid of a webapp user in a JSF webapp, Eclipse has nothing to do with it. Eclipse will not be running in the production server and therefore will not be able to help.

Any J2EE webapp that wishes to know the webapp user's ID can obtain it from the HttpServletRequest using the getRemoteUser() method. Providing that the webapp in question is using J2EE standard security and not some "clever" person's do-it-yourself login system.

It can be a little hard to obtain the actual HttpServletRequest object in JSF, since JSF is supposed to be platform independent and thus theoretically able to run in non-web environments. However, it's not that hard and what I personally do is provide a :"JSFUtils" service class where I put this kind of code so that I don't have to splatter platform-specific code all over my JSF webapp. If I want the userID, I just invoke my JSFUtils.getUserId() method.

For details on how to obtain the HttpServletRequest object in a JSF web application, ask in the JSF forum.

One other note: The webapp login user id is often not the same as the LAN userID. If I was to connect to your webapp from my Linux desktop, I would not have a Windows LAN user ID at all on your LAN, but I would expect to have a webapp user ID to log into.
 
wadha alketbi
Ranch Hand
Posts: 53
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all.

Now, I have java class "Userinfo" which is contain the userId and userName. Also, I have 2 Managed or back bean: one is "loginBean", it has "doLogin" method and the second one is "DetailsBean", it has "doAdd" method.
In the loginBean, I Put these lines of code to set the user info from the session:



what I want now is when I do the add, in the doAdd method I want to get the userId:



I'm trying to do like this but it dose not work with me:





The addDir method takes 2 parameters, the second parameter is int which is the userId.

I hope you understand me.
Thank you in advance.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wadha alketbi wrote:I'm trying to do like this but it dose not work with me:

And what's not working? Do you get an exception? Is the user id not populated? Please TellTheDetails.
 
wadha alketbi
Ranch Hand
Posts: 53
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do this:
Delegate.addDir(addDir, session.setAttribute("userInfo", userInfoDto.getUserId());

There is an error that tell me, it can not cast object to int. But I use Integer.pars and I got the same error. I do not know if what I'm doing is right or not in order to pass the logged-in user Id.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wadha alketbi wrote:I do this:
Delegate.addDir(addDir, session.setAttribute("userInfo", userInfoDto.getUserId());

As far as I know the setAttribute() method of HttpSession has void as a return type. And you try to use its return type in another method call, which is simply impossible and will result in a compiler error.
 
wadha alketbi
Ranch Hand
Posts: 53
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I put that in a separate method but it dose not work with me ):
Any comments!
 
Paul Clapham
Sheriff
Posts: 21558
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Show us the code which actually compiles and explain what exactly happens when it doesn't work.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wadha alketbi wrote:I put that in a separate method but it dose not work with me ):
Any comments!

So you expect us to be able to see what's happening on your computer? If you don't provide any details about the code you are using, what you are expecting and what's going wrong; there is no way we could help you! Maybe you should first have a read of HowToAskQuestionsOnJavaRanch so your chances of getting a satisfying answer will increase drastically.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic