Share sessions for different web projects under the same Tomcat

Source: Internet
Author: User

Sometimes we encounter a big project. To facilitate some functions, we split the project into different independent web projects.

However, when we manage these projects, there is only one login port, and then the session is used in other projects to verify the identity.

 

 

1. Configure the conf/server. xml file in Tomcat. The configuration in

<Host name = "localhost" appbase = "webapps"
Unpackwars = "true" autodeploy = "true"
Xmlvalidation = "false" xmlnamespaceaware = "false">

 

<! -- AA and BB are two different web projects -->

<Context Path = "/AA" reloadable = "true" crosscontext = "true"/>
<Context Path = "/BB" reloadable = "true" crosscontext = "true"/>

</Host>

 

2. Store the session in the AA Project

Session. setattribute ("user", new date (). tostring () + "weiqingli ");
Servletcontext contexta = session. getservletcontext ();
Contexta. setattribute ("session", session );

 

 

3. Retrieve the session in the BB Project

Httpsession session1 = request. getsession ();
Servletcontext context = session1.getservletcontext ();
Servletcontext context1 = context. getcontext ("/AA ");
If (context1! = NULL &&! Context1.equals ("")){
Httpsession sess = (httpsession) context1.getattribute ("session ");
Out. println (sess. getattribute ("user "));
}

 

 

 

It may be easy to see that this is a one-way transfer session. In fact, to achieve two-way, the above program can be reversed and saved from BB, and AA is taken out.

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.