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