if (needExclude) { log.debug("拦截......"); if (session.getAttribute("sysUser") == null) { log.debug("未登录..."); try { response.sendRedirect("/index"); //target = "/index"; } catch (Exception e) { e.printStackTrace(); } }else { log.debug("登陆放行......"); } } log.debug("下一个handler......"); next.handle(target, request, response, isHandled);
上面代码 会报错
2016-11-04 15:01:59 [] [] [DEBUG]-[Thread: qtp2112978551-22]-[com.lt.handler.PermissionHandler.handle()]: target:/ser/2231 2016-11-04 15:01:59 [] [] [DEBUG]-[Thread: qtp2112978551-22]-[com.lt.handler.PermissionHandler.handle()]: 拦截...... 2016-11-04 15:01:59 [] [] [DEBUG]-[Thread: qtp2112978551-22]-[com.lt.handler.PermissionHandler.handle()]: 未登录... 2016-11-04 15:01:59 [] [] [DEBUG]-[Thread: qtp2112978551-22]-[com.lt.handler.PermissionHandler.handle()]: 下一个handler...... JFinal action report -------- 2016-11-04 15:01:59 ------------------------------ Controller : com.lt.controller.SerController.(SerController.java:1) Method : Ser2231 Interceptor : com.lt.Interceptor.ExceptionInterceptor.(ExceptionInterceptor.java:1) -------------------------------------------------------------------------------- 2016-11-04 15:01:59 [] [] [ERROR]-[Thread: qtp2112978551-22]-[com.jfinal.core.ActionHandler.handle()]: /ser/2231 com.jfinal.render.RenderException: java.lang.IllegalStateException: Committed at com.jfinal.render.JspRender.render(JspRender.java:69) at com.jfinal.core.ActionHandler.handle(ActionHandler.java:97) at com.lt.handler.PermissionHandler.handle(PermissionHandler.java:55) at com.jfinal.ext.handler.ContextPathHandler.handle(ContextPathHandler.java:47) at com.jfinal.core.JFinalFilter.doFilter(JFinalFilter.java:72) at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1307) at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:453) at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:137) at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:560) at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:231) at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1072) at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:382) at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:193) at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1006) at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:135) at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:116) at org.eclipse.jetty.server.Server.handle(Server.java:365) at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:485) at org.eclipse.jetty.server.AbstractHttpConnection.headerComplete(AbstractHttpConnection.java:926) at org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.headerComplete(AbstractHttpConnection.java:988) at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:635) at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:235) at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:82) at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:628) at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:52) at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:608) at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:543) at java.lang.Thread.run(Thread.java:662) Caused by: java.lang.IllegalStateException: Committed at org.eclipse.jetty.server.Response.resetBuffer(Response.java:1136) at org.eclipse.jetty.server.Dispatcher.forward(Dispatcher.java:204) at org.eclipse.jetty.server.Dispatcher.forward(Dispatcher.java:103) at com.jfinal.render.JspRender.render(JspRender.java:67) ... 27 more
没有重定向,不知道为什么,程序继续执行。
在重定向下面加上return;还是会报错
2016-11-04 15:07:30 [] [] [DEBUG]-[Thread: qtp2112978551-22]-[com.lt.handler.PermissionHandler.handle()]: target:/ser/2231 2016-11-04 15:07:30 [] [] [DEBUG]-[Thread: qtp2112978551-22]-[com.lt.handler.PermissionHandler.handle()]: 拦截...... 2016-11-04 15:07:30 [] [] [DEBUG]-[Thread: qtp2112978551-22]-[com.lt.handler.PermissionHandler.handle()]: 未登录... 2016-11-04 15:07:30.718:WARN:oejs.Response:Committed before 404 null 2016-11-04 15:07:30.718:WARN:oejs.ServletHandler:/lt/ser/2231 java.lang.IllegalStateException: Committed at org.eclipse.jetty.server.Response.resetBuffer(Response.java:1136) at org.eclipse.jetty.server.Response.sendError(Response.java:314) at org.eclipse.jetty.server.Response.sendError(Response.java:416) at org.eclipse.jetty.servlet.DefaultServlet.doGet(DefaultServlet.java:486) at javax.servlet.http.HttpServlet.service(HttpServlet.java:735) at javax.servlet.http.HttpServlet.service(HttpServlet.java:848) at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:669) at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1336) at com.jfinal.core.JFinalFilter.doFilter(JFinalFilter.java:82) at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1307) at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:453) at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:137) at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:560) at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:231) at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1072) at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:382) at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:193) at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1006) at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:135) at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:116) at org.eclipse.jetty.server.Server.handle(Server.java:365) at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:485) at org.eclipse.jetty.server.AbstractHttpConnection.headerComplete(AbstractHttpConnection.java:926) at org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.headerComplete(AbstractHttpConnection.java:988) at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:635) at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:235) at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:82) at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:628) at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:52) at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:608) at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:543) at java.lang.Thread.run(Thread.java:662)
使用target可以跳转。
以前的帖子说target不能跳转,要用重定向。现在反过来有点懵。。。。
其次需要使用 isHandled[0] = true,告知 tomcat jetty 不要再接管本次请求
最后,可以使用 jfinal 提供的 HandlerKit.redirect(...) 等方法做这类事情,不需要再使用 isHandled[0] = true,简单省事。但仍然要确定不要让 next.handle(...) 这个方法传递本次请求,注意用好 return