如何使用java读取和保存session 各位兄弟姐妹,小弟刚开始学习java,不懂的还是特别的多,在此谢谢各位的帮忙。我的问题是:如何使用java读取和保存session,如果有源代码的,麻烦发送到.cn我的QQ:316029803再次谢谢各位好心的帮忙。谢谢。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 /* * 写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(); } } } 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 servlet里:HttpSession session = request.getSession(true);保存用session.setAttribute(String arg0, Object arg1)读取用session.getAttribute(String arg0) 存取时:session.setAttribute("list1",list)读取时:session.getAttribute("list1") Session session=request.getSession();这样表述最明了 呵呵 session对象,是servlet的内置对象,可以直接拿来用:HttpSession session = request.getSession(true); session.setAttribute(String arg0, Object arg1) session.getAttribute(String arg0) 还不是很明白 这个是写在哪啊 小弟是刚刚接触java 请不要见怪 不需要监听器 那样挺复杂的 这样简单!!存取时:session.setAttribute("list1",list) 读取时:session.getAttribute("list1") setAttribute和getattribute来实现,session范围内可以存取~ HttpSession session = request.getSession(true); 保存用 session.setAttribute(String arg0, Object arg1) 读取用 session.getAttribute(String arg0) -verbose:gc Xms这些参数有规范吗 JAVA中学习GUI重要吗? 求一个可以返回天数的的方法! 按照时间查询数据的问题 try catch finally 执行顺序? 请教如何实现替换字符的问题 关于win32com.dll 的版本问题 一起来讨论下日志吧(java核心技术卷1 日志的例子 看不懂) 请大家推荐一个管用的混淆器,最好有下载URL。谢谢! pdf文件不能打印,该如何解决?<jbuilder的开发指南> 小测试,没有什么意义,只是无聊. "class " or "interface"expected
* 写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();
}
}
}
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
HttpSession session = request.getSession(true);保存用
session.setAttribute(String arg0, Object arg1)
读取用
session.getAttribute(String arg0)
读取时:session.getAttribute("list1")
Session session=request.getSession();这样表述最明了 呵呵
HttpSession session = request.getSession(true);
session.setAttribute(String arg0, Object arg1)
session.getAttribute(String arg0)
还不是很明白 这个是写在哪啊 小弟是刚刚接触java 请不要见怪
存取时:session.setAttribute("list1",list)
读取时:session.getAttribute("list1")
session.setAttribute(String arg0, Object arg1)
读取用
session.getAttribute(String arg0)