当前页面: 开发资料首页 → JSP 专题 → Session Filter问题~!!!
Session Filter问题~!!!
摘要: Session Filter问题~!!!
用sessionfilter 过滤为登陆 或 session过期的用户 如果session中内容为空
就 .sendRedirect(httprequest.getContextPath()+ "/index.jsp");
在为登陆的情况下 访问 *.jsp 文件就可以送回 index.jsp 如过是 action 动作 就会报错~!!
java.lang.IllegalStateException: Cannot forward after response has been committed
请问 怎样 才能过滤 action(*.do)的请求~!!!
sessionfilter中内容
.....
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws java.io.IOException, ServletException {
HttpSession session = httprequest.getSession();
HttpServletResponse httpresponse = (HttpServletResponse) response;
String UserId=(String)session.getAttribute("UserId");
if(UserId==null){
httpresponse.sendRedirect(httprequest.getContextPath()+ "/index.jsp");
}
chain.doFilter(request, response);
}
.......
/////////////
web.xml
web-app_2_4.xsd
......
Session_filter
com.hdtq.filters.SessionFilter
Session_filter
/main/*
........
请 各位大侠帮帮忙 ~!解决问题 立刻 结贴~!!!
可以设置一个isLogin标志,看看下面例子:
public void doFilter(final ServletRequest req,final ServletResponse res,FilterChain chain)throws IOException,ServletException
{
HttpServletRequest hreq = (HttpServletRequest)req;
HttpServletResponse hres = (HttpServletResponse)res;
HttpSession session = hreq.getSession();
String isLogin="";
try
{
isLogin=(String)session.getAttribute("isLogin");
if(isLogin.equals("true"))
{
System.out.println("在SignonFilter中验证通过");
//验证成功,继续处理
chain.doFilter(req,res);
}
else
{
//验证不成功,让用户登录。
hres.sendRedirect(LOGIN_PAGE);
System.out.println("被SignonFilter拦截一个未认证的请求");
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void setFilterConfig(final FilterConfig filterConfig)
{
this.filterConfig=filterConfig;
}
//销毁过滤器
public void destroy()
{
this.filterConfig=null;
}
/**
*初始化过滤器,和一般的Servlet一样,它也可以获得初始参数。
*/
public void init(FilterConfig config) throws ServletException {
this.filterConfig = config;
}
}
过滤 action(*.do)的请求之只需要把你的filter的URL过滤选项设定为*.do
同意楼上
好象在 RequestProcessor 的 processPreprocess()方法中也可以过滤
它执行后才有可能会去调用Action的execute()方法
如果 设置为*.do 当遇见*.do的时候 还是 报错 是不是 还是先通过 ActionServlet来处理请求了???
java.lang.IllegalStateException;
if(UserId==null){
httpresponse.sendRedirect(httprequest.getContextPath()+ "/index.jsp");
return ; //////记住加上这个!!!!!!!!!!!!!!!!!!!!!!
}
chain.doFilter(request, response);
好 我试试
to :pigo() 好使了
在请教个问题,
如何 控制 .sendRedirect的 打开方式 target,因为是有的是 iframe 需要设置打开方式应该怎么做~!!!!!!!!!!就差这个了
问题已经解决 结贴~!