0

How to write request.getRemoteHost() method in servlet init() method?

eg:

public void init() throws ServletException
{
    String remoteHost = request.getRemoteHost();
    System.out.println("----------");
    System.out.println("------ ServletInitializer Servlet Initialized successfully ------");
    System.out.println("----------remoteHost :"+remoteHost);
}
GPI
  • 9,088
  • 2
  • 31
  • 38
  • 1
    `init()` method doesn't have any httprequest object. It's made for `doGet()` or `doPost()` when actual request is made from the client. – Braj Aug 06 '14 at 08:45
  • As per your comment you are interested to get the mac address. have a look at this [post](http://stackoverflow.com/questions/16448995/get-mac-address-of-system-in-java) as well. – Braj Aug 06 '14 at 08:48

1 Answers1

1
request.getRemoteHost()

inside init() doesnt make any sense

Please understand init method is called during the initialization of the servlet and not for the every request.

So you could handle it in the post/get methods. init() is not request dependent .

Update:

String ipAddress = request.getRemoteAddr();

gets the IP of the client . but you cant use it in the init() function as explained above use it in the doPost() or doGet() where you have implemented other methods

Santhosh
  • 8,181
  • 4
  • 29
  • 56
  • I want to get the Mac Id while starting the Tomcat – user3906140 Aug 06 '14 at 08:45
  • so you want to ip of the system where the server resides or application is deployed. – Braj Aug 06 '14 at 08:46
  • As per your question "you cant write request.getParameter() inside init()" because init() doesnt have the request object . update your question with proper details and clearly show us what you need – Santhosh Aug 06 '14 at 08:52
  • In my online project i need the IP Address of the client while he is trying to open this site – user3906140 Aug 06 '14 at 09:05
  • see my update in the answer – Santhosh Aug 06 '14 at 09:17
  • 1
    @user3906140 The request remote address won't give you the MAC address; the MAC address is only useful within its LAN; and it is of no use whatsoever in Java except as a very low grade system identifier string. – user207421 Aug 06 '14 at 10:00