Thursday 7 August 2014

getServerPort() : Get the Server Port

public int getServerPort()
Returns the port number to which the request was sent.

import java.io.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;

@WebServlet(urlPatterns = {"/GetServerDetails"})
public class GetServerDetails extends HttpServlet {
     @Override
    public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException {
        try(PrintWriter out = res.getWriter()){
            String serverName = req.getServerName();
            int port = req.getServerPort();
            
            out.println("<html><head><title>Server Details</title>");
            out.println("<body><h1>");
            out.println("Server Name: " + serverName);
            out.println("<br />Server Port: " + port);
            out.println("</h1></body></html>");
        }
    }   
}

Run the above servlet like
'http://localhost:8080/servlet/GetServerDetails'

Output



 
Prevoius                                                 Next                                                 Home

No comments:

Post a Comment