Thursday 14 August 2014

getRemoteHost() : Get the Host name of the client

public String getRemoteHost()
Returns a String containing the fully qualified name of the client. If unable to resolve the host name, this method returns the dotted-string form of the IP address.

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(urlPatterns = {"/GetRemoteHost"})
public class GetRemoteHost extends HttpServlet {
    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException{
        try(PrintWriter out = res.getWriter()){
            String remoteHost = req.getRemoteHost();
            out.println("<html><head><title>Client Details</title></head>");
            out.println("<body><h1>" + remoteHost);
            out.println("</h1></body></html>");
        } 
    }
}

Output



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment