1

i have a filter that checks if a session exists. If it doesn't exist i redirect the user, with a response.sendRedirect(), to the login page.

In some pages it works, in others it doesnt'. I get this error Cannot call sendRedirect() after the response has been committed.

It seems that if i delete some parts of code like <%=variable%> in my jsp pages it works. But, as i said, in other jsp pages it works even if i have code like <%=variable%>. I really can't understand what's the problem.

I read some posts similar on this forum but i didn't find a solution.

This is my filter

public class SessionFilter implements Filter {

    public void destroy() {}

    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {

        try {
            HttpServletRequest request = (HttpServletRequest) req;
            HttpServletResponse response = (HttpServletResponse) res;
            String url = request.getServletPath();

            HttpSession session = request.getSession(false);

            /*caso in cui il webserver non abbia ancora creato la sessione*/
            if (null == session) {
                reindirizza = true;
            } else {
                /*caso in cui il webserver abbia creato la sessione e l'utente  sia loggato*/
                if(session.getAttribute("loggato")!=null && session.getAttribute("loggato").equals("si")) {
                    reindirizza = false;
                }
                /*caso in cui il webserver abbia creato la sessione e l'utente  non sia loggato*/
                else {
                    reindirizza = true;
                }
            }

            chain.doFilter(req, res);
            if(reindirizza) {
                response.sendRedirect("Gestione.jsp?message=Sessione scaduta o non valida. Effettuare il Login");
            }
        } catch(Exception e) {
            System.out.println(e.getMessage());
        }
    }

    public void init(FilterConfig config) throws ServletException { }
}
Vasil Lukach
  • 3,658
  • 3
  • 31
  • 40
MDP
  • 4,177
  • 21
  • 63
  • 119
  • You should show some relevant code.. – Adil Shaikh Dec 01 '13 at 18:13
  • Did you try to debug? – Keerthivasan Dec 01 '13 at 18:28
  • I tried but i can't understand where to set a checkpoint. If i delete piece of html code, like div, the it works, after i delete a lot of code. But in my html code there isnt any error. i checked it with the w3c validator. It seems to be a 'random error' – MDP Dec 01 '13 at 19:51
  • i copied some parts of the code into pages with less html code, and i had no problem. It seems the error concerns the amount of code written in a page. – MDP Dec 01 '13 at 19:59

3 Answers3

3

I also faced the same issue after response.sendRedirect("jsppage") you should return it like

return;

Add above statement after sendredirect method then No Issue.

No Idea For Name
  • 11,411
  • 10
  • 42
  • 70
D8Sasi
  • 79
  • 6
0

i also used to get same type of issue i.e Cannot call sendRedirect()/forward after the response has been committed

error is bit misleading but actual issued comes out to be some improper usage of jstl tag in jsp. For example tag did not terminate correctly. So check if your all tags are in proper shape

M Sach
  • 33,416
  • 76
  • 221
  • 314
0

Here, you would need to close all out.println and out.flush statements that you have left open in your jsp. This should solve your issue.

Michele La Ferla
  • 6,775
  • 11
  • 53
  • 79