1

I need to detect whether a request to a website is coming from desktop or mobile or tablet and then take 3 different actions accordingly. for example if request is coming from desktop , i take action 1, if request is coming from mobile , i take action 2 and if request comes from tablet then i take action 3.

I need to do this on server side using java.

Thanks.

user1633800
  • 339
  • 1
  • 7
  • 14

2 Answers2

2

You must use:

request.getHeader("User-agent");

With the returned value you can detect the browser, OS and device used by client.

You should test the returned values from different supported devices. Wikipedia can help you in understanding User-agent header

Oscar Pérez
  • 4,377
  • 1
  • 17
  • 36
0

Possible duplicate of Detecting Device Type in a web application

You rely on the User-Agent string to detect this. Some trial and error may be expected...there exists quite a few libraries to help with stuff like this by now, but a quick google didn't give me anything for java...you may have more luck searching than me. A different approach I've seen used quite a bit is having something in front (web cache/load balancer/web server) set an HTTP header based on the User-String. This is effectively just implementing the feature somewhere else, but it makes for ease and re usability in your (and others'?) code.

Community
  • 1
  • 1
bryn
  • 3,155
  • 1
  • 16
  • 15
  • Can someone please help me with an example or point me to a free java library that i can use? – user1633800 Nov 20 '13 at 18:19
  • The accepted answer in the thread I posted contains a working code example that will help you distinguish between desktop users and mobile users - should get you going! You may have to become familiar with typical User-Agent strings from at least some device types - the example simply checks whether the User-Agent contains 'Mobile', and 'Tablet' is not a widely used User-Agent keyword. That said, an Android device that is not *Mobile* is typically a tablet... – bryn Nov 20 '13 at 21:45
  • I've heard good words about WURFL before, but it's not really free: http://wurfl.sourceforge.net/ There is Apache Mobile Filter, which sets some variable for you...but then you need Apache HTTPd in front. I also found a java class here: https://code.google.com/p/mobileesp/source/browse/Java/UAgentInfo.java – bryn Nov 20 '13 at 21:50