cookie 和sission的用法 及区别
哪位高手能帮我解释一下
------解决方案--------------------Cookie是保存在客户端的一小段信息,当请求时,可以读取这个信息发送到服务器端,它是由一个键值对组成的
//创建cookie
Cookie cookie = new Cookie( "userName ", "password ");
cookie.setMaxAge(60*1);
response.addCookie(cookie);
//显示Cookie
Cookie[] cookies = request.getCookies();
if(cookies!=null){
for(int i=0;i <cookies.length;i++){
cookie = cookies[i];
out.println(cookie.getName());
out.println(cookie.getValue());
}
}
session是指客户端与服务器端的请求和响应的一段时间.
String name = "123 ";
String password= "123 ";
HttpServletSession session = request.getSession();
session.setAttribute( "userName ",name);
session.setAttribute( "password ",password);
String name = session.getAttribute( "userName ");
String password = session.getAttribute( "password ");