java.lang.IllegalStateException:在servletfilter中提交响应后无法转发

时间:2014-03-20 09:04:04

标签: java jsp servlet-filters

我正在使用过滤器来确定请求的页面是否具有有效会话。

这是我的代码。 web.xml

    <filter>
    <filter-name>SessionFilter</filter-name>
    <filter-class>
        com.imagemanagementutility.filter.SessionFilter
    </filter-class>
    <init-param>
        <param-name>avoid-urls</param-name>
        <param-value>index.jsp</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>SessionFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

过滤类:

    public class SessionFilter implements Filter {

    private ArrayList<String> urlList;

    public void destroy() {
    }

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

        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;
        String url = request.getServletPath();
        boolean allowedRequest = false;
        System.out.println(url.replace("/",""));
        if (urlList.contains(url)) {
            allowedRequest = true;
        }

        if (!allowedRequest) {
            HttpSession session = request.getSession(false);
            if (null == session) {
                System.out.println("redirect in servlet session filter");
                RequestDispatcher dispatcher = request
                        .getRequestDispatcher("//index.jsp");
                dispatcher.forward(request, response);
            }
        }

            chain.doFilter(req, res);

        System.out.println("end");
    }

    public void init(FilterConfig config) throws ServletException {
        String urls = config.getInitParameter("avoid-urls");
        StringTokenizer token = new StringTokenizer(urls, ",");

        urlList = new ArrayList<String>();

        while (token.hasMoreTokens()) {
            urlList.add(token.nextToken());

        }
    }
}

我有一个登录页面,用于检查用户是否有效。 如果用户有效,则控制权转移到此servlet。

    public class MainService extends HttpServlet {
    private static final long serialVersionUID = 1L;
    static Logger log = Logger.getLogger(MainService.class.getName());

    /**
     * @see HttpServlet#HttpServlet()
     */
    public MainService() {
        super();
    }
/*
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {

    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession(true);
        response.setContentType("text/html;charset=UTF-8");
        LogUtil.logInfo("i m here");
        List<Images> imageList = null;
        response.setContentType("text/html");
        try {
            UserDataService userDataService = new UserDataImpl();



            String userId = null;
            String username = null;
            String password = null;
            username = request.getParameter("user");
            password = request.getParameter("password");

            userId = userDataService.checkUser(username, password);

            if (userId!=null) {

            System.out.println(userId);
            session.setAttribute("userId", userId);

                imageList = userDataService.getImages(userId);
                session.setAttribute("imageList", imageList);


            /*response.sendRedirect("//showUserImages.jsp");*/

                RequestDispatcher dispatcher = request
                        .getRequestDispatcher("//showUserImages.jsp");
                dispatcher.forward(request, response);


            } else {

                /*response.sendRedirect("//index.jsp");*/

                RequestDispatcher dispatcher = request
                        .getRequestDispatcher("//index.jsp");
                dispatcher.forward(request, response);

            }
        } catch (ImageException e) {

            e.getMessage();
            e.printStackTrace();
        }
    }

}

没有过滤器,但过滤器显示错误 当我将控制转发到“//showuserImages.jsp”。

1 个答案:

答案 0 :(得分:0)

doFilter的调用放在else块中:

if (!allowedRequest) {
    HttpSession session = request.getSession(false);
    if (null == session) {
        System.out.println("redirect in servlet session filter");
        RequestDispatcher dispatcher = request
                .getRequestDispatcher("//index.jsp");
        dispatcher.forward(request, response);
    }
}else{
    chain.doFilter(req, res);
}