The page isn't redirecting properly in Servlet/Filter

java redirect servlets

695 просмотра

1 ответ

412 Репутация автора

I am new to Servlet and Filter. I am trying to block all users that is not logged-in in my application.

Web.xml file


        <param-value>index.jsp, SelectDb.jsp, login.jsp</param-value>


The Filter Class

package user;

import java.util.ArrayList;
import java.util.StringTokenizer;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class UserFilter implements Filter {

    private ArrayList<String> urlList;

    public UserFilter() {

    public void doFilter(ServletRequest req, ServletResponse res,
            FilterChain chain)
            throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;

        String url = request.getServletPath();
        String contextPath = request.getContextPath();
        boolean allowedRequest = false;

        for (String urlList1 : urlList) {
            if (url.contains(urlList1)) {
                allowedRequest = true;
        if (!allowedRequest) {
            HttpSession session = request.getSession();
            if (null == session) {
            } else {
                String logged = (String) session.getAttribute("username");
                if (logged == null) {
                    response.sendRedirect(request.getContextPath() + "/dashboard/SelectDb.jsp");
                } else {
                    chain.doFilter(request, response);
        } else {
            chain.doFilter(request, response);


    public void destroy() {

    public void init(FilterConfig config) {
        String urls = config.getInitParameter("avoid-urls");
        StringTokenizer token = new StringTokenizer(urls, ",");
        urlList = new ArrayList<>();
        while (token.hasMoreTokens()) {


I can see that the page is redirecting properly to the page I want to redirect. The Url is changing. But the Firefox says that the page is not redirecting properly. The Message is somewhat like this: Firefox has detected that the server is redirecting the request for this address in a way that will never complete. This problem can sometimes be caused by disabling or refusing to accept cookies. I do not think that this error is from the browser itself. Because I've tried other browser also. Why this error ? How can Solve this error ?

Автор: Pranjal Choladhara Источник Размещён: 18.07.2016 10:12

Ответы (1)

0 плюса

1 Репутация автора

Try replacing

HttpSession session = request.getSession();


HttpSession session = request.getSession(false);

request.getSession() will create a new session if there is no session yet, so it will never return null, thus the user is redirected to the selectDb url where he has !allowedRequest and its enters and infinite redirection

Автор: lucoram Размещён: 10.08.2016 09:42
Вопросы из категории :