tags:

views:

321

answers:

5

How to get Client IP and Browser information using JSP?.

+1  A: 

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.

jabley
A: 

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...

KB22
A: 

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")
Alberto Zaccagni
A: 

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");

Pablo Santa Cruz
+2  A: 

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.

D. Wroblewski