Home » Java Codes

Java Servlet – How to get the client address in a Servlet

12 February 2009 179 views No Comment

This Java tip illustrates a method of getting the client’s address in a Servlet. In an embodiment developer may use this tip in their client server applications for knowing at the server end the address of the client which made the request.

// This method is called by the servlet container to process a GET request.
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {

// Get client's IP address
String addr = req.getRemoteAddr(); // 123.123.123.123

// Get client's hostname
String host = req.getRemoteHost(); // hostname.com
}


Tags: , , , , , , , , , , , , , , , , , , , ,

Leave your response!

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>