aspose file tools*
The moose likes Servlets and the fly likes parsing the user-agent to get OS version Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "parsing the user-agent to get OS version" Watch "parsing the user-agent to get OS version" New topic
Author

parsing the user-agent to get OS version

Mahendran Aiyappan
Ranch Hand

Joined: Sep 05, 2005
Posts: 102
Hi All,

I need to get the OS version and Browser Version from the user-agent HTTP header. Is there any standard API which should parse it and return? One more question. What is OS_TEXT, OS_CODE, BROWSER_TEXT, BROWSER_CODE in this context?

Thanks for your suggestions,

Mahendran
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

javax.servlet.http.HttpServletRequest contains methods to get the headers. Not sure what those values are, where did you come accross them?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Mahendran Aiyappan
Ranch Hand

Joined: Sep 05, 2005
Posts: 102
Paul,

Through HttpServletRequest I could only get the user agent string, But I require browser version and os version which any way is part of user_agent. But my question is there any method which will return something like this



Thanks,
Mahendran.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

No. But if you have the User-Agent header you've got all you need don't you? The HTTP specification only requires a list of product/product-version tokens, it doesn't specify what those should be so there is nothing the Servlet specification can do to parse it.
Mahendran Aiyappan
Ranch Hand

Joined: Sep 05, 2005
Posts: 102
Paul,

Is there any other standard API other than Servlet specification to do the parsing and return accordingly(Browser Version, Os Version etc)?

Thanks,
Mahendran.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

There may be utility classes out there - I don't know any.

This may be a futile excercise anyway since some browsers do not follow the standard and others deliberately spoof this header (Opera for example can be configured to pretend it is IE). Why do you need to know the browser? What do you need to do with this information?
Mahendran Aiyappan
Ranch Hand

Joined: Sep 05, 2005
Posts: 102
Paul,

In the database there is field to store the browser version, os version. I have requested to fill that data. That's it. No idea about why are we storing it.

Thanks,
Mahendran
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

In which case could you not just change the database table and store the contents of the User-Agent header itself? Then whatever needs to report on this can do the logic to workout what the data means.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42276
    
  64
I suggest to find out why this is done. Knowing that generally allows someone to make more intelligent decisions about their work.

Much good information on the subject of browser detection can be found at http://www.texsoft.it/index.php?c=software&m=sw.php.useragent&l=it, and if you search for "java browser detection source code" or something like that I'm sure you can find lots of ready-to-use code.


Ping & DNS - my free Android networking tools app
Mahendran Aiyappan
Ranch Hand

Joined: Sep 05, 2005
Posts: 102
Paul and Ulf,

Is this looks alright?



Thanks,
Mahendran.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42276
    
  64
That looks waaaaaaay obsolete. No Firefox? No IE 7 and 8? No XP? No Opera? No Safari? No Vista? And who cares about Netscape 4.0 vs. Netscape 4.7 these days?

Also note that the UA string can contain both "MSIE 5" and "Mozilla", a case not being handled by that code.

I can only repeat: You need to understand WHY this information is being collected; only then can you make informed decisions about what is and is not important.

Paul's suggestion also is a good one. If you save the UA string in full, then you can later dissect it as required (even as the requirements, and your understanding of UA strings, changes) in addition to doing any analysis on the spot.
Mahendran Aiyappan
Ranch Hand

Joined: Sep 05, 2005
Posts: 102
Ulf,

Currently I am handling those missing as you pointed out. But I will get more information regarding this and I will ask to make a database change if possible.

Thanks,
Mahendran.
Priyanka Dandekar
Ranch Hand

Joined: Aug 06, 2008
Posts: 52
For benefit of others, here is what I found up to date as per todays browser needs. Still doesnt talk about the OS and other details but I guess can be built.
Detecting User-agent for a browser

Have you seen Google Analytics report about the type of browsers and Operating systems accessing a web page. Thats possibly one of the reasons why you would want to store user-agent.

We are also storing user-agent for similar statistics, it gives more details about the type of users accessing your web site and based on that you can decide some future strategies.

I hope this helps.


Priya - Mockup Tool | Java | Struts2 | Lucene
Priyanka Dandekar
Ranch Hand

Joined: Aug 06, 2008
Posts: 52
One more thing I forgot to mention is, we are storing the User-agent field in a single database field. The logic for parsing the different field values need not be their on the storing application as their can be many applications logging the same information . Only the reporting module needs to be able to understand and parse the user-agent.

Imagine a scenario when you have used this in 5 different applications and a new browser version comes up. If the logic of parsing is present on reporting side then you need to just upgrade the reporting module not all 5 applications.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: parsing the user-agent to get OS version