Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to Get Request URL and other details

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 334
    Comment on it

    Following JavaServlet will give you Request URL and other details. You can choose which method suits your coding:

    import java.io.IOException;
    import java.io.PrintWriter;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    public class RequestServlet extends HttpServlet {
     private static final long serialVersionUID = 1L;
    
     public RequestServlet() {
      super();
     }
    
     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      response.setContentType("text/html");
      PrintWriter out = response.getWriter();
      out.println("<html>");
      out.println("<head>");
      out.println("<title>Request Details</title>");
      out.println("</head>");
      out.println("<body>");
      out.println("<h1>Request Details</h1>");
      out.println("<h3>URL = "+request.getRequestURL()+"</h3>");
      out.println("<h3>URI = "+request.getRequestURI()+"</h3>");
      out.println("<h3>Scheme = "+request.getScheme()+"</h3>");
      out.println("<h3>Server Name = "+request.getServerName()+"</h3>");
      out.println("<h3>Server Port = "+request.getServerPort()+"</h3>");
      out.println("<h3>Context Path = "+request.getContextPath()+"</h3>");
      out.println("<h3>Servlet Path = "+request.getServletPath()+"</h3>");
      out.println("<h3>Query String = "+request.getQueryString()+"</h3>");
      out.println("</body>");
      out.println("</html>");
     }
    
     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      doGet(request, response);
     }
    }
    

    Following is the output when we call this Servlet from URL: http://localhost:8080/ApplicationName/RequestServlet?someQueryString

    Request Details
    URL = http://localhost:8080/ApplicationName/RequestServlet
    URI = /ApplicationName/RequestServlet
    Scheme = http
    Server Name = localhost
    Server Port = 8080
    Context Path = /ApplicationName
    Servlet Path = /RequestServlet
    Query String = someQueryString
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: