Knowledge base

Java

How does it work?

To make use of Netbiscuits Server-Side Java Device Detection, all you have to do is download the server-side client, configure it, and start using it.

With the Java client configured, the following events happen when a user’s browser requests a page from your website:

  • Your server requests the Device Detection profile from a Netbiscuits Device Detection server
  • The Device Detection server runs a series of tests to identify the user’s device
  • A collection of parameters and values is created and returned to your application within a DeviceProfile Object, which offers methods to access single parameters and device information
  • The Java client can also be configured to gather dynamic data within the user’s browser, e.g. display dimensions and device pixel ratio
  • Once complete, the Device Detection object can be used to customize the user’s experience for the exact device they are using

How can I use Netbiscuits Java Device Detection?

  1. First of all, make sure you’ve created a Netbiscuits account and have logged in.
  2. If you’re all set there, then you need to download your desired Java client (with dependencies or not).
  3. Next, copy your Java configuration code.
  4. Once copied, paste that code block into ContextListener.java to instantiate a new com.netbiscuits.dcs.detection.service, such as:
    public void contextInitialized(ServletContextEvent sce) 
    {
        Config config = new Config();config.setAccountName("[accountName]");
        config.setServiceUrl("[serviceUrl]");
        config.setToken("[accountToken]");
        config.setParamProfile("[profileName]");
        config.setEnableAnalytics(true);
        try {
            Detection.init(config);
        } catch (Exception e) {
            System.out.println("Could not initialize cloud-service-client: " + e.getCause().getMessage() + ". Original message");
        }
    }
    
  5. Then, in your Servlet for example, call the detect() method. This will fill the DeviceProfile with the Device Detection information, something like:
    ...
    com.netbiscuits.dcs.detection.service.IDetection detection =
    com.netbiscuits.dcs.detection.service.Detection.getInstance();
    DeviceProfile deviceProfile = detection.detect(request);
    ...
    
  6. Finally, you are ready to use the Device Detection information in your code, as follows:
    ...
    String vendor = device.getVendor();
    String model = device.getModel();
    String os = device.getOS();
    boolean isBot = device.isBot();
    boolean isSmartphone = device.isSmartPhone();
    boolean isFeaturePhone = device.FeaturePhone();
    boolean isDesktop = device.isDesktop();
    boolean isTablet = device.isTablet();
    ...
    
  7. This data can now be used anywhere in your web application code, for example to change certain parts of your templates, send different CSS and JavaScript files based on device information, or even redirect users to another subdomain or template version of your website.

How do I configure dynamic parameter detection?

To detect dynamic parameters and make them available in your server side DeviceProfile Object, you need to set up a ServletFilter. This checks whether the dynamic parameters are already detected. If it is the first time the user visits your site, the servlet filter will send a device detection javascript to the browser to detect the dynamic parameters.

Here is how to set up dynamic parameter detection:

  1. Add DetectionFilter Servlet Filter to your web.xml file.
    <filter>
        <filter-name>DetectionFilter</filter-name>
        <filter-class>com.netbiscuits.dcs.detection.service.DetectionFilter</filter-class>
        <init-param>
            <param-name>accountName</param-name>
    	<param-value>[accountName]</param-value>
        </init-param>
        <init-param>
    	<param-name>accountToken</param-name>
    	<param-value>[accountToken]</param-value>
        </init-param>
        <init-param>
    	<param-name>serviceUrl</param-name>
    	<param-value>https://dcs.netbiscuits.net/ds/pretest/</param-value>
        </init-param>
    </filter>
    
    <filter-mapping>
        <filter-name>DetectionFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    
  2. Once the filter is set, it sends a detection script back to the browser the first time the user visits your site.
    The script detects and injects the dynamic parameters into your DeviceProfile Object on server side.
    You can then use those values, as follows:

    ...
    float pixelRation = device.getPixelRatio();
    long width = device.getMaxWidth();
    long height = device.getMaxHeight();
    ...
    

    Please note that you can also use those values without dynamic parameter detection. However, the values will not be as accurate as with dynamic parameter detection, because on some devices, those values can only be detected on the client side.

    You can use dynamic detection parameters to send images in an optimized resolution to the browser or control the layout of your site to offer an optimized usability on the users device.

Demos

To help you further with getting started, we have created a series of demos that you can download and run on your own server (including your localhost).

We hope this has helped you get up and running with the Netbiscuits Java Device Detection. If you have any questions or need help with your setup, please contact us.

Or if you’re ready for the next step, find out about all the Device Detection Parameters you can build into your website!

Learn more about Device Detection Parameters

YouTube aktivieren?

Auf dieser Seite gibt es mind. ein YouTube Video. Cookies für diese Website wurden abgelehnt. Dadurch können keine YouTube Videos mehr angezeigt werden, weil YouTube ohne Cookies und Tracking Mechanismen nicht funktioniert. Willst du YouTube dennoch freischalten?