0

I deployed my servlet in server using tomcat. It basically have some input fields and save changes button. When someone writes something and click save I want to save changes to DB along with the PC name and logged user name of the client. Everything is done till updating PC name and Username of client. I can get PC name by InetAddress inetAddress = InetAddress.getByName(request.getRemoteAddr);String PCName = inetAddress.getHostName(); How can I get username? I tried many things such as request.getRemoteUser()(returns null, request.getAuthType() also returns null. How to change auth type?) and also tried System.getProperty("user.name")(returns server username where as I need client username)

By username I mean the username by which logged in to PC.

  • @SamuelJMathew He has already done exacly that. He is *asking* for working code. – user207421 Jan 06 '17 at 09:16
  • So to make it clear, you are talking about getting the username with which the user logged into his PC, right? Or does your web application have its own authentication mechanism and you want the username that was input there? – Gimby Jan 06 '17 at 09:34
  • @Gimby , yes am interested in the user logged into the PC from which my servlet is called. – jayachandra chillipuri Jan 06 '17 at 09:57
  • The abovelinked duplicate elaborately explains your misunderstanding of how this stuff works. If you understood that you're actually looking for "SSO", then you would have found this duplicate: http://stackoverflow.com/q/4590227 – BalusC Jan 06 '17 at 10:26

1 Answers1

-1

HttpServletRequest.getUserPrincipal() returns the current user principal, if there is one, which contains the username: or null if there isn't a current user.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • @Gimby Only if you haven't read the question. It is about servlets and the 'logged user name of the client'. Clients of servlets are at the other end of an HTTP session, and the method I mentioned provides the user principal of that session. The words 'operating system' are your own invention: they do not appear in the question. – user207421 Jan 06 '17 at 09:17
  • Ive added following line in my code `Principal p = request.getUserPrincipal(); System.out.println("PrincipalUser : " + p.getName());` It shows me java.lang.NullPointerException at `request.getUserPrincipal()` [INFO : request is not null, Ive verified] – jayachandra chillipuri Jan 06 '17 at 09:23
  • @jayachandrachillipuri So, obviously, `request.getUserPrincipal()` returned null, under the conditions which both my answer and the link it refers to carefully specified. There is no currently authenticated user. You need to check for that condition. You really shouldn't need all this help to debug a simple NPE. – user207421 Jan 06 '17 at 09:24
  • more about the error : `Jan 06, 2017 1:09:41 PM org.apache.catalina.core.StandardWrapperValve invoke SEVERE: Servlet.service() for servlet [GetDataSales] in context with path [/MISHWeb] threw exception java.lang.NullPointerException at GetDataSales.doGet(GetDataSales.java:42) at javax.servlet.http.HttpServlet.service(HttpServlet.java:620) at javax.servlet.http.HttpServlet.service(HttpServlet.java:727) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)` and so on... – jayachandra chillipuri Jan 06 '17 at 09:24
  • Assuming you're talking about windows clients, you could check out NTLM. – Chris M Jan 06 '17 at 09:31
  • yes am getting null in return @ChrisM, but I am trying myself from a different PC with XXX user logged in, still your method returns null. This seems not working. Do you have any other working method – jayachandra chillipuri Jan 06 '17 at 09:40
  • @ChrisM I never heard of NTLM, can you provide any snippet or example so that it works for me. Thanks – jayachandra chillipuri Jan 06 '17 at 09:41
  • @jayachandrachillipuri Please answer the question you were asked. Are you talking about the client user's operating system login, or some authentication step that he has/hasn't performed directly into your webapp? – user207421 Jan 06 '17 at 09:45
  • Yes am interested in clients user's os login name and pc name – jayachandra chillipuri Jan 06 '17 at 09:46
  • In that case you need to investigate (1) Container Managed Authentication at the Tomcat end, (2) JAAS ditto, and (3) the NTLM login module for JAAS. After implementing all that, the technique in my answer above will work. – user207421 Jan 06 '17 at 09:47
  • well am beginner to each and every topic you mentioned. can you elaborate or provide a link which justifies your answer – jayachandra chillipuri Jan 06 '17 at 09:50
  • If the process mentioned by you (@EJP) is for OS username then what would I get with `request.getRemoteUser()` and `request.getUserPrincipal()` ? – jayachandra chillipuri Jan 06 '17 at 09:53
  • @jayachandrachillipuri You need to research these things, as I already stated. Far too broad to answer in a question here, let alone a comment. NB I have *already* provided a link which 'justifies my answer'. – user207421 Jan 06 '17 at 09:56
  • @downvoter Please explain. – user207421 Jan 06 '17 at 11:12