Identify the input URL of a Filter

0

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

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 why URL the filter is being accessed to forward to the specific place of the link, for example, when accessing the filter by URL "/alterar.jsp" , I want it to go to "/alterado.jsp" .     

asked by anonymous 07.02.2018 / 19:42

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 if to know which page to redirect.

    
07.02.2018 / 20:05
0

I used the getServletPath method to check which of the URLs the request had entered.

    
17.05.2018 / 21:21