?
Solved

Question about tomcat shared session.

Posted on 2006-05-03
14
Medium Priority
?
815 Views
Last Modified: 2010-08-05
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,
0
Comment
Question by:a122178
  • 7
  • 6
14 Comments
 
LVL 28

Expert Comment

by:rrz
ID: 16601293
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
0
 
LVL 29

Expert Comment

by:bloodredsun
ID: 16601829
a122178, can I assume that you have this article http://www.onjava.com/pub/a/onjava/2004/11/24/replication1.html

Does it help?
0
 
LVL 3

Author Comment

by:a122178
ID: 16601847
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.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 28

Expert Comment

by:rrz
ID: 16601949
>I want to have share sessions between two tomcat web application.    
Please give us more details. What exactly are you trying to accomplish.   rrz
0
 
LVL 3

Author Comment

by:a122178
ID: 16602919
If one of the sessions is updated, the other one will update immediately. I want to have the user information synchronized together.
0
 
LVL 28

Expert Comment

by:rrz
ID: 16607630
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/test"
         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 HttpSessionAttributeListener {
 public void attributeAdded(HttpSessionBindingEvent event){}
 public void attributeRemoved(HttpSessionBindingEvent event){}
 public void attributeReplaced(HttpSessionBindingEvent event){
        String currentAttributeName = event.getName();
        System.out.println(currentAttributeName + " was replaced in test app"); // in other context change to its name
        HttpSession session = event.getSession();
        String userName = (String)session.getAttribute("userName");
        String newValue = (String)session.getAttribute(currentAttributeName);
        Hashtable table = (Hashtable)session.getServletContext().getAttribute("sessionShare");
        table.put(userName,newValue);
 }
}

and registered each of them in their respective context's web.xml file  with something like  this.
    <listener>
              <listener-class>test.ShareListener</listener-class>
    </listener>

Then i made four JSPs and placed two in each context
first JSP
<%@ page import="java.util.*"%>
<%
  application.setAttribute("sessionShare", new Hashtable());
  session.setAttribute("share","I am from test app"); //   in other context change to its name
  session.setAttribute("userName","rrz");
  String userName = (String)session.getAttribute("userName");
  String shareString = (String)session.getAttribute("share");
  Hashtable table = (Hashtable)application.getAttribute("sessionShare");
  table.put(userName,shareString);
%>
<%=(String)table.get(userName)%>
<%
  ServletContext testContext = application.getContext("/test2"); //  in the other context change to point this context
  Hashtable testTable = (Hashtable)testContext.getAttribute("sessionShare");
%>
<%=(String)testTable.get(userName)%>

second JSP
<%
  session.setAttribute("share","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


0
 
LVL 28

Expert Comment

by:rrz
ID: 16607886
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
0
 
LVL 28

Accepted Solution

by:
rrz earned 1500 total points
ID: 16610829
Ok, I found some more time. I think it would be easier to *not* put the user's info into a session object. Just have have one shared Hashtable in one of the contexts. That Hashtable could contain  Hashtables for each user's info. I wrote a some test code.
First,  I made two Context element files like above. this time the two contexts called master and slave.  The master web app has a servlet  something like this.  
package master;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ShareServlet extends HttpServlet {
    public void init(ServletConfig config){
                   try{
                       super.init(config);
                       ServletContext context = config.getServletContext();
                       context.setAttribute("sharedTable",new Hashtable());
                       System.out.println("init of ShareServlet");
                      }catch(ServletException se){System.out.println("error in ShareSrevlet init");}
    }
}
and in master's web.xml   I put
    <servlet>      
                   <servlet-name>share</servlet-name>
                   <servlet-class>master.ShareServlet</servlet-class>
                   <load-on-startup/>
    </servlet>

For testing, I have two JSPs in the master web app
-------------------------------------------------------
<%@ page import="java.util.*"%>
hello from master web app.
<%
  session.setAttribute("userName","rrz");
  String userName = (String)session.getAttribute("userName");
  Hashtable sharedTable = (Hashtable)application.getAttribute("sharedTable");
  Hashtable myTable = (Hashtable)sharedTable.get(userName);
  if(myTable == null){
                      myTable = new Hashtable();
                      myTable.put("location","the office");
                      myTable.put("activity","working");
                      sharedTable.put(userName,myTable);
  }
%>
I am at <%=(String)myTable.get("location")%><br/>
I am <%=(String)myTable.get("activity")%>
-------------------------------------------------------------
<%@ page import="java.util.*"%>
hello from master web app.
<%
  String userName = (String)session.getAttribute("userName");
  if(userName == null) userName= "rrz";
  Hashtable sharedTable = (Hashtable)application.getAttribute("sharedTable");
  Hashtable myTable = (Hashtable)sharedTable.get(userName);
%>
I am at <%=(String)myTable.get("location")%><br/>
I am <%=(String)myTable.get("activity")%> But I going home to rest.
<%
  myTable.put("location","home");
  myTable.put("activity","resting");
  sharedTable.put(userName,myTable);
%>
-------------------------------------------------------
in the slave context I put another two JSPs
-------------------------------------------------------
<%@ page import="java.util.*"%>
hello from slave web app.
<%
  session.setAttribute("userName","rrz");
  String userName = (String)session.getAttribute("userName");
  ServletContext masterContext = application.getContext("/master");
  Hashtable sharedTable = (Hashtable)masterContext.getAttribute("sharedTable");
  Hashtable myTable = (Hashtable)sharedTable.get(userName);
  if(myTable == null){
                      myTable = new Hashtable();
                      myTable.put("location","the bar");
                      myTable.put("activity","drinking");
                      sharedTable.put(userName,myTable);
  }
%>
I am at <%=(String)myTable.get("location")%><br/>
I am <%=(String)myTable.get("activity")%>
--------------------------------------------------------
<%@ page import="java.util.*"%>
hello from slave web app.
<%
  String userName = (String)session.getAttribute("userName");
  if(userName == null) userName = "rrz";
  ServletContext masterContext = application.getContext("/master");
  Hashtable sharedTable = (Hashtable)masterContext.getAttribute("sharedTable");
  Hashtable myTable = (Hashtable)sharedTable.get(userName);
%>
I am at <%=(String)myTable.get("location")%><br/>
I am <%=(String)myTable.get("activity")%> But I going to the neighbor's house to visit.
<%
  myTable.put("location","at the neighbor's house");
  myTable.put("activity","visiting");
  sharedTable.put(userName,myTable);
%>
-----------------------------------------------------------
To test, browse to the first JSP in either context first.       rrz
0
 
LVL 3

Author Comment

by:a122178
ID: 16611062
Thanks.. but please give me some times to digest.
0
 
LVL 3

Author Comment

by:a122178
ID: 16611092
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?
0
 
LVL 28

Expert Comment

by:rrz
ID: 16611624
>-- 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 ?
0
 
LVL 3

Author Comment

by:a122178
ID: 16612395
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.
0
 
LVL 28

Expert Comment

by:rrz
ID: 16614548
>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
0
 
LVL 3

Author Comment

by:a122178
ID: 16616159
Thanks. I will try to implement the API by myself
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Strategic internal linking is often considered an SEO power technique, especially for content marketing. Do you need to hire an SEO agency to optimize you internal linking? No, this article will help you understand the basics of internal linking and…
In the below post we have mentioned the best hosting type for startups. Also, check out some of the superlative web hosting companies that are proposing affordable web hosting solutions to host your startup website.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question