[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2253
  • Last Modified:

How to get operating system user made the request to java servlet from the servlet it self

I have a java application where i want to get the currrent operating system user that made the request to a java servlet

i want a way to get it from the servlet

Please respond ASAP such it is a critical issue for me

Thanks in advance

0
amr_swg
Asked:
amr_swg
  • 10
  • 3
  • 3
  • +1
1 Solution
 
bloodredsunCommented:
>>I have a java application where i want to get the currrent operating system user that made the request to a java servlet

By definition, a servlet responds to a request over HTTP not the local operating system so it would imposiible to get " currrent operating system user" is you are tyring to get the id of a user on the local system.

What you can do is require the person to be authorized and you can then get their identity using the method HttpServletRequest.getUserPrincipal() which returns a java.security.Principal object containing the name of the current authenticated user.

Can you be more specific?
0
 
bloodredsunCommented:
In a servlet, the above can be done by:

      public void doGet( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            Principal myUserPricipal = request.getUserPrincipal() ;
            String name = myUserPricipal.getName() ;
      }
0
 
bloodredsunCommented:
Your other option would be to get HttpServletRequest.getRemoteUser() which returns the login of the user making this request, if the user has been authenticated, or null if the user has not been authenticated.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
bloodredsunCommented:
If you wanted to find the OS being used by the user you can get it from the headers
e.g.

            Enumeration headerNames = request.getHeaderNames() ;
            while( headerNames.hasMoreElements()){
                  String strHeaderKey = (String)headerNames.nextElement() ;
                  out.println("HeaderName: " + strHeaderKey + "<br>");
                  out.println("HeaderValue: " + request.getHeader( strHeaderKey ) + "<br>");
            }      

You need the one called "user-agent"
0
 
amr_swgAuthor Commented:
Dear Sir

I tried both ways but it didn't work.

Please advice
0
 
bloodredsunCommented:
amr_swg ,

Please re-state your question as it's not very clear. What exactly are you trying to do?

for example...

are you trying to get the OS of the user?
are you trying to get the login name of the user for their local OS?
are you trying to get the login name of the user for your own web app?
0
 
amr_swgAuthor Commented:
Dear sir

are you trying to get the login name of the user for their local OS (windows)

i want the user once login to the operating system ,if he tried to use our web application , it automatic detect it's name and open the application directly

This issue is very important for me to be done like that ,please advice

Thank you
0
 
amr_swgAuthor Commented:
Any update please ,this issue is very citical for me
0
 
bloodredsunCommented:
I understand that english is not your first language but it is very difficult to understand what you what...

If you are trying to get the windows login of the user as they access your web application, the answer is you can't.

If you look at the headers (information about the user) sent by the browser when it makes a request for a web page or servlet
you will see that there is no information about the user.

e.g. sample headers taken from a request

HeaderName      HeaderValue
host                      localhost:8080       
user-agent           Mozilla/5.0 (Windows; U; WinNT4.0; en-US; rv:1.7.8) Gecko/20050511 Firefox/1.0.4       
accept                     text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5       
accept-language  en-us,en;q=0.5       
accept-encoding  gzip,deflate       
accept-charset    ISO-8859-1,utf-8;q=0.7,*;q=0.7       
keep-alive           300       
connection          keep-alive       
referer            http://localhost:8080/scratchpad/index.jsp       
cookie                     JSESSIONID=FA64EDB49AC6FFC488E9C620971699CD       
cache-control        max-age=0

If you are needing to authenticate your web app you should look here, at either basic or form absed authentication.

http://jakarta.apache.org/tomcat/tomcat-5.5-doc/realm-howto.html
0
 
gnoonCommented:
amr_swg,

You can use java script to get OS name of client and send it to the servlet.

<html>
<head>
<script>
<!--

  // Get OS name of client
  function getOSName()
  {
     var OSName="Unknown OS";
     if (navigator.appVersion.indexOf("Win")!=-1) OSName="Windows";
     if (navigator.appVersion.indexOf("Mac")!=-1) OSName="MacOS";
     if (navigator.appVersion.indexOf("X11")!=-1) OSName="UNIX";
     if (navigator.appVersion.indexOf("Linux")!=-1) OSName="Linux";
     return OSName;
  }

  // Get os name and send to the servlet on page loaded.
  function sendOSNameToServlet()
  {
     if(confirm("Do you want to send your os name to the server?"))
     {
        var servlet = "http://your_server/your_servlet?osname="+escape( getOSName() );
        document.location.href = servlet;
     }
  }

//-->
</script>
</head>
<body onload="sendOSNameToServlet()">
</body>
</html>
0
 
gnoonCommented:
You can get the osname from the request in the servlet by this

     public void doGet( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          String osName = request.getParameter("osname");
     }
0
 
bloodredsunCommented:
Do you not think it would be easier to get the OS name from the request header "user-agent"?

The user-agent header is sent with every request a so you don't need to go to the trouble of detecting it with JavaScript (which may be turned off) and then reading it.

And as the question has been restated as "get the login name of the user for their local OS (windows)" which means that he wants the login name not the OS, I'm not sure that your method will achieve this. It can't be acheived in JSP or JavaScript but it can be acheived in ActiveX which requires the user to have IE running and Active X enabled, both of which cannot be guaranteed.

----------
if(typeof window.ActiveXObject != "undefined") {
var ws = new ActiveXObject("WScript.Network");
alert("Username: " + ws.UserName);
}
-----------


0
 
gnoonCommented:
bloodredsun,

Yes, my way can't get the login name of the user that stored in the os.

>i want the user once login to the operating system ,if he tried to use our web application , it automatic detect it's name and open the application directly

My understand is, "it's name" means "operating system's name".

amr_swg,

You said that:
>I have a java application where i want to get the currrent operating system user that made the request to a java servlet

Did you mean that your system consists of 2 applications: java application and web application ... ?
The java app. is running on the client, and you want the java app. to get some informations of the client and send them to the servlet on your server.
Do I miss understand something?

Can you please explain more detail of what you're trying to do again, scene by scene (if posible)?

Regards,
G noon
0
 
TimYatesCommented:
I think he wants a "unified login"*

ie:  by logging into the windows machine, that login information will be passed to and checked by the webserver...

Which I don't think is possible :-(

(* I may be wrong)

Sorry for butting in ;-)
0
 
bloodredsunCommented:
Hi Tim,

I think you're right, he wants the ability to do a "single-sign on" which can't be done.

>>If you are trying to get the windows login of the user as they access your web application, the answer is you can't.

But the wording of the question is a bit ambivalent...
0
 
TimYatesCommented:
true...  I just couldn't bear to see you both putting so much effort in ;-)

Tim
0
 
bloodredsunCommented:
>>I just couldn't bear to see you both putting so much effort in

LOL
0
 
TimYatesCommented:
;-)
0
 
bloodredsunCommented:
Agreed
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 10
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now