Tomcat uses Redis to store Session and tomcatredis

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.