protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { int localPort = request.getLocalPort(); // do something with the local port number }
@WebServlet("/example") public class ExampleServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { int localPort = request.getLocalPort(); response.getWriter().println("Local Port: " + localPort); } }In this example, the ExampleServlet retrieves the local port number and returns it as a response to the client. The package library for HttpServletRequest is javax.servlet.http.