Reputation: 2725
How do you get Client IP and Browser information using JSP?
Upvotes: 6
Views: 33760
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
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
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
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
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
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