使用memcache实现session共享

来源:互联网 发布:nginx配置技巧 编辑:程序博客网 时间:2024/06/05 19:24

session

示例jsp代码:

    String guid = null;/* * 读cookie*/    Cookie[] cookies = request.getCookies();    if (cookies != null) {        for (int i = 0; i < cookies.length; i++) {            Cookie c = cookies[i];            if (c.getName().equalsIgnoreCase("guid")) {                System.out.println("find cookie guid :"+c.getValue());                guid = c.getValue();            }        }    }    /*     * 如果没有发现guid cookie ,生成新的guid,并写cookie    */    if (guid == null) {        RandomGUID randomguid = new RandomGUID();        guid = randomguid.toString();        Cookie guidcookie = new Cookie("guid", guid);        guidcookie.setMaxAge(60 * 60 * 24 * 365);        guidcookie.setDomain(".domain.com");        response.addCookie(guidcookie);        System.out.println("not find cookie guid and set guid cookie:"+guid);    }    String username = request.getParameter("username");    System.out.println("request username : "+username);    if (username != null) {        System.out.println("memcache set username ["+guid+","+username+"]");        MemcachedUtil.put(guid, username);    }    username = (String) MemcachedUtil.get(guid);    System.out.println("memcache username : "+username);
原创粉丝点击