user405398
user405398

Reputation:

How to get response from this filter?

The following servlet filter is getting called, but not able to give the correct response. I just looping inside the filter itself. The Browser says,

The page isn't redirecting properly
Firefox has detected that the server is redirecting the request for this address in a way that will never complete.

ExpenseAuthenticationFilter.class

public class ExpenseAuthenticationFilter implements Filter {
public ExpenseAuthenticationFilter() {
    // TODO Auto-generated constructor stub
}

public void destroy() {
    // TODO Auto-generated method stub
}

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

    try
    {
        HttpSession session = ((HttpServletRequest)request).getSession(false);

        if (session == null){
            System.out.println("Inside login Filter");
            ((HttpServletResponse) response).sendRedirect("ExpenseManagementLogin.html");
        }
    }
    catch (IllegalStateException ise){
        System.out.println("Session Not Yet Created");
    }

    chain.doFilter(request, response);
}
public void init(FilterConfig fConfig) throws ServletException {
    // TODO Auto-generated method stub
}

 }

ExpenseAuthentication.class

public class ExpenseAuthentication implements Filter {
public ExpenseAuthentication() {
    // TODO Auto-generated constructor stub
}

public void destroy() {
    // TODO Auto-generated method stub
}

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException, IllegalStateException{
    try
    {
        HttpSession session = ((HttpServletRequest) request).getSession(false);
        if (session == null ){
            System.out.println("Management Filter");
            ((HttpServletResponse) response).sendRedirect("ExpenseManagementLogin.html");
        }

    }
    catch (IllegalStateException ise){
        System.out.println("Session Not Yet Created");
    }
    chain.doFilter(request, response);
}
public void init(FilterConfig fConfig) throws ServletException {
    // TODO Auto-generated method stub
}

 }

The Web.xml

<filter>
    <filter-name>ExpenseAuthentication</filter-name>
    <filter-class>com.pricar.hibernate.ExpenseAuthentication</filter-class>
</filter>
<filter-mapping>
    <filter-name>ExpenseAuthentication</filter-name>
    <url-pattern>*/ExpenseDetailsManagement.html</url-pattern>
</filter-mapping>
<filter-mapping>
    <filter-name>ExpenseAuthenticationFilter</filter-name>
    <url-pattern>*/ExpenseManagementLogin.html</url-pattern>
</filter-mapping>

The Console Log for "http://localhost:8080/Hibernate/ExpenseDetailsManagement.html" is

Inside login Filter
Inside login Filter
Inside login Filter
Inside login Filter
Inside login Filter
Inside login Filter
Inside login Filter
Inside login Filter
Inside login Filter
Inside login Filter
Inside login Filter
Inside login Filter
Inside login Filter
Inside login Filter
Inside login Filter
Inside login Filter
Inside login Filter
Inside login Filter

Its not actually going into ExpenseAuthentication.class, But it goes to ExpenseAuthenticationFilter.class and loops inside of it.

Any suggestions!!

Thanks!

Upvotes: 0

Views: 2442

Answers (1)

Colin Hebert
Colin Hebert

Reputation: 93187

When you do this :

((HttpServletResponse) response).sendRedirect("ExpenseManagementLogin.html");

you're sending the user right to the "ExpenseManagementLogin.html" page which is filtered with ExpenseAuthenticationFilter.

Here is your infinite loop.

As long as the user doesn't have a session it will loop. And as the filter is looping right to itself there won't be any session.
Same thing with your second filter. If a filter does a sendRedirect to a path that need to be filtered by itself. Unless you have some controls (here a session creation), you'll have an infinite loop.

As this filter only works on "ExpenseManagementLogin.html", why do you do this redirection ?

Upvotes: 3

Related Questions