blogger templates blogger widgets
This is part of a list of blog posts.
To browse the contents go to

Sharing session attributes across applications

See prev for sharing jsessionId across applications.

Using the previous projects AppOne and AppTwo let's try to store a session level attribute.

After storing a value for in request parameter on index.jsp.

Clicking One to open one.jsp.
Session Id: DCBA9501F134F2458DC16B6A336FFE89
Session attr 'a' : hello
Cookie from request: JSESSIONID=DCBA9501F134F2458DC16B6A336FFE89
Clicking Two to open AppTwo/two.jsp.
Session Id: DCBA9501F134F2458DC16B6A336FFE89
Session attr 'a' : null
Cookie from request: JSESSIONID=DCBA9501F134F2458DC16B6A336FFE89

Note that though the sessionID is shared, the attribute information is not transfered across to a different context. This is how J2EE specification requires containers to work. ServletContext is unique to a web application and HTTPSessions belong to individual contexts.

Here are 3 approaches I used to share session attributes.
- Using cookies
- Using shared application context
- Using single-signon

Approach 1 - Using cookies
This is the easiest approached provided you are not storing sensitive data.

Storing value in cookie
if (inputdata != null) {
 session.setAttribute("a", inputdata);
 Cookie cookie = new Cookie("a",inputdata);
 cookie.setPath("/");
 response.addCookie(cookie);
}

Retrieving value from cookie
Cookie cookie = null;
Cookie[] cookies = null;
// Get an array of Cookies associated with this domain
cookies = request.getCookies();
if( cookies != null ){
  out.println("<h2>
Found Cookies Name and Value</h2>
");
  for (int i = 0; i < cookies.length; i++){
  cookie = cookies[i];
  out.print("Name : " + cookie.getName( ) + ",  ");
  out.print("Value: " + cookie.getValue( )+" 
");
  }
}else{
  out.println("<h2>
No cookies founds</h2>
");
}

Below is the output of after using this approach.

one.jsp
Session Id: 53D3074F15F9DADE49174A1648E332D6
Session attr 'a' : hello
Cookie from request: JSESSIONID=53D3074F15F9DADE49174A1648E332D6; a=hello
Found Cookies Name and Value

Name : JSESSIONID, Value: 53D3074F15F9DADE49174A1648E332D6
Name : a, Value: hello

two.jsp
Session Id: 53D3074F15F9DADE49174A1648E332D6
Session attr 'a' : null
Cookie from request: JSESSIONID=53D3074F15F9DADE49174A1648E332D6; a=hello
Found Cookies Name and Value

Name : JSESSIONID, Value: 53D3074F15F9DADE49174A1648E332D6
Name : a, Value: hello


Approach 2 - Using shared application context

To access another application's context, in context.xml specify crossContext attribute value as true

<context  crossContext="true">

If you try to access another context without enabling crossContext, you will be greeted with Null pointer exception.

index.jsp

<form method="post" action="index.jsp">
<input type="text" name="in" /> <input type="submit" value="submit" />
</form>
<%
 String inputdata = null;
 ServletContext context = getServletContext();
 Map map = (Map)context.getAttribute("map");
 if (map == null) {
  map = new HashMap<String, Object>();
 }
 inputdata = request.getParameter("in");
 System.out.println(inputdata);
 if (inputdata != null) {
  Map attr = new HashMap();
  attr.put("a", inputdata);
  map.put(request.getSession().getId(), attr);
  context.setAttribute("map", map);
 }
%>
<a href="one.jsp">One</a>
<a href="/AppTwo/two.jsp">Two</a>


one.jsp

<% 
String cookieFromRequestHeader = request.getHeader("cookie"); 
%>
<% 
 Map map = (Map)getServletContext().getAttribute("map");
    System.out.println(map);
 Map attr = (Map)map.get(request.getSession().getId());
%>
Session Id: <%= request.getSession().getId() %>
Session attr 'a' : <%=attr.get("a") %>
Cookie from request: <%= cookieFromRequestHeader %>

two.jsp

<% 
String cookieFromRequestHeader = request.getHeader("cookie");
%>
<% 
 Map map = (Map)getServletContext().getContext("/AppOne").getAttribute("map");
    System.out.println(map);
 Map attr = (Map)map.get(request.getSession().getId());
%>
Session Id: <%= request.getSession().getId() %>
Session attr 'a' : <%=attr.get("a") %>
Cookie from request: <%= cookieFromRequestHeader %> 


Below is the output of after using this approach.

one.jsp
Session Id: C24ADD6E0DAD48B7D8AEF1133BD82AFD
Session attr 'a' : hello
Cookie from request: JSESSIONID=C24ADD6E0DAD48B7D8AEF1133BD82AFD

two.jsp
Session Id: C24ADD6E0DAD48B7D8AEF1133BD82AFD
Session attr 'a' : hello
Cookie from request: JSESSIONID=C24ADD6E0DAD48B7D8AEF1133BD82AFD
{C24ADD6E0DAD48B7D8AEF1133BD82AFD={a=hello}}


Approach 3 - Using Single SignOn


<valve className="org.apache.catalina.authenticator.SingleSignOn"/>



No comments:

Post a Comment