Detecting Device Type in a web application

Rishabh Ohri picture Rishabh Ohri · Dec 15, 2011 · Viewed 79.8k times · Source

We have a Java based application where in we want to detect the device type(mobile or desktop) for the device that is sending the request.

How is it possible?

Answer

leonardoborges picture leonardoborges · Dec 15, 2011

You'll have to read the User-Agent header from the request and decide on that.

In vanilla servlet apps, a crude way of doing it is:

public void doGet(HttpServletRequest request,
                HttpServletResponse response) throws ServletException, IOException {
  if(request.getHeader("User-Agent").contains("Mobi")) {
    //you're in mobile land
  } else {
    //nope, this is probably a desktop
  }
}