top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How can I get the absolute URL of a servlet/JSP page at runtime?

+1 vote
345 views
How can I get the absolute URL of a servlet/JSP page at runtime?
posted Oct 27, 2015 by Dominic

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

You can get all the necessary information to determine the URL from the request object. To reconstruct the absolute URL from the scheme, server name, port, URI and query string you can use the URL class from java.net. The following code fragment will determine your page’s absolute URL:

String file = request.getRequestURI();
               if (request.getQueryString() != null) {
                  file += '?' + request.getQueryString();
               }
               URL reconstructedURL = new URL(request.getScheme(),
                                                             request.getServerName(),
                                                             request.getServerPort(),
                                                             file);
               out.println(URL.toString());
answer Oct 29, 2015 by Karthick.c
...