In our Java application, we are looking to determine the type of device (mobile or desktop) that is making a request.
Is there a way to achieve this?
In our Java application, we are looking to determine the type of device (mobile or desktop) that is making a request.
Is there a way to achieve this?
To determine the device type, you need to extract the information from the User-Agent
header in the incoming request.
In traditional servlet applications, a basic approach would be:
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
if(request.getHeader("User-Agent").contains("Mobi")) {
//This is a mobile device
} else {
//This appears to be a desktop device
}
}
One way to retrieve device information is by analyzing the http header
String browserType = request.getHeader("User-Agent");
To determine the device type, you need to parse the browserType variable
This code snippet may be useful for this task:
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;
}
If you're looking to address mobile responsiveness, consider exploring Spring Mobile. This framework offers practical solutions with its user-friendly classes.
Start by retrieving the current device using DeviceUtils.getCurrentDevice(servletRequest);
if(currentDevice.isMobile()) { /* Actions for Mobile */ }
if(currentDevice.isTablet()) { /* Actions for Tablet */ }
if(currentDevice.isNormal()) { /* Actions for Desktop */ }
An alternative option is to utilize a software solution from a third-party provider. Numerous Open Source options are available for you to explore. In the past, I have utilized 51Degrees.mobi's Java solution (and have experience working on their open source C solution as well). Simply click on the provided link and download the solution. Setting it up is generally straightforward and hassle-free.
If you're looking to identify the device or software from a user agent string, I recommend checking out yauaa library.
To access the User-Agent
information, you can inspect the HTTP header within the HttpServletRequest
object.
For more details, visit: http://www.w3.org/Protocols/HTTP/HTRQ_Headers.html#user-agent
If you're working with Java, 51Degrees provides a convenient free API on GitHub that is regularly updated and designed to handle all your basic device detection needs. You can check it out here: https://github.com/51Degrees/Java-Device-Detection.
For those looking for a tutorial on how to use the API for Device Type detection, there's a helpful guide available at .
In addition, there is a cloud version offered by 51Degrees that includes the DeviceType Property (beyond just IsMobile). Details about this cloud option can be found on the 51Degrees website. Signing up for the free cloud service may be required through the website, but rest assured it is completely free and doesn't require any bank details. Depending on your specific needs, you can also explore integration options using JavaScript and other tools.
Full disclosure: I am an employee of 51Degrees.
Check out for Open Source Device Detection solutions tailored for:
This technology is built on the foundation of the W3C Device Description Repository.
When compared to similar offerings from 51Degrees, DeviceAtlas or ScientiaMobile/WURFL, OpenDDR stands out as an open source and freely accessible alternative.
Looking for a way to rotate PDF and image files displayed on an HTML page using jQuery. I attempted: adding a CSS class - without success. Here is an example code snippet: .rotate90 { webkit-transform: rotate(90deg); moz-transform: rotate(90de ...
Currently, I am attempting to download JSON data into an Excel spreadsheet. One of my requirements is to insert an image in cell a1 and then bold the headers. The code snippet below was sourced from Google; however, I need to populate the data dynamically ...
Recently, I attempted to implement a default value in a custom radio component that I developed. Below is the code snippet: <template> <div class="custom-radio-button"> {{value}} <div v-for= "item in localValue"> <input type ...
Is it possible to display a confirmation popup with "Yes" and "No" buttons every time there is a route change in my AngularJS App? I attempted the following approach. This event gets triggered before the route change, but despite selecting 'NO', ...
I'm encountering an issue with a script generated by PHP. When there is a single quote in the description, it throws a JavaScript error and considers the string terminated prematurely. print "<script type=\"text/javascript\">\n ...
I am facing an issue with a particular element that resembles a combo box (DevEx control). Unlike a typical Select tag, this element's Tag is input. This poses a challenge as the usual Select command does not work in this case. I can successfully loca ...
Having a Json text, I encountered two similar names for the desired field "localscope" {"processingTime":"0.002522", "version":"1.6.0.801 build 120621", "documentLength":"22", "document":{ "administrativeScope":{ "woeId":"23424848", "type" ...
I've utilized the Google My Tracks code found here in my application. Interestingly, while the menu bar is visible within the app itself, when integrated into my app, the menu bar becomes invisible. It's puzzling as the code remains identical. ...
Currently, I am utilizing the innerHTML attribute to modify the inner HTML of a tag. In this instance, it involves the <td></td> tag, but it could be applied to any tag: <td *matCellDef="let order" mat-cell [innerHTML]="order. ...
Is there a way to retrieve a file from the Android Assets manager without requiring context? I've come across suggestions to initialize a File object with a path starting with "file:///android_assets", but have found that it doesn't work as expec ...
Just starting to learn Node and working on building an app. Currently, in my route file, I am attempting to connect to MySQL and retrieve the major of the user, then use it for other operations. However, when I run the webpage, the console displays that t ...
Currently, I am in the process of establishing a connection between a Java client and a Python server operated by my colleague. Our immediate goal at the moment is to transmit a JSON array. In our approach, we initiate communication by sending the length o ...
After creating a JavaScript script that converts a string to BigInt, I encountered a challenge while trying to find a C# equivalent function. The original conversion in Javascript looked like this: BigInt("0x40000000061c924300441104148028c80861190a0ca4088 ...
Is there a way to convert a java.sql.Clob into a byte[] in Java? ...
HTML <a class="btn" data-popup-open="popup-1" href="#">More Details</a> <div class="popup" data-popup="popup-1"> <div class="popup-inner"> <h2>Unbelievable! Check this Out! (Popup #1)</h2> ...
Here is the code snippet from my dashboard's page.jsx 'use client' import { useSession } from 'next-auth/react' import { redirect } from 'next/navigation' import { getUserByEmail } from '@/utils/user' export d ...
I am currently working on developing a mouseover navigation website. Initially, my design included main buttons for "Our Team", Locations, and Patient Resources. Here is the basic structure I had before attempting to switch to a mouseover approach... &l ...
Having trouble getting my JSON / AJAX script to work. I've searched everywhere but can't find a clear explanation of how to use $.getJSON. Can someone please help me out and explain why my code isn't functioning? I suspect the issue lies wit ...
Whenever I incorporate the following code into my Meteor app: Home = FlowRouter.route("/", { name: "App", action(params) { ReactLayout.render(App); } }); An error message pops up in the Chrome console: Warning: React.createElement: type shoul ...
I am trying to locate patterns in strings where a character is followed by another character, and then followed by the first character again. For instance, in the string "abab" I am looking for "aba" and "bab". /([a-z])[a-z]{1}\1/g But when I run t ...