Take the 2-minute tour ×

I have an HttpServletRequest object.

How do I get the complete and exact URL that caused this call to arrive at my servlet?

Or at least as accurately as possible, as there are perhaps things that can be regenerated (the order of the parameters, perhaps).

share|improve this question
Why is there a bounty on this question ? This looks random... –  dystroy Mar 28 '13 at 8:52

7 Answers 7

up vote 138 down vote accepted

The HttpServletRequest has the following methods:

  • getRequestURL() - returns the part of the full URL before query string separator character ?
  • getQueryString() - returns the part of the full URL after query string separator character ?

So, to get the full URL, just do:

public static String getFullURL(HttpServletRequest request) {
    StringBuffer requestURL = request.getRequestURL();
    String queryString = request.getQueryString();

    if (queryString == null) {
        return requestURL.toString();
    } else {
        return requestURL.append('?').append(queryString).toString();
share|improve this answer
You copied the description from getRequestURI (wrong) but use getRequestURL in the code (right). –  Vinko Vrsalovic Feb 8 '10 at 14:56
You need to conditionally check if the query string is empty. –  Adam Gent Sep 15 '10 at 20:43
You're also mutating the StringBuffer backing the request URL. If the request implementation isn't making a defensive copy that's a very good way to introduce strange behavior and bugs in other parts of code that expect it in it's original form. –  Ken Blair May 22 '12 at 16:53
Use StringBuilder instead of StringBuffer –  Gladwin B Mar 28 '13 at 15:27
@KenBlair: A StringBuffer is returned on purpose, so that you easily can add on more stash. Since this is specified on the javadoc, it would be extremely absurd of the implementation to expect that the returned StringBuffer would not be modified by the caller - hence, this is cool. –  stolsvik Jul 15 '13 at 12:53

Combining the results of getRequestURL() and getQueryString() should get you the desired result.

share|improve this answer
how to handle post related params? –  flash May 29 at 5:58
@flash strictly speaking, POST-parameters are not part of the URL, they are the body of the request. –  Geert Jun 30 at 12:06

HttpUtil being deprecated, this is the correct method

StringBuffer url = req.getRequestURL();
String queryString = req.getQueryString();
if (queryString != null) {
String requestURL = url.toString();
share|improve this answer
this class is deprecated. –  Bozho Feb 8 '10 at 14:45
// http://hostname.com/mywebapp/servlet/MyServlet/a/b;c=123?d=789

public static String getUrl(HttpServletRequest req) {
    String reqUrl = req.getRequestURL().toString();
    String queryString = req.getQueryString();   // d=789
    if (queryString != null) {
        reqUrl += "?"+queryString;
    return reqUrl;
share|improve this answer
You're ignoring the advantage of StringBuffer. –  BalusC Feb 8 '10 at 14:45
Yes. I accept it, but its only two additional objects I guess. –  Teja Kantamneni Feb 8 '10 at 15:36
It's one additional object (a StringBuilder) and does not mutate the underlying StringBuffer that is returned. I would actually prefer this over the "accepted" answer, the JVM will optimize the difference away regardless and this does not have any of the risk of introducing bugs. –  Ken Blair May 22 '12 at 16:55

I use this method:

public static String getURL(HttpServletRequest req) {

    String scheme = req.getScheme();             // http
    String serverName = req.getServerName();     // hostname.com
    int serverPort = req.getServerPort();        // 80
    String contextPath = req.getContextPath();   // /mywebapp
    String servletPath = req.getServletPath();   // /servlet/MyServlet
    String pathInfo = req.getPathInfo();         // /a/b;c=123
    String queryString = req.getQueryString();          // d=789

    // Reconstruct original requesting URL
    StringBuffer url =  new StringBuffer();

    if ((serverPort != 80) && (serverPort != 443)) {


    if (pathInfo != null) {
    if (queryString != null) {
    return url.toString();
share|improve this answer
This is a helpful answer for a quick reference to all the bits of info available on the HttpServletRequest. However, I think you'd want to check the scheme in deciding whether to add the port piece to the result. "https" would be 443, for example. –  Peter Cardona Jun 20 '12 at 0:52
a small optimization would be to use a StringBuilder instead of StringBuffer, just a hint –  Chris Oct 22 '13 at 15:32
That depends on the fact if you require synchronization. I figured better safe than sorry. So if anyone needs the code to be faster they should use StringBuilder but if you want thread safety well stick with what is in this answer. –  Mat Banik Oct 22 '13 at 19:24
awedome function.....use it so often! –  Nitin Bansal Dec 5 '13 at 8:38
Just a comment: thread safety is not an issue in this specific example because url is declared, instantiated, and used only inside the method, so it can't be accessed from threads other than the one that called the method. –  Diogo Kollross Jun 12 at 15:32

You can use filter .

    public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException {
            HttpServletRequest test1=    (HttpServletRequest) arg0;

         test1.getRequestURL()); it gives  http://localhost:8081/applicationName/menu/index.action
         test1.getRequestURI()); it gives applicationName/menu/index.action
         String pathname = test1.getServletPath()); it gives //menu/index.action

            arg2.doFilter(arg0, arg1); // call to urs servlet or frameowrk managed controller method

            // in resposne 
           HttpServletResponse httpResp = (HttpServletResponse) arg1;
           RequestDispatcher rd = arg0.getRequestDispatcher("another.jsp");     
           rd.forward(arg0, arg1);


donot forget to put <dispatcher>FORWARD</dispatcher> in filter mapping in web.xml

share|improve this answer

Use the following methods on HttpServletRequest object

java.lang.String getRequestURI() -Returns the part of this request's URL from the protocol name up to the query string in the first line of the HTTP request.

java.lang.StringBuffer getRequestURL() -Reconstructs the URL the client used to make the request.

java.lang.String getQueryString() -Returns the query string that is contained in the request URL after the path.

share|improve this answer

Your Answer


