2015年7月9日 星期四

hostname

http://devco.re/blog/2014/06/19/client-ip-detection/

http://examples.javacodegeeks.com/enterprise-java/servlet/get-client-s-address-and-hostname-in-servlet/

package com.javacodegeeks.snippets.enterprise;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class GetClientAddressAndHostnameInServlet extends HttpServlet {

 private static final long serialVersionUID = -2128122335811219481L;

 public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {

  PrintWriter out = res.getWriter();
  res.setContentType("text/plain");

  // client's IP address
  String remoteAddr = req.getRemoteAddr();

  // client's hostname
  String remoteHost = req.getRemoteHost();

  out.write("remoteAddr = ");
  out.write(remoteAddr);
  out.write("n");
  out.write("remoteHost = ");
  out.write(remoteHost);

  out.close();

 }

}

沒有留言:

張貼留言