Tomcat session sharing: Session sharing between different projects with Tomcat

Source: Internet
Author: User

Sometimes we come across a larger project, and to make it easier to implement some of the features, we'll split the project into separate Web projects.
But when we manage these projects, there is only one landing port, and then we take the session in other projects to verify the identity.
View tomcat about HTTP Connector There is a emptysessionpath that explains the following:
If set to True, the all paths for session cookie is set to/. This can is useful for portlet specification implementations. If not specified, this attribute are set to False. A side effect to setting-true, is if Tomcat creates a new session it'll attempt to use the cookie session I D if supplied by the client.
So need a tomcat under two Web sharing data between sessions via session
Because each Web application has a unique ServletContext instance object, all of its following servlets share this ServletContext.
Use the SetAttribute () method in ServletContext to pass the session past and get the session instance in another Web program.
1: Modify TOMCAT---conf----server.xml file

XML code
  1. <Host appbase="WebApps" autodeploy="true" name="localhost" unpackwars="   True " xmlnamespaceaware=" false "x mlvalidation=" false "></Host>
  2. Modified to:
  3. <host appbase= "WebApps"  autodeploy= "True"  name= "localhost"   unpackwars= "true"   Xmlnamespaceaware= "false"  x mlvalidation= "false" >  
  4.    <Context path="/Project A" reloadable="false" crosscontext="true"></ Context>
  5. <Context path="/project B" reloadable="false" crosscontext="true"> </Context>
  6.  </Host>
  

Note the Crosscontext property in the Help document means
Crosscontext:set to True if-want calls within this application-servletcontext.getcontext () to successfully return A request dispatcher for other Web applications running on this virtual host. Set to False (the default) in security conscious environments, to make getcontext () always return null.
Set to True to indicate that you can call another Web application to get ServletContext by Servletcontext.getcontext () and then call its getattribute () to get the object you want.

2: In Project A, write the following code:
We assume that
Item A is/myweb
Item B is/W2
The following is used to test the sharing session between different projects under the same tomcat

Java code
    1. HttpSession session = Req.getsession ();
    2. Session.setattribute ("name", "Xbkaishui");
    3. Session.setmaxinactiveinterval (6565);
    4. ServletContext contexta =req.getsession (). Getservletcontext ();
    5. Contexta.setattribute ("Session", Req.getsession ());
    HttpSession session = Req.getsession ();        Session.setattribute ("name", "Xbkaishui");    Session.setmaxinactiveinterval (6565);    ServletContext contexta =req.getsession (). Getservletcontext ();    Contexta.setattribute ("Session", Req.getsession ());

Test

Java code
    1. Out.println ("insessionrangleservlet name:" +session.getattribute ("name"));
    Out.println ("In Sessionrangleservlet Name:" +session.getattribute ("name"));

3. In Project B, write the following code to remove the session

Java code
    1. httpsession session1 =req .getsession ();     
    2.  servletcontext context = session1.getservletcontext ();     
    3.  //  this pass is the virtual path of project a   
    4.  ServletContext  Context1= context.getcontext ( "/myweb");   
    5.   System.out.println (CONTEXT1);   
    6.  httpsession session2 = (HttpSession) Context1.getattribute (
    7.  system.out.println ( Span class= "string" "base" is passed by the user: "+session2.getattribute (
HttpSession session1 =req. getsession ();     ServletContext Context = Session1.getservletcontext ();     This is passed the virtual path of project a   servletcontext context1= context.getcontext ("/myweb");   System.out.println (CONTEXT1);   HttpSession Session2 = (HttpSession) Context1.getattribute ("session");   System.out.println ("Base pass user is:" +session2.getattribute ("name"));

And then redeploy it.

Tomcat session sharing: Session sharing between different projects with Tomcat

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.