• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

How To Get Client IP Address In Servlet

February 12, 2014 //  by Krishna Srinivasan//  Leave a Comment

  • Java EE Tutorials
  • Servlets Tutorials
  • Servlets Interview Questions

The host name and IP address of the client requesting the servlet can be obtained using the HttpRequest object. In the following section we have a detailed java code to get clients address in a servlet. If the client uses proxy server , the real request to the servlet directly arrives from the proxy server and so it uses proxy IP address. Syntax of this method is java.lang.String getRemoteAddr()

Methods to get client IP address in a servlet

There are mainly two methods used to get the remote address or client address. They are as follows:

  • HttpServletRequest.getRemoteAddr(): This method returns the internet address of the client sending the request.
  • HttpServletRequestgetRemoteHost(): This returns the host name of the client sending the request. If the name is unknown then its returns an empty string.

Example

Example of getting clients address in a servlet:

Listing 1:ClientsAdd

import java.io.*;

import java.util.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class ClientsAdd extends HttpServlet {

  public void doGet(HttpServletRequest request,

HttpServletResponse response)

    throws IOException, ServletException{

    response.setContentType("text/html");

    PrintWriter out = response.getWriter();

    String name = request.getParameter("name");

	out.println("<h3>You have entered name : " + name + "<br>");

    out.println("<b><font color='blue'>" + "IP Address of request : </font></b>" +

request.getRemoteAddr()+"<h3>");

  }
}

Execute ClientsAdd.java in Eclipse and an output on screen appears which displays IP address of your server.

Previous Tutorial : Servlet Filter || Next Tutorial : Cookies In Servlet

Category: Java EETag: Servlets Tutorials

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « Servlet Filter
Next Post: Cookies In Servlet »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact