Thursday, 14 August 2014

getRemotePort() : Get remote port of client

public int getRemotePort()
Returns the Internet Protocol (IP) source port of the client or last proxy that sent the request.

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 = {"/GetRemotePort"})
public class GetRemotePort extends HttpServlet {
    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException{
        try(PrintWriter out = res.getWriter()){
            int remotePort = req.getRemotePort();
            out.println("<html><head><title>Client Details</title></head>");
            out.println("<body><h1>" + remotePort);
            out.println("</h1></body></html>");
        } 
    }
    
}


Sample Output



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment