• About Us
  • Blog
  • Basket
  • Account
  • Sign In
  •  

Java API

What's Covered

This tutorial demonstrates how to extend the BaseServlet to use device detection capabilities. The BaseServlet gives access to the 51Degrees WebProvider class that manages temporary data files whilst providing the same device detection capabilities as the Core provider. You need to run this example from a Java Web project servlet. Maven project on github contains a full version of this example, ready to run.

The BaseServlet takes care of instantiating the provider and managing the data file. If you wish to use device detection but do not want to extend the BaseServlet provided by 51Degrees, you will need to manually instantiate the provider and implement the mechanisms to manage temporary files for using automatic updates with StreamMode. Other services like the image optimiser and bandwidth monitoring can still be accessed by configuring listeners. See Web.xml configuration for further details.

Code and Explanation

Returns a page with predefined properties, match metrics information and a list of the relevant HTTP headers used for the detection.

Device detection functionality becomes available upon extending the 51Degrees BaseServlet from the WebApp module.

  public class Example extends BaseServlet {
 

This example servlet outputs a similar page to that of the other 51Degrees APIs.

WebApp module provides access to the WebProvider which extends the Core Provider, hence allowing access to the same set of features you can use for offline device detection. To access property:

  Property pr = super.getProvider(request).dataSet.get("IsMobile");
 

The WebProvider’s role is to manage the temporary data files. In the Servlet environment it is not always possible to stop device detection to update the data file, by using temporary files the master file can be kept free of write locks.

As of version 3.2 the Lite data file is no longer embedded in the API. Please remember to specify the BINARY_FILE_PATH in the Web.xml and ensure the data file is placed there.

Full Source File
public class UsingDetection extends BaseServlet {
    
    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs.
     * @throws IOException if there was a problem accessing the data file.
     */
    protected void processRequest(HttpServletRequest request, 
                                  HttpServletResponse response)
                                  throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet UsingDetection</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>51Degrees Servlet Device Detection Example.</h1>");
            String name, value;
            // Get a list of all properties available in the data file.
            // For each property print corresponding value.
            for (Property property : 
                    super.getProvider(request).dataSet.getProperties()) {
                // Name of the current property.
                name = property.getName();
                // Value of the current property as a string.
                value = super.getProperty(request, name);
                // Only use value if it's not null.
                if (value != null) {
                    out.println("<li>" + name + ": " + value + "</li>");
                }
            }
            out.println("</body>");
            out.println("</html>");
        } finally {
            out.close();
        }
    }

    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }
}

Full Source File

Summary

This tutorial demonstrated how to extend the BaseServlet to access device detection capabilities. The example produced a sample page with a pre-defined set of properties displaying match metrics, relevant headers plus a table with properties and values. Properties include both Lite and Premium properties.

Using Premium and Enterprise data gives you access to considerably more properties as well as features like automatic data updates:

Feature Lite Premium Enterprise
Automatic Data Updates No Weekly Daily
JavaScript Yes Yes Yes
Image Optimiser Yes Yes Yes
Client Side Overrides No No Yes
PerformanceMonitoring No No Yes