Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Server IP address from JSP Request/session object

How can I get the IP address of the server from a JSP page?

Right now, all I can do is request.getLocalName(), which returns the server name, not the IP address?

like image 638
Lydon Ch Avatar asked Jul 05 '10 04:07

Lydon Ch


People also ask

How do I find the IP address of a session?

To get the IP address of a user session you simply have to use the 'getClientIP()' method to pull the IP address from the current user's session object. gs. getSession().

What is JSP request?

The JSP request is an implicit object of type HttpServletRequest i.e. created for each jsp request by the web container. It can be used to get request information such as parameter, header information, remote address, server name, server port, content type, character encoding etc.

Which of the following is used to insert Java values directly into the output of a SP page?

A JSP expression is used to insert the resultant value of a single Java expression into the response message.


4 Answers

Actually, for the IP address of the server, you need to use

String serverIP = request.getLocalAddr();
like image 111
ig0774 Avatar answered Oct 01 '22 19:10

ig0774


String sIPAddr = request.getRemoteAddr();
like image 22
Abhijeet Pathak Avatar answered Oct 01 '22 19:10

Abhijeet Pathak


To get an actual server IP and hostname (actual and not set by e.g. a proxy) use this:

            <%@ page import="java.net.*" %> 
            [...]
            <%
            String hostname, serverAddress;
            hostname = "error";
            serverAddress = "error";
            try {
                InetAddress inetAddress;
                inetAddress = InetAddress.getLocalHost();
                hostname = inetAddress.getHostName();
                serverAddress = inetAddress.toString();
            } catch (UnknownHostException e) {

                e.printStackTrace();
            }
            %>
            <li>InetAddress: <%=serverAddress %>
            <li>InetAddress.hostname: <%=hostname %>
like image 27
Nux Avatar answered Oct 01 '22 17:10

Nux


String addr = request.getRemoteAddr();
like image 36
Aaron Saunders Avatar answered Oct 01 '22 19:10

Aaron Saunders