比如在login.java中将登录用户信息写入session,在action中每次都先取出用户信息以判断权限?使用session需要import哪些包?最好给个例子,谢谢。

解决方案 »

  1.   

    java的api里面都有啊
    你用的时候自动导入就可以
    package com.hy.struts.action;import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;import org.apache.struts.action.Action;
    import org.apache.struts.action.ActionForm;
    import org.apache.struts.action.ActionForward;
    import org.apache.struts.action.ActionMapping;import com.hy.struts.form.CsdnForm;public class CsdnAction extends Action {
    public ActionForward execute(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response) {
    CsdnForm csdnForm = (CsdnForm) form;// TODO Auto-generated method stub
    HttpSession hs = request.getSession();
    hs.getAttribute(""); return null; }
    }其他的信息你自己搞定就可以了啊
      

  2.   

    //导入这个包
    import javax.servlet.http.HttpSession;HttpSession   session =   request.getSession(); 
    然后像JSP里面一样直接用session就好了,当然也可以换成别的名字
      

  3.   

    一般不在Action中做这件事,这样太麻烦了,万一有一百个Action那得重复写一百遍的。可以配置一个Filter,在Filter中做这件事。
      

  4.   

    做一个Filter,在web.xml中配置一下。Filter中的doFilter方法大致如下:public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException { 
        
        HttpServletRequest httpRequest = (HttpServletRequest)request;
        HttpServletResponse httpResponse = (HttpServletResponse) response;
        HttpSession session = httpRequest.getSession();
        if(session.getAttribute(ApplicationSource.USER) == null
                && !httpRequest.getRequestURI().endsWith("/login.jsp") // 这些都是一些需要排除的页面,避免出现死循环
                  && !httpRequest.getRequestURI().endsWith("/calendar/")
                && (httpRequest.getRequestURI().indexOf("/calendar/img") < 0)
                && (httpRequest.getRequestURI().indexOf("/calendar/resources") < 0)
                && !httpRequest.getRequestURI().endsWith("/login.do")){
            // 除了以上的这些页面和资源外,对于其他的页面,凡session中没有用户存在,就转到登录页面
            httpResponse.sendRedirect(httpRequest.getContextPath() + "/login.jsp");
        }
        // 否则的话将请求发送给接收方,过滤器好比是页面与Action中的一层滤网
        chain.doFilter(request, response);