SSH的项目中有没有判断session是否为空的过滤器之类的东西

SSH的项目中,如何统一判断session是否为空,项目中很多操作都需要登录后才能执行,不想在每个action中都写判断session是否为空的语句,有没有什么拦截器,或者spring的什么配置可以搞定这个的?
[b]问题补充:[/b]
写了个struts2 的拦截器,代码如下:
@Override
public String intercept(ActionInvocation arg0) throws Exception {
Logger logger = Logger.getLogger(this.getClass());
// TODO Auto-generated method stub
Corp corp = (Corp)ActionContext.getContext().getSession().get("corp");
if(corp==null){

        logger.info("strus 拦截器 corp 为空");
        return "none";
    }
    logger.info("struts 拦截器 invoke:返回"+arg0.invoke());      
    return arg0.invoke();   
}

结果出现了个异常如下:

java.lang.IllegalStateException
at org.apache.catalina.connector.ResponseFacade.sendError(ResponseFacade.java:407)
at org.apache.struts2.dispatcher.Dispatcher.sendError(Dispatcher.java:725)
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:485)
at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:76)
at com.cool.filter.MyFilter.doFilter(MyFilter.java:61)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.struts2.dispatcher.ActionContextCleanUp.doFilter(ActionContextCleanUp.java:102)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at com.cool.filter.MyCharacterEncodingFilter.doFilterInternal(MyCharacterEncodingFilter.java:58)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:263)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:584)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Unknown Source)
[b]问题补充:[/b]
哪个能帮忙搞定,不明白为什么会和struts2的过滤器相关,myfilter是我自己的过滤器,集成struts2的过滤器。试过了只要去掉拦截器就不会出现这个错误。
[b]问题补充:[/b]
蔡华江同学,还是一样的错误哦,这个和session没有什么关系吧,网上都说是调用了两次response,我很费解,如果没有登录返回none的时候没有错误,登录后session不为空了反倒出了这个错误,你再帮我看看吧,我实在搞不定了,我看好你哦。

6个回答

public class MyInterceptor implements Interceptor {

public String intercept(ActionInvocation arg0) throws Exception 
{
    System.out.println("interceptor");

    Map map= arg0.getInvocationContext().getSession();

    if(map.get("userName")==null)
    {
        return Action.LOGIN;
    }
    else
    {
        return arg0.invoke();
    }
}

}

写个拦截器。在拦截器中判断。

哈哈,对不起,我还没说完。在配置好拦截器后,对你每个要拦截是action加上这个拦截器,在定义一个全局的login。


/login.jsp


/welcome.jsp
/login.jsp
/login.jsp
/login.jsp

web.xml中
[code="xml"]
sessionfilter
com.servlet.SessionFilter


sessionfilter
.jsp


sessionfilter
.do
[/code]

com.servlet.SessionFilter中
[code="java"]package com.servlet;

import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class SessionFilter extends HttpServlet implements Filter {
private FilterConfig filterConfig;
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
}

public void doFilter(ServletRequest request, ServletResponse response,
                     FilterChain filterChain) {
    HttpServletRequest req = (HttpServletRequest) request;
    HttpServletResponse res = (HttpServletResponse) response;
    try {
        String url = req.getRequestURL().toString();
        if (url != null) {
            if (url.indexOf("loginAction.do") == -1 && url.indexOf("index.jsp") == -1 &&
                url.indexOf("license.jsp") == -1 && url.indexOf("licenseAction.do") == -1) {
                HttpSession session = req.getSession();
                if (会话检验不通过) {
                    res.sendRedirect("/index.jsp");
                    return;
                }
            }
        }
        filterChain.doFilter(request, response);
    } catch (ServletException sx) {
        filterConfig.getServletContext().log(sx.getMessage());
    } catch (IOException iox) {
        filterConfig.getServletContext().log(iox.getMessage());
    }
}

public void destroy() {}

}[/code]

session的取出方式是错的.
[code="java"]package com.flyy.web.interceptor;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class AuthorizationInterceptor extends AbstractInterceptor {
private String gn;

public String intercept(ActionInvocation ai) throws Exception {
    Map sess = ai.getInvocationContext().getSession();
    HttpServletRequest request = (HttpServletRequest) ai.getInvocationContext().get(ServletActionContext.HTTP_REQUEST);
    if (校验通过) {
                return ai.invoke();
    } else {
        return "跳转错误页面";
    }
}

}[/code]

那原因就是你的用户信息放错了session,你放到了httpservletsession中去了。但是你使用的却是struts2的session map.
改为
[code="java"]HttpServletRequest request = (HttpServletRequest) ai.getInvocationContext().get(ServletActionContext.HTTP_REQUEST);
HttpServletSession session = request.getSession();[/code]

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问