SpringMVC处理cookie和session

Cookie

将cookie返回到浏览器,cookie在http请求头和响应头里面。

@RequestMapping(value = "checkCookie")
    public String checkCookie(String username, String password, HttpServletResponse response){
        // 新建Cookie
        Cookie username_cookie = new Cookie("username", username);
        Cookie password_cookie = new Cookie("password", password);
        // 输出到客户端
        response.addCookie(username_cookie);
        response.addCookie(password_cookie);
        return "redirect:getCookie";
    }

获得cookie。

  @RequestMapping(value = "getCookie")
    public String getCookie(@CookieValue("username") String username, @CookieValue("password") String password){
        // 控制台输出
        System.out.println("username: " + username);
        System.out.println("password: " + password);
        return "success";
    }

 

Session

第一种方式
 


public void getSessionAction(HttpServletRequest request){
 HttpSession session = request.getSession();
}

第二种方式
 

public void getSessionAction(HttpSession session){
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值