Problem in getting windows username

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tiijnar
    New Member
    • Nov 2008
    • 14

    Problem in getting windows username

    Hi,

    To get windows username Im using the following code.
    Code:
    public class GetWindowUsername {
    public String getUser() {
    String userName = System.getProperty("user.name");
    System.out.println("Window's Username: "+userName);
    return userName;
    }
    }
    
    public class TestUserName {
    public static void main(String args[]) {
    GetWindowUsername g = new GetWindowUsername();
    String s = g.getUser();
    System.out.println("UserName : "+s);
    }
    }
    If I execute the above java class I get the windows username correct.

    But when I use it in jsp like-
    Code:
    <% 
    String s = System.getProperty("user.name");
    out.println("Windows Username : "+s);
    %>
    I get the ouput as-
    " Windows Username : SYSTEM "

    I tried to call the "getUser()" in jsp using an object of "GetWindowUsern ame"
    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
    Last edited by Nepomuk; Dec 19 '08, 05:14 PM. Reason: Please use [CODE] tags
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Does System.getenv(" USERNAME") help?

    kind regards,

    Jos

    ps. I deleted your other identical thread.

    Comment

    • tiijnar
      New Member
      • Nov 2008
      • 14

      #3
      Still facing problem

      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

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by tiijnar
        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.
        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

        Comment

        • tiijnar
          New Member
          • Nov 2008
          • 14

          #5
          In need of new solution

          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.getPrope rty("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

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            It's USERNAME in capital letters although I don't think that matters much on a Windows box.

            kind regards,

            Jos

            Comment

            • tiijnar
              New Member
              • Nov 2008
              • 14

              #7
              How to get windows username

              Hi,

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

              <%
              String s1 = System.getPrope rty("user.name" );
              out.println("Th e Username : "s1" ");
              String s2 = System.getenv(" USERNAME");
              out.println("Th e 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

              Comment

              • Dököll
                Recognized Expert Top Contributor
                • Nov 2006
                • 2379

                #8
                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

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  threads merged

                  @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

                  Comment

                  Working...