amr_swg
asked on
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
i want a way to get it from the servlet
Please respond ASAP such it is a critical issue for me
Thanks in advance
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() ;
}
public void doGet( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Principal myUserPricipal = request.getUserPrincipal()
String name = myUserPricipal.getName() ;
}
Your other option would be to get HttpServletRequest.getRemo teUser() 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.
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.hasMoreElement s()){
String strHeaderKey = (String)headerNames.nextEl ement() ;
out.println("HeaderName: " + strHeaderKey + "<br>");
out.println("HeaderValue: " + request.getHeader( strHeaderKey ) + "<br>");
}
You need the one called "user-agent"
e.g.
Enumeration headerNames = request.getHeaderNames() ;
while( headerNames.hasMoreElement
String strHeaderKey = (String)headerNames.nextEl
out.println("HeaderName: " + strHeaderKey + "<br>");
out.println("HeaderValue: " + request.getHeader( strHeaderKey ) + "<br>");
}
You need the one called "user-agent"
ASKER
Dear Sir
I tried both ways but it didn't work.
Please advice
I tried both ways but it didn't work.
Please advice
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?
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?
ASKER
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
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
ASKER
Any update please ,this issue is very citical for me
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.inde xOf("Win") !=-1) OSName="Windows";
if (navigator.appVersion.inde xOf("Mac") !=-1) OSName="MacOS";
if (navigator.appVersion.inde xOf("X11") !=-1) OSName="UNIX";
if (navigator.appVersion.inde xOf("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="sendOSNameToServle t()">
</body>
</html>
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.inde
if (navigator.appVersion.inde
if (navigator.appVersion.inde
if (navigator.appVersion.inde
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="sendOSNameToServle
</body>
</html>
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("osna me");
}
public void doGet( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String osName = request.getParameter("osna
}
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.Net work");
alert("Username: " + ws.UserName);
}
-----------
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.Net
alert("Username: " + ws.UserName);
}
-----------
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
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
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 ;-)
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 ;-)
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...
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...
true... I just couldn't bear to see you both putting so much effort in ;-)
Tim
Tim
>>I just couldn't bear to see you both putting so much effort in
LOL
LOL
;-)
Agreed
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.getUser
Can you be more specific?