各位兄弟姐妹,小弟刚开始学习java,不懂的还是特别的多,在此谢谢各位的帮忙。我的问题是:如何使用java读取和保存session,如果有源代码的,麻烦发送到.cn我的QQ:316029803再次谢谢各位好心的帮忙。谢谢。

解决方案 »

  1.   

    /*
     * 写cookie
    */
    Cookie namecookie = new Cookie("name",name);
    Cookie passwordcookie = new Cookie("password",password);
    Cookie optioncookie = new Cookie("option","1");//生命周期    
    namecookie.setMaxAge(60*60*24*365);
    passwordcookie.setMaxAge(60*60*24*365);
    optioncookie.setMaxAge(60*60*24*365);
        
    response.addCookie(namecookie);
    response.addCookie(passwordcookie); 
    response.addCookie(optioncookie); /*
     * 读cookie
    */
    Cookie[] cookies = request.getCookies();
    if(cookies!=null)
    {
        String name = "";
        String password = "";
        String option = "";
        for (int i = 0; i < cookies.length; i++)
        {
           Cookie c = cookies[i];     
           if(c.getName().equalsIgnoreCase("name"))
           {
              name = c.getValue();
            }
            else if(c.getName().equalsIgnoreCase("password"))
            {
               password = c.getValue();
            }
            else if(c.getName().equalsIgnoreCase("option"))
            {
               option = c.getValue();
            }     
        } 
      }
      

  2.   

    servlet监听器判断用户的session是否还存import   java.io.*;    
       
      import   java.util.*;    
       
      import   javax.servlet.http.*;    
       
         
       
      //监听登录的整个过程    
       
      public   class   SessionListener   implements    
       
       
      HttpSessionBindingListener    
       
      {    
       
         
       
      public   String   privateInfo="";   //生成监听器的初始化参数字符串    
       
      private   String   logString="";   //日志记录字符串    
       
      private   int   count=0;   //登录人数计数器    
       
         
       
      public   SessionListener(String   info){    
       
      this.privateInfo=info;    
       
      }    
       
         
       
      public   int   getCount(){    
       
      return   count;    
       
      }    
       
         
       
      public   void   valueBound(HttpSessionBindingEvent   event)    
       
      {    
       
      count++;    
       
      if   (privateInfo.equals("count"))    
       
      {    
       
      return;    
       
      }    
       
      try{    
       
      Calendar   calendar=new   GregorianCalendar();    
       
      System.out.println("LOGIN:"+privateInfo+"    
       
       
      TIME:"+calendar.getTime());    
       
      logString="\nLOGIN:"+privateInfo+"   TIME:"+calendar.getTime()  
       
       
      +"\n";    
       
      for(int   i=1;i<1000;i++){    
       
      File   file=new   File("yeeyoo.log"+i);    
       
      if(!(file.exists()))    
       
      file.createNewFile();   //如果文件不存在,创建此文件    
       
      if(file.length()>1048576)   //如果文件大于1M,重新创建一个文件    
       
      continue;    
       
      FileOutputStream   foo=new   FileOutputStream  
       
       
      ("yeeyoo.log"+i,true);//以append方式打开创建文件    
       
      foo.write(logString.getBytes(),0,logString.length());   //写入日志  
       
       
      字符串    
       
      foo.close();    
       
      break;//退出    
       
      }    
       
      }catch(FileNotFoundException   e){}    
       
      catch(IOException   e){}    
       
      }    
       
         
       
      public   void   valueUnbound(HttpSessionBindingEvent   event)    
       
      {    
       
      count--;    
       
      if   (privateInfo.equals("count"))    
       
      {    
       
      return;    
       
      }    
       
      try{    
       
      Calendar   calendar=new   GregorianCalendar();    
       
      System.out.println("LOGOUT:"+privateInfo+"    
       
       
      TIME:"+calendar.getTime());    
       
      logString="\nLOGOUT:"+privateInfo+"   TIME:"+calendar.getTime()  
       
       
      +"\n";    
       
      for(int   i=1;i<1000;i++){    
       
      File   file=new   File("yeeyoo.log"+i);    
       
      if(!(file.exists()))    
       
      file.createNewFile();   //如果文件不存在,创建此文件    
       
      if(file.length()>1048576)   //如果文件大于1M,重新创建一个文件    
       
      continue;    
       
      FileOutputStream   foo=new   FileOutputStream  
       
       
      ("yeeyoo.log"+i,true);//以append方式打开创建文件    
       
      foo.write(logString.getBytes(),0,logString.length());   //写入日志  
       
       
      字符串    
       
      foo.close();    
       
      break;//退出    
       
      }    
       
      }catch(FileNotFoundException   e){}    
       
      catch(IOException   e){}    
       
      }    
       
         
       
      }     引用地址http://blog.chinaunix.net/u1/57965/showart_475701.html
      

  3.   

    servlet里:
    HttpSession session = request.getSession(true);保存用
    session.setAttribute(String arg0, Object arg1)
    读取用
    session.getAttribute(String arg0)
      

  4.   

    存取时:session.setAttribute("list1",list)
    读取时:session.getAttribute("list1")
      

  5.   


    Session session=request.getSession();这样表述最明了  呵呵
      

  6.   

    session对象,是servlet的内置对象,可以直接拿来用:
    HttpSession session = request.getSession(true); 
    session.setAttribute(String arg0, Object arg1) 
    session.getAttribute(String arg0)
      

  7.   


     还不是很明白   这个是写在哪啊   小弟是刚刚接触java   请不要见怪
      

  8.   

    不需要监听器  那样挺复杂的 这样简单!!
    存取时:session.setAttribute("list1",list) 
    读取时:session.getAttribute("list1") 
      

  9.   

    setAttribute和getattribute来实现,session范围内可以存取~
      

  10.   

    HttpSession session = request.getSession(true); 保存用 
    session.setAttribute(String arg0, Object arg1) 
    读取用 
    session.getAttribute(String arg0)