Thursday 14 August 2014

getRemoteAddr() : Get the address of the client

public String getRemoteAddr()
Returns a String containing the IP address of the client or last proxy that sent the request.

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

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

Output



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment