Identify the input URL of a Filter

Asked

Viewed 40 times

0

How do I identify by which URL a filter is being accessed?

My situation is as follows:

I have the class below:

@WebFilter(filterName = "FiltroLogado", urlPatterns = {"/alterar.jsp",
"/deletar.jsp", "/cadastrado.jsp", "/logado.jsp"})

public class FiltroLogado implements Filter{

@Override
public void init(FilterConfig filterConfig) throws ServletException {

}

@Override
public void doFilter(ServletRequest servletRequest, ServletResponse 
servletResponse, FilterChain filterChain) throws IOException, 
ServletException {

HttpSession session = ((HttpServletRequest) 
servletRequest).getSession();

if (session.getAttribute("usuario") == null){
    ((HttpServletResponse) servletResponse).sendRedirect("index.jsp");

}else
    if(){
    ((HttpServletResponse) servletResponse).sendRedirect("alterar.jsp");

}

}

I want to know by which URL the filter is being accessed to forward to the specific location of the link, for example by accessing the filter through the URL "/alterar.jsp", I want him to go to "/alterado.jsp".

2 answers

0

@Override
public void doFilter(ServletRequest servletRequest, ServletResponse 
servletResponse, FilterChain filterChain) throws IOException, 
ServletException {
    HttpServletRequest request = (FAZER CAST) servletRequest;
       if ( request instanceof HttpServletRequest ) {
             String url = ((HttpServletRequest)request).getRequestURL().toString();
             String queryString = ((HttpServletRequest)request).getQueryString();
            }

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

}

you can make a if to know which page and redirect.

  • Yes. My doubt was about the method, but I already found it. Thank you.

0

I used the method getServletPath to verify by which of the Urls the request had entered.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.