【tomcat8】Tomcat 使用Redis存储Session的详解

时间:2020-11-04  来源:redis  阅读:

Tomcat Redis Session Github 地址。

下载 commons-pool2-2.2.jar,jedis-2.5.2.jar,tomcat-redis-session-manager-2.0.0.jar 这三个包,将其放到 tomcat 目录下的lib目录下。

修改tomcat 的conf目录下的 context.xml 文件。

在Context中插入下面的代码。

 代码如下

redissessions.RedisSessionHandlerValve" />
         host="localhost"
     port="6389"
     database="0"
     maxInactiveInterval="60"
     />

更详细的方式可以查看github的配置。

这样就配置好了一件简单的使用redis存储session的环境,对于集群可以采取相同的配置。

测试Servlet:

 代码如下

@WebServlet(urlPatterns = "/myhttp")
public class MyHttpServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //获取session
        HttpSession httpSession = request.getSession;
        httpSession.setAttribute("name","name");
        //设置为0 永不过期
        httpSession.setMaxInactiveInterval(1000);
        //使httpserssion无效
//        httpSession.invalidate;
        System.out.println(httpSession.getId);
        response.getWriter.print("http");
    }
}

启动tomcat,访问 http://localhost:8080/myhttp则可以在redis下看到

证明环境已经配置成功,可以使用了。

【tomcat8】Tomcat 使用Redis存储Session的详解

http://m.bbyears.com/shujuku/108704.html

推荐访问:tomcat下载 tomcat环境变量配置 tomcat7 tomcat安装 tomcat启动一闪而过 tomcat启动 tomcat部署web项目
相关阅读 猜你喜欢
本类排行 本类最新