这种做法不好,请使用filter。请尽量避免使用session,开销过大

解决方案 »

  1.   

    直接用一个过滤器filter
    就可以验证所有页面了……
      

  2.   

    request.getQueryString()获得参数部分
      

  3.   

    //sessionlistener
    public class SessionListener implements HttpSessionListener  {
       //session建立的时候将username存一个空字符串进去    public void sessionCreated(HttpSessionEvent e){
            HttpSession session = e.getSession();
            session.setAttribute("username","");      
        }
        
       public void sessionDestroyed(HttpSessionEvent e){
            HttpSession session = e.getSession();
            session.removeAttribute("username");
       }
    }//session filter
    public class SessionFilter implements Filter{
        public void init(FilterConfig fc){
        }
        
        public void doFilter(ServletRequest req,ServletResponse rsp,FilterChain chain){
            HttpServletRequest request = (HttpServletRequest)req;
            HttpServletResponse response = (HttpServletResponse)rsp;
            
            javax.servlet.http.HttpSession session = request.getSession();
            javax.servlet.ServletContext application = session.getServletContext();
            
            String username =(String)session.getAttribute("username");
            try {
                //只有初始化的session里面的username才是0
                if(username.length()==0)
                //回到首页
                    response.sendRedirect(request.getContextPath()+"/index.html");
                else 
                    chain.doFilter(req,rsp);
            } catch (IOException ex) {
                ex.printStackTrace();
            } catch (ServletException ex) {
                ex.printStackTrace();
            }
            
            
        }
        
        public void destroy(){
        
        }
    }
      

  4.   

    忘了一句,你要把登陆页面和登陆后可访问的页面分开。。
    然后配置xml里的urlpatten是可访问的文件夹就可以了
      

  5.   

    如果需要返回到登陆前的页面
    那就在session里存一下当前url,登陆后自动回去就是了-。-