Java 响应提交后无法调用 sendRedirect()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20315470/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 01:01:45  来源:igfitidea点击:

Cannot call sendRedirect() after the response has been committed

javajspservletsservlet-filters

提问by MDP

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.

我有一个过滤器来检查会话是否存在。如果它不存在,我将用户重定向response.sendRedirect()到登录页面。

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

在某些页面中它有效,而在其他页面中则不起作用'。我收到此错误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.

似乎如果我删除一些像<%=variable%>在我的 jsp 页面中那样的代码,它就可以工作。但是,正如我所说,在其他 jsp 页面中,即使我有类似<%=variable%>. 我真的不明白有什么问题。

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 { }
}

回答by M Sach

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

我也曾经遇到过相同类型的问题,即响应提交后无法调用 sendRedirect()/forward

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

错误有点误导,但实际发出的是jsp中jstl标签的一些不正确使用。例如标签没有正确终止。因此,请检查您的所有标签是否形状正确

回答by Souvanik Roy

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.

在这里,您需要关闭在 jsp 中保持打开状态的所有 out.println 和 out.flush 语句。这应该可以解决您的问题。

回答by D8Sasi

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

response.sendRedirect("jsppage")您应该将其退回后,我也遇到了同样的问题

return;

Add above statement after sendredirectmethod then No Issue.

sendredirect方法之后添加上面的语句,然后没有问题。