Detecting Device Type in a web application
You can get device information by parsing http header
String browserType = request.getHeader("User-Agent");
You should parse browserType to get device type
This may help
public String getBrowserInfo( String Information )
{
String browsername = "";
String browserversion = "";
String browser = Information;
if (browser.contains("MSIE"))
{
String subsString = browser.substring(browser.indexOf("MSIE"));
String info[] = (subsString.split(";")[0]).split(" ");
browsername = info[0];
browserversion = info[1];
} else if (browser.contains("Firefox"))
{
String subsString = browser.substring(browser.indexOf("Firefox"));
String info[] = (subsString.split(" ")[0]).split("/");
browsername = info[0];
browserversion = info[1];
} else if (browser.contains("Chrome"))
{
String subsString = browser.substring(browser.indexOf("Chrome"));
String info[] = (subsString.split(" ")[0]).split("/");
browsername = info[0];
browserversion = info[1];
} else if (browser.contains("Opera"))
{
String subsString = browser.substring(browser.indexOf("Opera"));
String info[] = (subsString.split(" ")[0]).split("/");
browsername = info[0];
browserversion = info[1];
} else if (browser.contains("Safari"))
{
String subsString = browser.substring(browser.indexOf("Safari"));
String info[] = (subsString.split(" ")[0]).split("/");
browsername = info[0];
browserversion = info[1];
}
return browsername + "-" + browserversion;
}
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
}
}
You could get a 3rd party software solution. There are plenty of Open Source ones out there. I've used 51Degrees.mobi's Java solution before now (and have also worked on their open source C solution). Follow that link and hit the download button. It's relatively easy to get up and running.