如何在servlet过滤器中重定向? [英] How to redirect in a servlet filter?
本文介绍了如何在servlet过滤器中重定向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试找到一种方法将我的请求从过滤器重定向到登录页面,但我不知道如何从servlet重定向。我搜索了但我找到的是 sendRedirect()
方法。我在过滤器中的响应对象上找不到这个方法。原因是什么?我该如何解决这个问题?
I'm trying to find a method to redirect my request from filter to login page but I don't know how to redirect from servlet. I searched but what I find is sendRedirect()
method. I can't find this method on my response obect in filter. What's the cause? How can I solve this?
推荐答案
在过滤器中,响应是 ServletResponse
而不是 HttpServletResponse
.Hence执行转换为 HttpServletResponse
。
In Filter the response is of ServletResponse
rather than HttpServletResponse
.Hence do the cast to HttpServletResponse
.
HttpServletResponse httpResponse = (HttpServletResponse) response;
httpResponse.sendRedirect("/login.jsp");
如果使用上下文路径:
httpResponse.sendRedirect(req.getContextPath() + "/login.jsp");
也不要忘记致电 return;
at at结束。
Also dont forget to call return;
at the end.
这篇关于如何在servlet过滤器中重定向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文