1.获取HttpSession对象:
HttpSession session = request.getSession();
2.使用HttpSession对象:
Object getAttribute(String name) //获取session对象
void setAttribute(String name, Object value) //设置session对象
void removeAttribute(String name) //删除session对象
3.当客户端关闭后,服务器不关闭,两次获取的session不是同一个
如果需要相同,则可以创建Cookie,键为JSESSIONID,设置最大存活时间,永久保存
Cookie c = new Cookie("JSESSIONID",session.getId());
c.setMaxAge(60*60);
response.addCookie(c);
4.session的销毁时间
服务器关闭
session对象调用invalidate()
session默认失效时间是30分钟
选择性配置修改
<session-config>
<session-timeout>30</session-timeout>
</session-config>
-
session的特点
- session用于存储一次会话的多次请求的数据,存在服务器端
- session可以存储任意类型,任意大小的数据
- session与Cookie的区别:
- session存储数据在服务器端,Cookie在客户端
- session没有数据大小限制,Cookie有
- session数据安全,Cookie相对于不安全
6.实现session共享的代码:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import redis.clients.jedis.Jedis;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
@Controller
public class ItemController3 {
@Autowired
private RedisTemplate redisTemplate;
public ItemController3(RedisTemplate redisTemplate) {
this.redisTemplate = redisTemplate;
}
/*@RequestMapping("/query")
public ModelAndView query(HttpServletRequest request){
HttpSession session = request.getSession();
request.setAttribute("name", "zhangsan");
this.saveSession(session);
ModelAndView mv=new ModelAndView();
mv.setViewName("item");
return mv;
}
*/
@RequestMapping("/saveSession")
public void saveSession(HttpSession session) {
redisTemplate.boundHashOps(“userMap1”).put(“userSession”, session);
}
@RequestMapping("/query")
public ModelAndView query(HttpServletRequest request) {
Jedis jedis = new Jedis("127.0.0.1", 6379);
HttpSession session = request.getSession();
System.out.println(session);
//session.setAttribute("name","lisi");
jedis.set("userMap", String.valueOf(session));
jedis.set("username","zhangsan");
jedis.set("password","123456");
// redisTemplate.boundHashOps("userMap").put("userSession",session);
ModelAndView mv = new ModelAndView();
mv.setViewName("item");
return mv;
}
@RequestMapping("/getSession")
public ModelAndView getSession() {
Jedis jedis = new Jedis("127.0.0.1",6379);
String session = jedis.get("userMap");
System.out.println(session);
String username = jedis.get("username");
System.out.println(username);
String password = jedis.get("password");
System.out.println(password);
// HttpSession session = (HttpSession) redisTemplate.boundHashOps("userMap");
// System.out.println(session);
if (session == null) {
throw new RuntimeException("没有对应的数据");
}
ModelAndView mv = new ModelAndView();
mv.setViewName("item");
return mv;
}
}