Spring/JSF Support For Multiple Browser Tabs

by on October 22, 2012 10:00 am

PREFACE

In January, I wrote a blog post about the need to partition HttpSession across multiple browser tabs or windows. In that blog, the client was using JSF 2.0, but not Spring. Now, nine months later, I find myself at a different client with the same problem, but with a different technology stack. They are using Spring 3.0.5 but are back a level on JSF using 1.0. This update represents the pitch I made to this client for the same challenge of trying to support “Open in new Tab” built into most browsers today.

Web applications will often need to store information across requests. For example, saving search results so that once the user returns from viewing details about one result in the search then they can select another. UI beans that need this behavior are often annotated as Session scope. This means that information needed across HTTP requests are stored in the user’s HTTP Session. This session object is maintained on the server and normally has an expiration of 30 minutes. Normally, each time a user opens a new browser instance, they get a new session. However, most browsers use the same session when a user just opens another tab in the same web browser.

The problem with reusing session across two browser tabs can be illustrated with a simple example. Suppose a user opens a browser, enters the URL for an application and then specifies three pieces of information for search criteria and gets back 24 rows of results. The user then selects one of the rows to see the details of that item. The same user opens another tab, navigates to the same application and does a new search this time with different criteria that brings back 3 results. What happens when the user goes back to the first tab and returns to the search results? If the application uses HTTP session to store the search results, then the search results from the first tab have been overridden by the second tab’s search. Rather than seeing the original search’s 24 results in the first tab, the user will see the 3 results from the second tab’s search.

This blog post describes a common approach to both preventing multi-tab usage, as well as how to support it.

DETECTING MULTI-TAB USAGE

The first step in handling a problem is detecting when the situation that causes the problem occurs. Application servers are designed to know when to create a new HTTP session and to pass a unique ID back to the web browser so that subsequent requests from that instance of that browser can be re-associated to that session. However, normal browser window processing does not give the server application any indication that there are two separate browser windows trying to use the same session. As far as the application server is concerned, the requests from two different tabs within the same browser will appear as the same user, just making multiple requests from the same window.

A common approach to overcoming this limitation is for the application to create an ID (let’s call it a window ID) and pass it to the browser. The trick is how to get the browser to return that ID on all subsequent requests from that tab but not on requests from new tabs. The answer to this problem is in understanding that a subsequent related request should only happen from URLs that rendered on the initial response. In other words, the only way to make a subsequent related request to this response is for this response to give you a button or a link for you to press. All buttons and links (as well as AJAX calls) in HTML have to specify a URL to which the user is directed when the action occurs. With this in mind, the answer is simply to provide the window ID as a URL query string parameter on every URL which will be seen by the application as an HTTP request parameter.

For example, the search button in myApp may have a URL on the buttons form that was “/myApp/search.jsf”. Rendering that URL as “/myapp/search.jsf?windowID=1” will give the server application a request parameter of “windowID” that is equal to “1”. Seeing this window ID on a subsequent request lets the application know that this request is associated with a window from which a request to this application was made earlier. If the user opens a new tab and navigates to the application without specifying a window ID, as is normal, then the application knows a brand new request is coming in.

At this point the application can easily distinguish requests from different windows and, if this scenario is not supported, it can simply return an error page with a message stating that multiple windows is not supported. Of course this also assumes that that the there is an initial request, such as login, that initiates the window ID and that subsequent requests to login can be recognized as such so that requests sent without a window ID can be returned as errors.

SUPPORTING MULTI-TAB USAGE

Once your application can distinguish between results from different tabs, the next problem is how to store information in HTTP session without corrupting information from one window with information from another window with information. The answer is that each window needs its own partition of session space.

To attack that problem, you first need to understand that for the purposes of this topic, HTTP session is really just a java.util.Map. It allows the application to store a piece of information using a unique key. For instance, a singleton UI -bean such as a user search bean is typically stored in session with a key equal to its bean name, e.g. userSearchBean. This is the crux of our problem. If the UserSearchBean is session scope, then both windows will access the same bean as they are both sharing session. The fix to our problem is to have a separate map per window id. These per-window maps are then kept in session. Each request for an item out of session, assuming the application knows the “current” window ID, is then just two mapping look-ups instead of one.

IMPLEMENTATION IN SPRING 3.0.5

So far, this discussion has been theoretical with no discussion of how to add a query parameter to every URL or how to intercept requests for session. A quick search of the internet will show that there are two semi-common implementations of this.

The first is in an open source framework called Orchestra. Its documentation of this issue and the solution is superior to all others. See this link for instance. However, as is normally the case with frameworks, this is not its main problem space so incorporating Orchestra brings you a lot more functionality and overhead.

