Java code to get the Users IP Address
Java Program to get the Users IP Address
This following code shows how to obtain the IP address of the user that calls the server. The Request object has a few methods to get information about the call and about the caller, and one of them is named getRemoteAddr() which returns the IP address of the calling computer.
The below example will print the IP address of the user on a blank html-page.
import java.io.*;
import java.util.Enumeration;
import javax.servlet.*;
import javax.servlet.http.*;
public class ExampleServlet extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
printPageStart(out);
//Funtion to Print out the IP address of the caller
out.println(request.getRemoteAddr());
}
}
0 comments:
Post a Comment