Reputation: 331
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.
Upvotes: 1
Views: 8324
Reputation: 4397
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
Upvotes: 2
Reputation: 3325
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.
Upvotes: 0