@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		System.out.println("AutoLoginFilter works");
		HttpServletRequest req = (HttpServletRequest) request;
	    HttpServletResponse res = (HttpServletResponse) response;
	    session = req.getSession(true); 	//Don't set it false,or external browser will get null pointer exception
	    //Whenever come in,if session is null,try to login by detecting cookie
        if (session == null || session.getAttribute(com.ccf.action.user.UserLoginAction.USER_SESSION) == null) {
        	Cookie cookies[] = req.getCookies();
        	if(cookies != null) {
        		for(Cookie cookie : cookies) {
            		System.out.println("Got the cookie:" + cookie.getName());
    				if(cookie.getName().equals(com.ccf.util.UserCookieUtil.USER_COOKIE)) {
    					System.out.println("And already existed cookie is " + cookie.getName());
    					System.out.println("already existed cookie's account " + 
    							cookie.getValue().split(",")[0] + cookie.getValue().split(",")[1]);
    					//increase cookie's maxage
    					cookie.setMaxAge(com.ccf.util.UserCookieUtil.USER_COOKIE_MAXAGE);
    					res.addCookie(cookie);
    					//get the user by cookie
    					user = service.login(cookie.getValue().split(",")[0], cookie.getValue().split(",")[1]);
    					System.out.println("user_session add:" + user.getU_name());
    					//then put it to session
    					if(user != null)
    						session.setAttribute(com.ccf.action.user.UserLoginAction.USER_SESSION, user);
    					else
    						chain.doFilter(request, response);	//login wrong
    				}
            	}
        		chain.doFilter(request, response);	//no such name session
        		return;
        	}
        	chain.doFilter(request, response);
        }
        else {
        	System.out.println("straight to the heaven");
        	user = (User) session.getAttribute(com.ccf.action.user.UserLoginAction.USER_SESSION);
        	Cookie cookie = cookieUtil.generateCookie(user);
        	res.addCookie(cookie);
        	chain.doFilter(request, response);	//if session existed,then keep going
        }
	}