Can not forward after response has been committed to solve the problem

Can not forward after response has been committed problem-solving and analysis

TOMCAT by the system startup, the portal can be a normal landing, landing into when selection subsystem when clicking landing, but go back to the login screen, and so forth is not able to enter the subsystem, background check report errors:

Cannot forward after response has been committed

Chinese meaning has been submitted can not be turned again, and then the error page JSP tag set back to the landing page.

(Core: with a servlet (and there is a redirect or forward the request to the servlet inside) is several visits, which led to the errors above). I see examples of error occurs:

package sc.tl.filter;

import java.io.IOException;
import java.io.PrintWriter;

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.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.websocket.Session;

/**
 * Servlet Filter implementation class adminLogin
 */
@WebFilter("/manage/*")
public class adminLogin implements Filter {
    
    public void destroy() {
        
    }

    /**
     * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
     */
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        //父类转子类
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse res = (HttpServletResponse) response;
        
        //设置字符集
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        
        HttpSession s = req.getSession();
        //获取session中的isAdmin的值
        String flag = (String)s.getAttribute("isAdminLogin");
        
        String spath =  req.getRequestURI().substring(req.getContextPath().length());
        if(spath.contains("admin_")) {
            if(flag!=null && flag.equals("1")) {
                ; the chain.doFilter (Request, Response)
                return ;       // where the aforesaid problem 
            } the else { 
                the PrintWriter OUT = response.getWriter (); 
                out.write ( "<Script>" ); 
                out.write ( "Alert ( 'please login administrator user account '); "! ); 
                out.write ( " the location.href =' the login.jsp '; " ); 
                out.write ( " </ Script> " ); 
                the out.close (); 
                return ; 
            } 
        } 
        the chain.doFilter (Request, Response);
    }

    /**
     * @see Filter#init(FilterConfig)
     */
    public void init(FilterConfig fConfig) throws ServletException {
        // TODO Auto-generated method stub
    }

}

The above code is a filter on a user visits a page administrator. If you are an administrator login and then forwarded to the appropriate servlet or jsp page to go, otherwise pull off access.

And I found a half-day error turned out to be a successful administrator authentication

chain.doFilter (request, response);
then, forget out of the return, once again led to the implementation of the rearmost
chain.doFilter (request, response);
that is, if the administrator can access the servlet and jsp, it will always be accessed twice, it will explode Can not forward error after response has been committed in!
Note own view, such a mistake, certainly not pay attention to that after visiting twice.

Guess you like

Origin www.cnblogs.com/854594834-YT/p/12037810.html