日期:2014-05-19  浏览次数:20949 次

JSP结合struts判断用户是否登录?
怎样实现呢?

------解决方案--------------------
拦截器。
------解决方案--------------------
顶顶楼上的

Java code
import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;

public class Filter extends HttpServlet implements javax.servlet.Filter{

    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {
        // TODO Auto-generated method stub
        //if(true) ;
        //else ;
        chain.doFilter(request, response);
        
    }
    public void init(FilterConfig filterConfig) throws ServletException {
        // TODO Auto-generated method stub
        
    }
}

------解决方案--------------------
Java code

    <interceptors>
            <!-- 配置普通用户是否登录的拦截器 -->
            <interceptor name="userAuth"
        class="xxxx.authority.UserAuthorityInterceptor" />
            <!-- 配置普通用户的默认的拦截器栈 -->
            <interceptor-stack name="userStack">
                <interceptor-ref name="defaultStack" />
                <interceptor-ref name="userAuth" />
            </interceptor-stack>
            </interceptors>

类:
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
/**
 * @author closewbq
 */
public class UserAuthorityInterceptor extends AbstractInterceptor {

    private static final long serialVersionUID = 1L;

    /**
     * 拦截器,判断用户是否登录
     */
    public String intercept(ActionInvocation invocation) throws Exception {
        ActionContext ctx = ActionContext.getContext();
        String login=(String) ctx.getSession().get(WebConstant.LOGIN);
        if (login == null || !WebConstant.LOGIN.equals(login)) {
            return Action.LOGIN;
        }
        return invocation.invoke();
    }
}
//WebConstant.LOGIN就是代表了一个常量,当你登录工程的时候回放入这个常量到session中。

------解决方案--------------------
写一个session过滤器,判断session中的登录对象是否为空;或者写个baseAction,让所有的action都继承它,在baseAction判断session中的登录对象是否为空