2

How can I identify whether the browser is Firefox or Chrome? Basically, I want an application to run only on the specific browser that is registered by a user. For this scenario, I want my application to identify the browser which the user is using, to know whether the application is permitted to run.

I am using java servlet.

I tried a the browser’s local storage, but it can be deleted with no control from my application. If local storage can be used, please let me know how.

(Yes I can get a browser info, but I want to identify a specific machine with a browser from where my application user is permitted to run the application; otherwise, I need to restrict that user from accessing my application.)

TRiG
  • 10,148
  • 7
  • 57
  • 107
Swap L
  • 698
  • 3
  • 12
  • 27

5 Answers5

2
httpRequest.getHeader("user-agent")
Mustafa Genç
  • 2,569
  • 19
  • 34
2

Fetch user-agent properties from the HTTP requeste header.

  String userAgent=req.getHeader("user-agent");
  String browserName = "";
  String  browserVer = "";
  if(userAgent.contains("Chrome")){ //checking if Chrome
        String substring=userAgent.substring(userAgent.indexOf("Chrome")).split(" ")[0];
        browserName=substring.split("/")[0];
        browserVer=substring.split("/")[1];
    }
    else if(userAgent.contains("Firefox")){  //Checking if Firefox
        String substring=userAgent.substring(userAgent.indexOf("Firefox")).split(" ")[0];
        browserName=substring.split("/")[0];
        browserVer=substring.split("/")[1];
    }
Alpesh Gediya
  • 3,706
  • 1
  • 25
  • 38
  • this is working but i want to configure this browser with my application so that user can only access my application through this browser only – Swap L Oct 21 '13 at 12:19
  • I did not understand what you want to say? – Alpesh Gediya Oct 21 '13 at 12:33
  • see i want my application only run on specific browser in specific machine. If somebody try to access from different browser then i want to restrict that user – Swap L Oct 21 '13 at 12:34
  • @SwapL If browser detected by the servlet is other than what you want to allow, discard the request or either show user appropriate error message.. – Alpesh Gediya Oct 21 '13 at 12:37
  • Yes but req.getHeader("user-agent"); is enough to detect diffrent brower and diffrent machin is used that i want to know – Swap L Oct 21 '13 at 12:40
  • @SwapL, Do you want to allow for particular IP address of machanine ? – Alpesh Gediya Oct 21 '13 at 12:50
  • not IP Address that i did already by IP address now i want it restricted upto machine and browser because my application is web base – Swap L Oct 21 '13 at 13:05
1

Please use the below code in servlet to know what browser is hitting your servlet.

String userAgent = request.getHeader("user-agent");
Raptor
  • 53,206
  • 45
  • 230
  • 366
Debabrata
  • 5
  • 2
1

Here is the code :

String userAgent = req.getHeader("user-agent");
UserAgent ua = UserAgent.parseUserAgentString(userAgent);
Version browserVersion = ua.getBrowserVersion();
String browserName = ua.getBrowser().toString();
int majVersion = Integer.parseInt(browserVersion.getMajorVersion());

Or use can easily get the browser deatils from javascript code like this - Browser CodeName = navigator.appCodeName Browser Name = navigator.appName Browser Version = navigator.appVersion

LHH
  • 3,233
  • 1
  • 20
  • 27
  • is there any unique property i can get ? is there any unique property with browser ???? – Swap L Oct 21 '13 at 08:04
  • User-Agent value as follows: Mozilla/[version] ([system and browser information]) [platform] ([platform details]) [extensions]0 – LHH Oct 21 '13 at 08:08
  • Copy this script into your JavaScript files. It works immediately, and you can query three properties of the BrowserDetect object: Browser name: BrowserDetect.browser Browser version: BrowserDetect.version OS name: BrowserDetect.OS – LHH Oct 21 '13 at 08:10
  • @LalitHaryani this all are the unique property of browser??? if i save this data then i can trigger where my application run and where not??? – Swap L Oct 21 '13 at 11:44
1
public class MyServlet extends HttpServlet
{  
   @Override
   public void doGet(final HttpServletRequest aRequest,   
                     final HttpServletResponse aResponse) throws ServletException, IOException
   {  
       final String agent = aRequest.getHeader("user-agent");
       // agent will looks like  
       // Mozilla/5.0 (Windows NT 5.1; rv:23.0) Gecko/20100101 Firefox/23.0
       // Mozilla/2.0 (compatible; MSIE 6.0; Windows NT 5.2)
       // Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36
       // etc.
   }
}
Ilya
  • 29,135
  • 19
  • 110
  • 158
  • from user-agent can i restrict application login also ? means suppose my application is only allowed to login with one machine and one browser.... – Swap L Oct 21 '13 at 08:00
  • @SwapL what means `application login`? Is it login entered by user in `form input`? – Ilya Oct 21 '13 at 08:07
  • @SwapL client: `` server: `aRequest.getParameter("someName")` – Ilya Oct 21 '13 at 09:13