protected Realm getRealm(HttpServletRequest request) { String serverName = request.getServerName(); String contextPath = request.getContextPath(); Host host = (Host) engine.findChild(serverName); if (host == null) { // if it cannot find host, then use the default host. host = (Host) engine.findChild(engine.getDefaultHost()); if (host == null) throw new NullPointerException( "Could not find Tomcat host for: " + serverName + " or: " + engine.getDefaultHost()); } Context context = (Context) host.findChild(contextPath); if (context == null) throw new NullPointerException("Could not find Tomcat context for: " + contextPath); Realm realm = context.getRealm(); if (realm == null) throw new NullPointerException( "Could not find Tomcat realm for: " + serverName + "" + contextPath); return realm; }