1菜皂、網(wǎng)上查找資料丰榴,大部分從下面網(wǎng)址下載java代碼惶楼,因是幾年前實現(xiàn)的(大概2,3年前吧)整葡,不支持tomcat8
https://github.com/jcoleman/tomcat-redis-session-manager
2、在myeclipse 新建一個maven項目【maven-archetype-quickstart】
源文件新建包名com.orangefunction.tomcat.redissessions
講下載下來的java類拷貝到該包之下(這些java類只實現(xiàn)tomcat7良姆,實現(xiàn)tomcat8需要做一些修改)
JavaSerializer.java
RedisSession.java
RedisSessionHandlerValve.java
RedisSessionManager.java
Serializer.java
SessionSerializationMetadata.java
image
3肠虽、maven設置
====tomcat8 maven pom.xml====
<dependencies>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-catalina</artifactId>
<version>8.0.33</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.7.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin> <!-- 打jar包 -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<!-- 源代碼編譯版本 -->
<source>1.8</source>
<!-- 目標平臺編譯版本 -->
<target>1.8</target>
<!-- 字符集編碼 -->
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
image
將打包出來的tomcat-redis的jar包和
jedis-2.7.2.jar
commons-pool2-2.3.jar
拷貝到tomcat的lib文件夾下面
4、tomcat配置
====tomcat context.xml====
<!--
com.orangefunction.tomcat.redissessions 是自定義maven項目的報名路徑玛追,切需要與maven 中 RedisSessionManager的serializationStrategyClass值一致
-->
<!-- redis session 共享配置 -->
<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
<Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
host="127.0.0.1"
port="6379"
database="0"
maxInactiveInterval="60" />
5税课、需要特別注意的是項目中要存入session的對象必須實現(xiàn)序列化,否知會出現(xiàn)序列化錯誤
public class Test implements Serializable {
private static final long serialVersionUID = 5021582410009851677L;
......
}
tomcat209
<%@ page import="com.neuedu.bean.Person" %><%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2018/7/17
Time:
13:57
To change this template use File | Settings | File Templates.
--%>
<%@ page
contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<%
Person person = new Person();
person.setId(3);
person.setName("zhangsan");
session.setAttribute("person",person);
session.setAttribute("msg","hello");
%>
<%=session.getId()%>
tomcat141
</body>
</html>
tomcat209
<%@ page import="com.neuedu.bean.Person" %><%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2018/7/17
Time:
13:57
To change this template use File | Settings | File Templates.
--%>
<%@ page
contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<%
if(session.getAttribute("person") != null)
{
Person person = (Person)session.getAttribute("person");
out.print(person.getName());
}
else
{
out.print("null");
}
%>
<%=session.getId()%>
tomcat209
</body>
</html>