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

WebApp with Standard Servlet - Version 3

The Core Detector can be used in a servlet without using the FiftyOneServlet as a base. Note that you will still need to set up Listener from the WebApp project. See Java Web Application guide if you have not already done this.

Your servlet will need to get the WebProvider directly from the ServletContext.

public class ExampleServlet extends HttpServlet {

  WebProvider provider;

  @Override
  public void init(final ServletConfig sc) throws ServletException {
    super.init();
    this.provider = ((FiftyOneDegreesListener)sc.getServletContext().
            getAttribute(Constants.WEB_PROVIDER_KEY)).getProvider();
  }

  @Override
  protected void doGet(HttpServletRequest request,
          HttpServletResponse response) throws IOException, ServletException {
      
    boolean isMobile = provider.match(request.getHeader("USER_AGENT"))
            .getValues("IsMobile")
            .toBool();
  }
}

Need Help?

WebApp with a Standard Servlet - Version 2

The Core Detector can be used in a servlet without using the FiftyOneServlet as a base. Note that you will still need to set up Listener from the WebApp project. See Java Web Application guide if you have not already done this.

Your servlet will need to get the Factory and Provider directly from the ServletContext. Note that this will require two additional imports: fiftyone.mobile.detection.webapp.Constants and fiftyone.mobile.detection.webapp.Listener.
// In your servlet class...
    /**
     * Gets the factory being used by this servlet context.
     * @param sc
     * @throws ServletException 
     */
    @Override
    public void init(ServletConfig sc) throws ServletException {
        super.init();
        
        // get factory from servlet context
        Listener Factory = (Listener)sc.getServletContext().
                getAttribute(Constants.FACTORY_KEY);
        
        // get provider from factory
        Provider provider = Factory.getProvider();
        // other initialisation code...
}

Important

Note that this is only required if you cannot extend your servlet from FiftyOneServlet. If you are using it you do not need to follow this guide. Instead see Creating Web Applications in Java.