97

I am trying to write a filter that can retrieve the request URL, but I'm not sure how to do so.

Here is what I have so far:

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

public class MyFilter implements Filter {
    public void init(FilterConfig config) throws ServletException { }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        chain.doFilter(request, response);

        String url = ((HttpServletRequest) request).getPathTranslated();
        System.out.println("Url: " + url);
    }

    public void destroy() { }
}

When I hit a page on my server, the only output I see is "Url: null".

What is the correct way to get the requested URL from a given ServletRequest object in a Filter?

5 Answers 5

206

Is this what you're looking for?

if (request instanceof HttpServletRequest) {
 String url = ((HttpServletRequest)request).getRequestURL().toString();
 String queryString = ((HttpServletRequest)request).getQueryString();
}

To Reconstruct:

System.out.println(url + "?" + queryString);

Info on HttpServletRequest.getRequestURL() and HttpServletRequest.getQueryString().

Sign up to request clarification or add additional context in comments.

4 Comments

getRequestURL() returns StringBuffer, not String.
It's better to consider the pattern of null queryString.
if you want the "blabla:8080" part stripped away for you, getRequestURI() ('I' not 'l') returns a String starting with "/"
If you want only the path to the servlet you can use request.getServletPath(), it's useful on JavaServer Faces to retrieve the equivalent of the view id (the path from the webapp root to the xhtml page, excluding domains, deployment prefix, etc)
6

Building on another answer on this page,

public static String getCurrentUrlFromRequest(ServletRequest request)
{
   if (! (request instanceof HttpServletRequest))
       return null;

   return getCurrentUrlFromRequest((HttpServletRequest)request);
}

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

    if (queryString == null)
        return requestURL.toString();

    return requestURL.append('?').append(queryString).toString();
}

Comments

3

If you use Spring, you can use OncePerRequestFilter or others.

import org.springframework.web.filter.OncePerRequestFilter;
import org.springframework.stereotype.Component;

@Component
public class MyFilter extends OncePerRequestFilter {
    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
         String url = request.getRequestURL();
         filterChain.doFilter(request, response);
    }

}

Comments

0

If you want to acquire them separately you can use as the following

((HeaderWriterFilter.HeaderWriterRequest) request).getRequest().getAttribute("org.apache.catalina.AccessLog.ServerName") => host info
((HeaderWriterFilter.HeaderWriterRequest) request).getRequest().getAttribute("org.apache.catalina.AccessLog.ServerPort") => port info
((HttpServletRequest)request).getServletPath() or ((HttpServletRequest)request).getRequestURI() => requested source path
((HttpServletRequest)request).getQueryString() => query parameters

Comments

-2
request.getRequestURL();   

2 Comments

ServletRequest does not implement getRequestURL(). You need to cast it to HttpServletRequest if possible as per Buhake's answer
its missing parameters

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.