469,356 Members | 2,055 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,356 developers. It's quick & easy.

Problem in getting windows username

14
Hi,

To get windows username Im using the following code.
Expand|Select|Wrap|Line Numbers
  1. public class GetWindowUsername {
  2. public String getUser() {
  3. String userName = System.getProperty("user.name");
  4. System.out.println("Window's Username: "+userName);
  5. return userName;
  6. }
  7. }
  8.  
  9. public class TestUserName {
  10. public static void main(String args[]) {
  11. GetWindowUsername g = new GetWindowUsername();
  12. String s = g.getUser();
  13. System.out.println("UserName : "+s);
  14. }
  15. }
  16.  
If I execute the above java class I get the windows username correct.

But when I use it in jsp like-
Expand|Select|Wrap|Line Numbers
  1. <% 
  2. String s = System.getProperty("user.name");
  3. out.println("Windows Username : "+s);
  4. %>
  5.  
I get the ouput as-
" Windows Username : SYSTEM "

I tried to call the "getUser()" in jsp using an object of "GetWindowUsername"
but even after that I got same output.

Im running the above jsp using Apache Tomcat 5.0 on my server machine.

When I run the same code on my local system using eclipse & Apache Tomcat v5.5, I get the output correct.

Can anyone solve my problem.

Thanks in advance
Tiijnar
Dec 19 '08 #1
8 12098
JosAH
11,448 Expert 8TB
Does System.getenv("USERNAME") help?

kind regards,

Jos

ps. I deleted your other identical thread.
Dec 19 '08 #2
tiijnar
14
Hi JosAH,

Thank you for the solution but I still face the problem.

I have used System.getenv("USERNAME") on my server which uses Tomcat 5.0 to run jsp, but I got the username as NULL.

Does my problem depends on the usage of Tomcat 5.0, because when I use same code in a jsp which is run using Tomcat 5.5 I get correct result.

Can I have another solution for my problem..

Thanks in advance
Tiijnar
Dec 22 '08 #3
JosAH
11,448 Expert 8TB
@tiijnar
I'd blame version 5.0 for that; I just tested it on version 6.0.18 and there were no problems there either, i.e. I just got a valid user name back.

kind regards,

Jos
Dec 22 '08 #4
tiijnar
14
Hi JosAH,

I have installed Tomcat 5.5 on my server and run the same jsp. Even then Im getting same old output. i.e
If I use System.getProperty("user.name") ---> SYSTEM
If I use System.getenv("username") ---> null

My problem is not solved yet.

Can you provide me a new solution.

Regards,
Tiijnar
Dec 23 '08 #5
JosAH
11,448 Expert 8TB
It's USERNAME in capital letters although I don't think that matters much on a Windows box.

kind regards,

Jos
Dec 23 '08 #6
tiijnar
14
Hi,

In my java application I have following code to get windows username-

<%
String s1 = System.getProperty("user.name");
out.println("The Username : "s1" ");
String s2 = System.getenv("USERNAME");
out.println("The Username : "+s2);
%>

If I log in to my PC and execute it I get correct output for both the statements, But when I execute it on server machine(when I logged in) I get the output as-

The Username : SYSTEM The Username : null

I use Apache Tomcat 5.5 to run the jsp on both (PC & server) machines. One small difference is I use eclipse on my PC and I don't use eclipse on server (I think it doesn't matter).

But when I use the same code inside a java class I get correct output on both (PC & server) machines.

Can any one solve my problem.

Thanks in advance,
Tiijnar
Dec 24 '08 #7
Dököll
2,364 Expert 2GB
Greetings tiijnar!

Just guessing, but if you are intending to get credentials of a user on a Windows domain, you'll need to use LDAP. If this is the case, please continue to search here, if nothing, try Sun Microsystems for added input.

I would tell you but it is so involved. Rest assured however, Sun has a generic code you can use;-)

Hope this is what you needed...

Good luck!

Dököll
Dec 24 '08 #8
JosAH
11,448 Expert 8TB
@OP: I've merged your identical threads; there was (and is) no need to start two separate threads for one single question. If needed you can bump your thread (by posting a small reply to it yourself).

kind regards,

Jos
Dec 24 '08 #9

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

9 posts views Thread by Bartosz Wegrzyn | last post: by
2 posts views Thread by Linda | last post: by
4 posts views Thread by Grind Boy | last post: by
13 posts views Thread by Steve Enzer | last post: by
8 posts views Thread by Bruno Barros | last post: by
7 posts views Thread by cmrchs | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.