Tomcat uses Redis to store Session and tomcatredis
Tomcat Redis Session Github address.
Download the commons-pool2-2.2.jar, jedis-2.5.2.jar, tomcat-redis-session-manager-2.0.0.jar these three packages, put it under the tomcat directory lib directory.
Modify the context. xml file in the conf directory of tomcat.
Insert the following code in Context.
<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" /> <Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager" host="localhost" port="6389" database="0" maxInactiveInterval="60" />
For more details, see the github configuration.
In this way, a simple session storage environment using redis is configured, and the same configuration can be used for clusters.
Test Servlet:
@ WebServlet (urlPatterns = "/myhttp") public class MyHttpServlet extends HttpServlet {@ Override protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// get session HttpSession httpSession = request. getSession (); httpSession. setAttribute ("name", "name"); // set to 0 and never expire httpSession. setMaxInactiveInterval (1000); // make httpserssion invalid // httpSession. invalidate (); System. out. println (httpSession. getId (); response. getWriter (). print ("http ");}}
Start tomcat and access http: // localhost: 8080/myhttp. You can see it in redis.
The environment is successfully configured and can be used.