SpringMVC操作Session和Cookie

本文详细介绍了如何在SpringMVC框架中操作Session和Cookie,包括使用HttpServletRequest/HttpSession作为参数以及利用@CookieValue注解获取Cookie数据的方法。
摘要由CSDN通过智能技术生成

SpringMVC操作 Session 和 Cookie

1. 操作 Session

方式一:方法中声明、使用 HttpServletRequest 参数

这种情况下,Spring MVC 在调用请求处理方法时,会传入本次请求的 Request 对象,后续可以像普通同的 Servlet 代码一样操作 Session 。

@RequestMapping("/...")
public String demo1(HttpServletRequest request, ...) {
  HttpSession session = request.getSession();
  session.setAttribute(..., ...);
  ...
}

方式二:方法中直接声明、使用 HttpSession 参数

该方式是 方式一 的改进版。直接要求 SpringMVC 『帮』我们去调用 request.getSession() 后再传入到请求处理方法中。

@RequestMapping("/...")
public String login(HttpSession session, ...) {
    ....
}

2. 操作 Cookie

在 Spring MVC 中操作 Cookie 和操作 Session 非常类似,你可以要求 SpringMVC 将当前请求的 Response 对象传入你的方法中,而后对其添加 Cookie :

@RequestMapping("...")
public String demo1(HttpServletResponse resp) {
  Cookie cookie = new Cookie("name", "hello world");
  cookie.setMaxAge(60);
  resp.addCookie(cookie);
}

Spring MVC 提供了一个 @CookieValue 注解来简化『获取』客户端『传入』的 Cookie 数据:

@RequestMapping("...")
public String demo2(@CookieValue("name") String value) {
  System.out.println(value);
  ...
}
  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 添加Redis依赖库 在项目的pom.xml文件中添加Redis的依赖库。 ```xml <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>2.3.2.RELEASE</version> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>3.0.0</version> </dependency> ``` 2. 配置Redis连接池 在SpringMVC的配置文件中添加Redis的连接池配置。 ```xml <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <property name="hostName" value="localhost"/> <property name="port" value="6379"/> <<property name="password" value="password"/>> <property name="poolConfig"> <bean class="redis.clients.jedis.JedisPoolConfig"> <property name="maxTotal" value="1000"/> <property name="maxIdle" value="100"/> <property name="minIdle" value="10"/> <property name="maxWaitMillis" value="10000"/> </bean> </property> </bean> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="jedisConnectionFactory"/> </bean> ``` 3. 配置Session共享 在SpringMVC的配置文件中添加Session共享的配置。 ```xml <bean id="redisHttpSessionConfiguration" class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration"> <property name="maxInactiveIntervalInSeconds" value="1800"/> </bean> <bean class="org.springframework.session.web.http.CookieHttpSessionIdResolver"> <constructor-arg name="cookieName" value="SESSION_ID"/> <property name="cookiePath" value="/"/> <property name="cookieMaxAge" value="-1"/> </bean> ``` 4. 启用Session共享 在SpringMVC的配置文件中启用Session共享。 ```xml <bean class="org.springframework.session.web.http.SessionRepositoryFilter"> <constructor-arg> <bean class="org.springframework.session.data.redis.RedisOperationsSessionRepository"> <constructor-arg ref="redisTemplate"/> </bean> </constructor-arg> </bean> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> <property name="sessionAttributeStore"> <bean class="org.springframework.session.web.http.SessionRepositoryFilter"> <constructor-arg> <bean class="org.springframework.session.data.redis.RedisOperationsSessionRepository"> <constructor-arg ref="redisTemplate"/> </bean> </constructor-arg> </bean> </property> </bean> ``` 5. 测试 启动项目后,访问任意一个页面,然后在Redis中就可以看到Session的信息了。 ```shell redis-cli 127.0.0.1:6379> keys * 1) "spring:session:sessions:5b5d4b4f-9a9f-4e4e-bd56-4b4c7f1d8f0b" 127.0.0.1:6379> get spring:session:sessions:5b5d4b4f-9a9f-4e4e-bd56-4b4c7f1d8f0b "\xac\xed\x00\x05sr\x00\x17java.util.HashMap\x85\x95\x1d\x9b\x94\xee\x0c\x00\x00\x03I\x00\x05valuexr\x00\x10java.lang.String\x86\xac\xed\x00\x05sr\x00\x11java.lang.String\x12\xe2\xa0\xa4\xf7\x81\x878\x02\x00\x01Ljava/lang/String;L\x00\x04namet\x00\x12Ljava/lang/String;L\x00\x08usernameq\x00~\x00\x01sq\x00~\x00\x0bpasswordq\x00~\x00\x0b" ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值