a122178
asked on
Question about tomcat shared session.
Hi all,
I want to have share sessions between two tomcat web application. I have read some information about the Tomcat cluster. However, the session will only be replicated when one of the servers are down. I want to have the sessions to be replicated anytime when there is a request. Also, the setup of the cluster is heavy. Since that all the objects have to be serialized, I could not find some of them. I want to see if anyone have any other suggestion for me to do this session sharing.
Thanks,
I want to have share sessions between two tomcat web application. I have read some information about the Tomcat cluster. However, the session will only be replicated when one of the servers are down. I want to have the sessions to be replicated anytime when there is a request. Also, the setup of the cluster is heavy. Since that all the objects have to be serialized, I could not find some of them. I want to see if anyone have any other suggestion for me to do this session sharing.
Thanks,
I don't know anyway of sharing sessions. But what did you end up doing with your context elements in your last question ? The reason I ask is because the context element has the crossContext attribute. If you set crossContext = "true" , then you might be able to use ServletContext.getContext( java.lang. String uripath) to share info between the two contexts. I haven't tried this but maybe fargo or some other expert has tried it and will help you. rrz
a122178, can I assume that you have this article http://www.onjava.com/pub/a/onjava/2004/11/24/replication1.html
Does it help?
Does it help?
ASKER
For the last question, I figure it out how to do.
what I need is just to create an [myapp].xml file inside the folder, tomcat/conf/[EngineName]/[ host]/
<context docBase="/myapp2">
</context>
Then when user put the link URL: http://localhost/myapp will be treated as http://localhost/myapp2
Hope it helps.
what I need is just to create an [myapp].xml file inside the folder, tomcat/conf/[EngineName]/[
<context docBase="/myapp2">
</context>
Then when user put the link URL: http://localhost/myapp will be treated as http://localhost/myapp2
Hope it helps.
>I want to have share sessions between two tomcat web application.
Please give us more details. What exactly are you trying to accomplish. rrz
Please give us more details. What exactly are you trying to accomplish. rrz
ASKER
If one of the sessions is updated, the other one will update immediately. I want to have the user information synchronized together.
Hopefully bloodredsun will comeback here to help you. He has more knowledge then I do.
>I want to have the user information synchronized together.
I have an idea how this could be accomplished. I did do a limited test. If you want and we have time, then we could do more work on this.
I made two contexts and created two xml files that looked like this
<Context docBase="${catalina.home}/ webapps/te st"
crossContext="true">
</Context>
and put those two files in myTomcatHome\conf\Catalina \localhost
Then I made two listeners that looked like this
package test;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ShareListener implements HttpSessionAttributeListen er {
public void attributeAdded(HttpSession BindingEve nt event){}
public void attributeRemoved(HttpSessi onBindingE vent event){}
public void attributeReplaced(HttpSess ionBinding Event event){
String currentAttributeName = event.getName();
System.out.println(current AttributeN ame + " was replaced in test app"); // in other context change to its name
HttpSession session = event.getSession();
String userName = (String)session.getAttribu te("userNa me");
String newValue = (String)session.getAttribu te(current AttributeN ame);
Hashtable table = (Hashtable)session.getServ letContext ().getAttr ibute("ses sionShare" );
table.put(userName,newValu e);
}
}
and registered each of them in their respective context's web.xml file with something like this.
<listener>
<listener-class>test.Share Listener</ listener-c lass>
</listener>
Then i made four JSPs and placed two in each context
first JSP
<%@ page import="java.util.*"%>
<%
application.setAttribute(" sessionSha re", new Hashtable());
session.setAttribute("shar e","I am from test app"); // in other context change to its name
session.setAttribute("user Name","rrz ");
String userName = (String)session.getAttribu te("userNa me");
String shareString = (String)session.getAttribu te("share" );
Hashtable table = (Hashtable)application.get Attribute( "sessionSh are");
table.put(userName,shareSt ring);
%>
<%=(String)table.get(userN ame)%>
<%
ServletContext testContext = application.getContext("/t est2"); // in the other context change to point this context
Hashtable testTable = (Hashtable)testContext.get Attribute( "sessionSh are");
%>
<%=(String)testTable.get(u serName)%>
second JSP
<%
session.setAttribute("shar e","I am from test app and I am sharing!"); // in other context change its name
%>
I tested all this by first accessing the first JSP in each context then accessing the second in each context. and finally going back to the first JSP. Please give it a try and let us know what you think. rrz
>I want to have the user information synchronized together.
I have an idea how this could be accomplished. I did do a limited test. If you want and we have time, then we could do more work on this.
I made two contexts and created two xml files that looked like this
<Context docBase="${catalina.home}/
crossContext="true">
</Context>
and put those two files in myTomcatHome\conf\Catalina
Then I made two listeners that looked like this
package test;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ShareListener implements HttpSessionAttributeListen
public void attributeAdded(HttpSession
public void attributeRemoved(HttpSessi
public void attributeReplaced(HttpSess
String currentAttributeName = event.getName();
System.out.println(current
HttpSession session = event.getSession();
String userName = (String)session.getAttribu
String newValue = (String)session.getAttribu
Hashtable table = (Hashtable)session.getServ
table.put(userName,newValu
}
}
and registered each of them in their respective context's web.xml file with something like this.
<listener>
<listener-class>test.Share
</listener>
Then i made four JSPs and placed two in each context
first JSP
<%@ page import="java.util.*"%>
<%
application.setAttribute("
session.setAttribute("shar
session.setAttribute("user
String userName = (String)session.getAttribu
String shareString = (String)session.getAttribu
Hashtable table = (Hashtable)application.get
table.put(userName,shareSt
%>
<%=(String)table.get(userN
<%
ServletContext testContext = application.getContext("/t
Hashtable testTable = (Hashtable)testContext.get
%>
<%=(String)testTable.get(u
second JSP
<%
session.setAttribute("shar
%>
I tested all this by first accessing the first JSP in each context then accessing the second in each context. and finally going back to the first JSP. Please give it a try and let us know what you think. rrz
What I posted is "proof of concept" code. Certainly we need to do a lot more work on it. One thing that I can think of is using a servlet in each context that loads on startup to create the Hashtable in each context. That is all the time I got now. rrz
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks.. but please give me some times to digest.
ASKER
You have mentioned master / slave webapp.
-- Are they in the same machine?
-- Are they in the different Tomcat servers?
Does the code work on different Tomcat servers?
-- Are they in the same machine?
-- Are they in the different Tomcat servers?
Does the code work on different Tomcat servers?
>-- Are they in the same machine?
Yes
>Does the code work on different Tomcat servers?
No
I assumed that the two contexts were in the same the Tomcat. What is your situation ?
Yes
>Does the code work on different Tomcat servers?
No
I assumed that the two contexts were in the same the Tomcat. What is your situation ?
ASKER
The situation is that they are in different machine and different Tomcat without cluster.
Thanks for your solution. At least I know how to share session in the same machine.
Thanks for your solution. At least I know how to share session in the same machine.
>The situation is that they are in different machine
I guess you might be able to use RMI. I have never used it. But with a quick google, I found
http://java.sun.com/docs/books/tutorial/rmi/index.html
I guess you might be able to use RMI. I have never used it. But with a quick google, I found
http://java.sun.com/docs/books/tutorial/rmi/index.html
ASKER
Thanks. I will try to implement the API by myself