vipin k.
vipin k.

Reputation: 2725

How to get browser information in JSP?

How do you get Client IP and Browser information using JSP?

Upvotes: 6

Views: 33760

Answers (6)

chandru  kutty
chandru kutty

Reputation: 567

String browser=request.getHeader("user-agent");
String browsername = "";
String browserversion = "";
String[] otherBrowsers={"Firefox","Chrome","Chrome","Safari"};
    if(browser != null ){
        if((browser.indexOf("MSIE") == -1) && (browser.indexOf("msie") == -1)){
            for(int i=0; i< otherBrowsers.length;  i++){
                System.out.println(browser.indexOf(otherBrowsers[i]));
                browsername=otherBrowsers[i];
                break;
            }
            String subsString = browser.substring( browser.indexOf(browsername));
            String Info[] = (subsString.split(" ")[0]).split("/");
            browsername = Info[0];
            browserversion = Info[1];
    }
    else{
        String tempStr = browser.substring(browser.indexOf("MSIE"),browser.length());
            browsername    = "IE"
        browserversion = tempStr.substring(4,tempStr.indexOf(";"));
    }
}

Upvotes: 1

D. Wroblewski
D. Wroblewski

Reputation: 7590

The following jsp will output your ip address and user-agent:

Your user-agent is: <%=request.getHeader("user-agent")%><br/>
Your IP address is: <%=request.getRemoteAddr()%><br/>

To find out what browser and/or OS the user is using, parse the user-agent header.

For example:

<%
String userAgent = request.getHeader("user-agent");
if (userAgent.indexOf("MSIE") > -1) {
  out.println("Your browser is Microsoft Internet Explorer<br/>");
}
%>

For a list of user agents, look here.

Upvotes: 12

Pablo Santa Cruz
Pablo Santa Cruz

Reputation: 181460

You can get all the information the client is willing to give you through HTTP headers. Here's a complete list of them.

To access the header in a servlet or JSP, use:

request.getHeader("name-of-the-header-you-want");

Upvotes: 0

Alberto Zaccagni
Alberto Zaccagni

Reputation: 31590

Here you can find getRemoteAddr(), which

Returns the fully qualified name of the client or the last proxy that sent the request

...and with this you (maybe) retrieve the browser

request.getHeader("User-Agent")

Upvotes: 0

KB22
KB22

Reputation: 6979

For the browser part you need to parse the reqeust's User-Agent section.

String browserType = request.getHeader("User-Agent");

There you'll find the relevant information...

Upvotes: 6

jabley
jabley

Reputation: 2222

ServletRequest.getRemoteAddr() or the X-Forwarded-For header, if you think you can trust it.

What sort of browser information? The request headers will have the User-Agent.

Upvotes: 1

Related Questions