Since the Spring Framework also has a solution for this, if you are already using Spring, this is the preferred approach. However, if your application is limited in the version of frameworks supported and you are still on Spring 3.0.5, then you will be unable to use the built-in ConversationScope that is available in version 3.1. Therefore “reuse” of future framework code is required. The following design is not fool-proof but will suffice until Spring 3.1 can replace it.

MANAGING WINDOWS

The first design requirement is that every request must have a window ID. There are two scenarios to cover; either this must be the first request and a new window ID is needed or this must be a subsequent request from a previously established window. Which scenario we process is determined once for each request by looking for a prescribed request parameter that is holding the window ID. There are three distinct responsibilities here that must be implemented:

  1. Detecting a request parameter for each request can be handled with a Servlet Filter. For Spring 3.0.5, a subclass of the GenericFilterBean, WindowIdFilter, can be used to easily check the request parameters for the existence of the window ID.
  2. Establishing a new, unique ID for a new window can be handled using an java.util.concurrent.atomic.AtomicLong that serves up the next long available when explicitly requested. Encapsulating this value is the first responsibility of a new WindowIdManager class.
  3. Holding onto the window ID for this request and making it available without having to pass it through every filter and context can be handled by using the WindowIdManager class that has a static TheadLocal field for holding onto the ID for this request’s window.

The WindowIdFilter and WindowIdManager classes work together to gather and keep the window ID for the request. Putting the ID on subsequent requests is handled via URL ENCODING.

URL ENCODING

The real magic of this solution is how to ensure that each subsequent related request passes in the appropriate window ID. To understand how this is possible requires you to understand that every HTML renderer should always encode any URL that is returned on a response. The task of URL encoding is needed to prevent any text in the URL from confusing the browser when rendering the HTML page. The conventional way to handle URL encoding is for the renderer to delegate the task to the HTTP Response object passed into the servlet and/or filter to ensure a consistent encoding happen regardless of which renderer is rendering the response.

This design depends on this approach by providing a response wrapper that intercepts all encoding requests. The wrapper class, WindowIdResponseWrapper, checks each URL passed in to determine if a query string is already in effect. If so, an ampersand is appended to the URL; otherwise a question mark is appended. In both cases, the window ID parm name is appended along with an equals sign and the actual window ID value.

To activate this wrapper, the aforementioned WindowIdFilter constructs the wrapper using the actual response object as an argument and then supplies that to downstream filters. This is a common scheme used to override servlet behavior. The final piece of the puzzle is to ensure that any information that would have been stored in Session Scope is instead related to a window.

WINDOW SCOPE

The scope of a bean in Spring is determined by the @Scope annotation. Normal scopes are Request, Session, or Prototype. Spring gives the ability to provide a custom scope also. The class used to implement the custom scope is called each time Spring needs to resolve a bean that is annotated with that scope. For our situation, we want to take most of the beans that were annotated as Session scope and change them to Window scope in order to have them behave correctly when using multiple tabs. You must also introduce the new scope in the Spring configuration as follows:


<bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
  <property name="scopes">
    <map>
      <entry key="window">
	<bean class="org.myorg.WindowIdScope" />
      </entry>
    </map>
  </property>
</bean>

The WindowIdScope class is called each time a bean with Window scope is referenced to resolve the one instance of that bean. This scope class depends on the WindowIdManager class to return the window id from its value stored in a ThreadLocal. That value is used to determine which HashMap in Session should contain the actual instance of the bean.

TECHNICAL SUMMARY

The whole process fits together as follows:

The user opens a browser and hits the initial URL for the application.

The WindowIdFilter first checks the request parameters for a window ID; if one is not found, the WindowIdManager, is asked to create one. If one is found it is set on the WindowIdManager and stored in its ThreadLocal.

The WindowIdFilter then wrappers the HTTP Response and passes it into the downstream servlet filters.

The normal application functionality is invoked changing the state of beans. Each reference to a bean with window scope is resolved by calling the WindowIdScope to look for the map in session corresponding to the appropriate window ID and getting the bean from there.

Once rendering is begun on the response, every URL is encoded using the wrappered Response object which adds the window ID as a query string.

LIMITATIONS

While this approach is used in various places, it is not without its flaws. Three of these limitations are listed below:

  1. Any link containing the window id that is opened in a new window will fool the application into thinking that requests from the new window are still part of the original window which will cause you to be right back to where you started with session info being shared across windows. This is the most serious of the flaws. One approach for overcoming this is to use buttons styled as links so that the browser won’t offer “open in new tab/window” on its context menu.
  2. An unavoidable nuisance is that every URL that displays in the URL Address field of the browser or that is shown in the window footer when you hover over it will show the window ID value.
  3. If a user bookmarks a page, the window ID will be included in the bookmark.

