添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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?

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().

if you want the "blabla:8080" part stripped away for you, getRequestURI() ('I' not 'l') returns a String starting with "/" – Alexander Taylor Nov 28, 2014 at 22:31 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) – Polyana Fontes Apr 28, 2016 at 5:24

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();

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);

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
                ServletRequest does not implement getRequestURL().  You need to cast it to HttpServletRequest if possible as per Buhake's answer
– Anthony Hayward
                Jun 5, 2015 at 11:06
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.