Tomcat uses Redis to store session details

Source: Internet
Author: User
Tags redis tomcat

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 three packages and put them under the Lib directory under the Tomcat directory.

Modify the Context.xml file under Tomcat's Conf directory.

Insert the following code in the context.

The code is as follows Copy Code

<valve classname= "Com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve"/>
<manager classname= "Com.orangefunction.tomcat.redissessions.RedisSessionManager"
host= "localhost"
Port= "6389"
database= "0"
Maxinactiveinterval= "60"
/>

You can view the configuration of GitHub in a more detailed way.

This configures a simple environment that uses Redis to store sessions, and can take the same configuration for clusters.

To test the servlet:

The code is as follows Copy Code

@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 never expires
Httpsession.setmaxinactiveinterval (1000);
Make httpserssion Invalid
Httpsession.invalidate;
System.out.println (Httpsession.getid);
Response.getWriter.print ("http");
}
}

Start Tomcat, access http://localhost:8080/myhttp can be seen under Redis

The proof environment has been configured successfully 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.