最近在看一些关于spring session 的知识,特做一个笔记记录一下。
在项目中经常会遇到这么一种情况,同一个web项目有时需要部署多份,然后使用nginx实现负载均衡,那么遇到的问题就是,部署多份之后,如何实现一个session的共享功能。此时就可以使用spring session来实现。
参考网址:http://docs.spring.io/spring-session/docs/current/reference/html5/guides/httpsession-jdbc-xml.html
1.引入spring session 的jar包依赖。
<?xml version="1.0"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.huan.spring</groupId> <artifactId>spring-session-parent</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>02_spring_session_jdbc</artifactId> <packaging>war</packaging> <name>02_spring_session_jdbc Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> </dependency> <dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session-jdbc</artifactId> <type>pom</type> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> </dependency> <dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc14</artifactId> </dependency> </dependencies> <build> <finalName>02_spring_session_jdbc</finalName> </build> </project>
spring 的版本:4.1.5.RELEASE spring session-jdbc的版本:1.2.0.RELEASE
二、配置spring session的配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd"> <context:annotation-config /> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <!-- 基本属性driverClassName、 url、user、password --> <property name="driverClassName" value="oracle.jdbc.OracleDriver" /> <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl" /> <property name="username" value="scott" /> <property name="password" value="tiger" /> </bean> <bean class="org.springframework.session.jdbc.config.annotation.web.http.JdbcHttpSessionConfiguration" /> <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <constructor-arg ref="dataSource" /> </bean> </beans>
三、配置web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:spring-session.xml </param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <filter> <filter-name>springSessionRepositoryFilter</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> </filter> <filter-mapping> <filter-name>springSessionRepositoryFilter</filter-name> <url-pattern>/*</url-pattern> <dispatcher>REQUEST</dispatcher> <dispatcher>ERROR</dispatcher> </filter-mapping> </web-app>
四、编写测试代码
1.登录界面(login.jsp)
<body> <form action="LoginServlet" method="post"> <table> <tbody> <tr> <td>用户名:</td> <td><input name="loginName" required="required" /></td> </tr> <tr> <td>密码:</td> <td><input type="password" name="password" required="required"></td> </tr> </tbody> <tfoot> <tr> <td colspan="2"><input type="submit" value="登录"></td> </tr> </tfoot> </table> </form> </body>
2.后台的逻辑处理
@WebServlet("/LoginServlet") public class LoginServlet extends HttpServlet { private static final long serialVersionUID = -8455306719565291102L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) resp; request.setCharacterEncoding("UTF-8"); String loginName = request.getParameter("loginName"); String password = request.getParameter("password"); request.getSession().setAttribute("login", true); request.getSession().setAttribute(loginName, password); response.sendRedirect(request.getContextPath() + "/show.jsp"); } }
3.展示界面(show.jsp ---> 没有登录访问次界面,直接跳到登录界面)
<body> 输出session中的值: <% if (request.getSession().getAttribute("login") == null) { response.sendRedirect(request.getContextPath() + "/login.jsp"); } %> <% Enumeration<String> enumeration = request.getSession().getAttributeNames(); while (enumeration.hasMoreElements()) { String key = enumeration.nextElement(); Object value = request.getSession().getAttribute(key); out.println(key + " --- " + value); } %> <br> ----------------- jsessionId:<%=request.getSession().getId() %> ---------------------- <%=request.getRemoteAddr()%> <%=request.getRemotePort()%> <%=request.getRemoteHost()%> <%=request.getRemoteUser()%> <%=request.getHeader("X-Real-IP")%> <%=request.getHeader("Host")%> <%=request.getHeader("X-Forwarded-For")%> <%=request.getLocalPort()%> </body>
4.nginx的简单配置 --- 启动ngnix
5.分别启动2个tomcat容器
6.修改其中一个tomcat容器中的show.jsp的值,随便加一个内容,以示区分。
7.页面上访问
8.可以到oracle数据区中进行查询,用到的表
select * from SPRING_SESSION t; select * from SPRING_SESSION_ATTRIBUTES t;
9.用到的sql语句所在的jar包目录
相关推荐
赠送jar包:spring-session-core-2.0.5.RELEASE.jar; 赠送原API文档:spring-session-core-2.0.5.RELEASE-javadoc.jar; 赠送源代码:spring-session-core-2.0.5.RELEASE-sources.jar; 赠送Maven依赖信息文件:...
spring-boot 集成h2数据库实现session以及数据缓存
springsession-jdbc 示例应用程序展示了如何使用JDBC配置Spring Session。
spring-session的官方文档pdf版本,下载到本地比较方便。描述了如何结合redis、jdbc等多种中间件来实现session共享。其中有代码示例和github项目示例的链接。
spring-session实例。访问地址http://blog.csdn.net/y_y_y_k_k_k_k/article/details/49253931看详细
Spring-JDBC会话使用JDBC将Spring会话存储在数据库中的Spring Boot应用程序
以SpringBoot 为中心,模块化开发系统,用户可以随意删减除权限...复用,组装性强主要应用技术:spring Security+Ehcache+quartz+swagger2+Mysql5.6+springjdbc+druid+spring social+spring session + layerui+vue.js
- Spring 缓存,包括redis、ehcache、spring-cache、memcached、使用redis实现session共享 等。 - spring-docs - Spring 文档生成工具,包括 Swagger - spring-bussiness - Spring 业务应用,包括 AOP、过滤...
springMvc4.3+spring4.3+mybatis3.4+shiro1.4+log4j2+freemarker2.3+shiro-redis2.9
Excel工具类 Word工具类 Java NIO实现socket工具类 分布式session jdk升级到1.7 嵌入式redis服务(只支持linux) 1.0.13 修改默认的beanName生成策略,controller参数扩展 1.0.14 分布式session使用zookeeper 1.0.15 ...
包含基本步骤实现完整的JAVA框架搭建 1 创建web项目,创建dao包,service包,pojo包,controller包,mapper包, 2 导入架包,将架包导入到项目的lib 文件中,如图 3 导入配置文件,将如下配置文件导入到src下面 ...
5.1.8.1Spring的事务管理器 117 5.1.8.2Spring事务的传播属性 117 5.1.8.3Spring事务的隔离级别 117 拓展: 118 5.1.8.4以XML配置的 形式 119 拓展: 120 5.1.8.5以注解方式配置 125 拓展: 127 5.1.9使用CGLIB以XML...
该项目已成功集成 actuator(监控)、admin(可视化监控)、...job(分布式定时任务)、swagger(API接口管理测试)、security(基于RBAC的动态权限认证)、SpringSession(Session共享)、Zookeeper(结合AOP实现分布式锁)、Ra
Spring Data JDBC 1.0.5.RELEASE 中文文档.epub Spring Data JPA 2.1.5.RELEASE 中文文档.epub Spring Data Redis 2.1.5.RELEASE 中文文档.epub Spring Framework 5 中文文档.epub Spring Security 5.1.2.RELEASE ...
9.5.1. 理解Spring的声明式事务管理实现 9.5.2. 第一个例子 9.5.3. 回滚 9.5.4. 为不同的bean配置不同的事务语义 9.5.5. <tx:advice/> 有关的设置 9.5.6. 使用 @Transactional 9.5.6.1. @Transactional 有关的设置 ...
--------------=======Spring 使用技巧========------------------- 1.在SSH中的DAO中执行SQL //申请变量 Connection conn = null; PreparementStatement ps = null; ResultSet rs = null; //通过this.getSession()...
9.5.1. 理解Spring的声明式事务管理实现 9.5.2. 第一个例子 9.5.3. 回滚 9.5.4. 为不同的bean配置不同的事务语义 9.5.5. <tx:advice/> 有关的设置 9.5.6. 使用 @Transactional 9.5.7. 插入事务操作 9.5.8. ...
- Spring Boot 缓存,包括redis、ehcache、spring-cache、memcached、使用redis实现session共享 等。 - springboot-templates - Spring Boot 模板,包括thymeleaf、freemarker、jsp、表单校验 等。 - ...
9.5.1. 理解Spring的声明式事务管理实现 9.5.2. 第一个例子 9.5.3. 回滚 9.5.4. 为不同的bean配置不同的事务语义 9.5.5. <tx:advice/> 有关的设置 9.5.6. 使用 @Transactional 9.5.7. 事务传播 9.5.8. 通知...
9.5.1. 理解Spring的声明式事务管理实现 9.5.2. 第一个例子 9.5.3. 回滚 9.5.4. 为不同的bean配置不同的事务语义 9.5.5. <tx:advice/> 有关的设置 9.5.6. 使用 @Transactional 9.5.7. 事务传播 9.5.8. 通知...