CONCLUSIONS

This design provides an inexpensive way to achieve a partitioned session approach prior to the official, full-blown approach found in Spring 3.1.0. There is a relatively small amount of code needed to implement this design, but also all pages of the application are impacted so any side effects from this design could appear anywhere in your application.

Regardless, the avoidance of any solution would yield, at best, unpredictable results. In this case, the risks are outweighed by the pervasiveness of the solution and the maintainability of the design. Also, since the ability to use Spring 3.1 is fairly close, this solution does not need to live for long.

— Keith Shakib, asktheteam@keyholesoftware.com

  • Share:

One Response to “Spring/JSF Support For Multiple Browser Tabs”

  1. Willy says:

    Good article. I am dealing with some of these issues
    as well..

Leave a Reply

Things Twitter is Talking About
  • How do we harness the power of callbacks without the confusing mess of nested functions in #JavaScript? Promises - http://t.co/obK811q48q
    October 21, 2014 at 2:18 PM
  • Pssst... Our free monthly newsletter comes out tomorrow with dev tips/articles via email. Not on the list? Sign up: http://t.co/F8h0NSzleZ
    October 21, 2014 at 12:05 PM
  • Did you know today is Clean Your Virtual Desktop Day? It really is: https://t.co/TCRpWgTmxg Celebrate by organizing your desktop files.
    October 20, 2014 at 4:50 PM
  • Don't miss the newest post from @bricemciver: Make Me a Promise - http://t.co/obK811q48q #JavaScript
    October 20, 2014 at 10:43 AM
  • RT @DZone: #Docker 1.3 Releases with Security, Signed Images, and Process Injection by @bendzone #devops http://t.co/uytIwFPgO6
    October 17, 2014 at 10:04 AM
  • If you have 15+ years #Java exp, you don't expect to be puzzled debugging a null pointer exception. See an exception: http://t.co/m2iDgNEleK
    October 17, 2014 at 9:51 AM
  • Many on our team attended the #Royals victory last night & @cdesalvo even got a selfie with the Gov. Go #KansasCity! http://t.co/N1Psooe2CE
    October 16, 2014 at 3:39 PM
  • Interesting ExplainLikeI'm5 talk: Why do companies develop iOS first when Android holds 70% of the 'Smart' Market? http://t.co/fxgjIBmqBi
    October 16, 2014 at 12:26 PM
  • We're looking for a top-notch #Java developer to join our team. Learn more about our company culture & the role - http://t.co/0fKsFmN0Ql
    October 16, 2014 at 9:08 AM
  • Want to learn to create custom #Java annotations & process them using the Reflection API? @jhackett01's tutorials - http://t.co/mf1F3eIDY3
    October 15, 2014 at 11:43 AM
  • Happy Ada Lovelace Day! It's a celebration of the achievements of women in STEM - if there's a woman in tech that you admire, tell her today
    October 15, 2014 at 9:13 AM
  • .@fpmoles We absolutely agree - thanks for reading!
    October 15, 2014 at 8:13 AM
  • With 15 yrs exp, @bmongar didn't expect surprise when debugging a null pointer exception. Why it puzzled him - http://t.co/m2iDgNEleK #Java
    October 14, 2014 at 11:20 AM
  • #Royals fans with tickets to tonight's canceled game, here's what you need to know - http://t.co/EErHht3zoN
    October 13, 2014 at 4:23 PM
  • RT @UzilitySoftware: Watch as Wayne explains to the boss, Marvin, what an agile board is about. #scrumalliance #scrum http://t.co/5MzB1bNw…
    October 13, 2014 at 12:01 PM
  • Getting started with #MongoDB? (Flexible #NoSQL for Humongous Data) Here's a free cheat sheet from the folks @Dzone - http://t.co/oBMvICzfcL
    October 13, 2014 at 11:10 AM
  • Brad Mongar's newest post is live on the Keyhole blog - #Java and the Sweet Science http://t.co/m2iDgNEleK
    October 13, 2014 at 8:59 AM
  • RT @housecor: If users have share links to your web app like this: "Go to here. Then click here. Then here." You're doing it wrong. #de
    October 10, 2014 at 2:18 PM
  • CSS is 20 years old today! Happy birthday, #CSS - web design would not be the same without you. http://t.co/8tEMoUjorI
    October 10, 2014 at 9:55 AM
  • Expansion update: remodel, electrical & mudding done; painting in process; carpet to go. We can't wait for our bigger team rooms!
    October 10, 2014 at 8:42 AM
Keyhole Software
8900 State Line Road, Suite 455
Leawood, KS 66206
ph: 877-521-7769
© 2014 Keyhole Software, LLC. All rights reserved.