• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Client browser type and platform detection thru Servlets

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I want to know the way in which I can detect a client's browser type and platform(Mac or non-Mac)through the request header object. Also, is there any server side way (non-javascript)to detect these without cumbersome and independable string parsing.
TIA
regards,
vishal.
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
there are but I have never really investigated that. here are a couple links you might find interesting
http://www.wdvl.com/Authoring/HTML/Tutorial/http.html
http://www.ics.uci.edu/pub/ietf/http/rfc1945.html
there is also information about getting header info at suns site
and in the Tomcat docs and examples that come with it. if you dont get a better answer I will try to find the examples
 
vishal goyal
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Randall.
I visited the web-sites suggested and found them interesting. But still, am in need of suitable examples where in a servlet, the client's system(platform) could be detected(say, from WinNT, Mac) and its browser type (MSIE5.5, NN4.75)could be detected as my response needs to be dependent on this information acquired.
Please suggest asap.
 
Randall Twede
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
vishal,
all i remember is they had examples when i downloaded Tomcat. Im sure they are still there but i just have to find them. when you download(and install) it they have a index.html with links to the examples but when you change index.html(for your own) those links go away. like i said if noone else gives you a better answer by tomorrow i will find them. they are servlets or JSP's they have examples of both
 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
request.getHeader("User-Agent"). This will return you something like the following:
Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)
You will have to do some parsing to seperate the OS from the browser, etc.
 
Randall Twede
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok this might be it im not sure


Source Code for RequestHeader Example
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class RequestHeaderExample extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
Enumeration e = request.getHeaderNames();
while (e.hasMoreElements()) {
String name = (String)e.nextElement();
String value = request.getHeader(name);
out.println(name + " = " + value);
}
}
}
[/quote

 
Randall Twede
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
of course you wont want to send the info to the response like this servlet did, but it shows the code you need
 
vishal goyal
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Thanks for the responses.
I've already tried with request.getHeader("user-agent"; but this doesn't seem like a method which can be relied upon for all types of platforms and browser types.
The values in user-agent are as:
Netscape Navigator 4.75:
user-agent : Mozilla/4.75 [en] (WinNT; U)
Internet Explorer 5.5:
user-agent : Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 4.0)
If any one has a Mac then he.she will get the set in a totally different manner.
So, I am looking for a standard way of getting this.
And also, even using request.getHeader("user-agent"), is there any standard function present somewhere which can be used to handle most of the majorly used platform and browser types.
Thanks.
 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take a look at this example here.
Execute it! http://objectpro.surfnetusa.com/jsp-heaven/opexamplejsps/snoop/supersnoop.jsp
Source Code! http://objectpro.surfnetusa.com/jsp-heaven/opexamplejsps/snoop/supersnoop_jsp.htm
Hope this will help you.
Take care,
m, phx
 
vishal goyal
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please let me know what do we get as user-agent value on a Mac system.
To see the values, please visit this site: http://objectpro.surfnetusa.com/jsp-heaven/opexamplejsps/snoop/supersnoop.jsp
Please cut and paste the value which you are getting on a Mac system with IE and Netscape Navigator as browsers.
Thanks.
regards,
vishal.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For a mac machine with IE the User-Agent value is
Mozilla/4.0 (compatible; MSIE 4.5; Mac_PowerPC)
For the same Mac machine under Netscape you get this:
Mozilla/4.73 (Macintosh; U; PPC)
Does anyone know what the three value in the parenthesis represent?
 
mark weitz
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm still searching for info on what the user agent contents represent but as best as I can figure at this point, the contents are something like:
appname/appversion (platform; security; os-cpu [; language] [;misc])
where:
appname always Mozilla (I haven't found otherwise)
appversion seems to be 4.0 for IE and 4.x for the version of
Netscape you are running.
Within the (), however, there doesn't seem to be a rhyme or reason for its contents. Some of the variations I have seen include:
(compatible; MSIE 4.5; Mac_PowerPC) - on a PowerMac with IE
(Macintosh; U; PPC) - on a PowerMac with Netscape
(compatible; MSIE 5.01; Windows NT) - on NT with IE
(WinNT; U; Nav) - on NT with Netscape 4.03
(WinNT; U) - on NT with Netscape 4.78

The best I've come up with is that you will always find 'Win' for a windows platform (will also find 95, 98, or NT) or 'Mac' for a Macintosh platform.
If you see MSIE you are running Internet Explorer. If not, I'm assuming you are under Netscape. (I don't have any other browsers so that's the assumption I have to make -- clearly it's wrong).
Here is some code I have to detect a Mac and IE platform/browser because I have a problem that only occurs under that environment.
String userAgent = request.getHeader("User-Agent");
boolean isMacIE = false;
if ( (userAgent.indexOf("Mac") > -1) &&
(userAgent.indexOf("MSIE") > -1) ) {
isMacIE = true;
}
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic