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
- <Host appbase="WebApps" autodeploy="true" name="localhost" unpackwars=" True " xmlnamespaceaware=" false "x mlvalidation=" false "></Host>
- Modified to:
- <host appbase= "WebApps" autodeploy= "True" name= "localhost" unpackwars= "true" Xmlnamespaceaware= "false" x mlvalidation= "false" >
- <Context path="/Project A" reloadable="false" crosscontext="true"></ Context>
- <Context path="/project B" reloadable="false" crosscontext="true"> </Context>
- </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
- HttpSession session = Req.getsession ();
- Session.setattribute ("name", "Xbkaishui");
- Session.setmaxinactiveinterval (6565);
- ServletContext contexta =req.getsession (). Getservletcontext ();
- 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
- 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
- httpsession session1 =req .getsession ();
- servletcontext context = session1.getservletcontext ();
- // this pass is the virtual path of project a
- ServletContext Context1= context.getcontext ( "/myweb");
- System.out.println (CONTEXT1);
- httpsession session2 = (HttpSession) Context1.getattribute (
- 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