While working recently on a Spring MVC project I found myself wishing it supported flash scope. I hunted around for a simple solution but couldn’t find anything that didn’t rely on having to import large framework libraries. After a little thought I came up with the following simple and lightweight solution that has worked really well for me. I’ve included the code for this below so feel free to try it out.

What is Flash Scope

Flash scope is a useful part of many web frameworks. It allows the use of the post/redirect/get design pattern to alleviate many of the problems associated with handling multiple submits or resubmission of data in browser requests to the server.

Flash scope is an additional scope to those provided in a standard Java Web application (page, request, session and application). Any attributes held in flash scope will be available for the duration of the current request, and the subsequent request too.

My Implementation

To implement flash scope in my application, I added a servlet filter that checks for request attribute names starting with ‘flash.‘. When it finds such an attribute, it ensures that it is made available in the subsequent request by temporarily storing it in the user’s session, and then reinstating it when the next request is received.

For example, to add a message to flash scope, just use the regular syntax for adding a request scoped attribute and let the fiter take care of it:

1.request.setAttribute("flash.message", "Here is the news...");

The above attribute can then be accessed in the redirect target JSP using EL or scriptlet syntax (omitting the ‘flash.‘ prefix):

1.${message}
2.or
3.<%= request.getAttribute("message") %>

Here’s the code for the filter:

01.import javax.servlet.*;
02.import javax.servlet.http.HttpServletRequest;
03.import javax.servlet.http.HttpSession;
04.import java.io.IOException;
05.import java.util.Enumeration;
06.import java.util.HashMap;
07.import java.util.Map;
08.
09./**
10.* Ensures that any request parameters whose names start
11.* with 'flash.' are available for the next request too.
12.*/
13.public class FlashScopeFilter implements Filter {
14.
15.private static final String FLASH_SESSION_KEY = "FLASH_SESSION_KEY";
16.
17.@SuppressWarnings("unchecked")
18.public void doFilter(ServletRequest request, ServletResponse response,
19.FilterChain chain) throws IOException, ServletException {
20.
21.//reinstate any flash scoped params from the users session
22.//and clear the session
23.if (request instanceof HttpServletRequest) {
24.HttpServletRequest httpRequest = (HttpServletRequest) request;
25.HttpSession session = httpRequest.getSession(false);
26.if (session != null) {
27.Map flashParams = (Map)
28.session.getAttribute(FLASH_SESSION_KEY);
29.if (flashParams != null) {
30.for (Map.Entry flashEntry : flashParams.entrySet()) {
31.request.setAttribute(flashEntry.getKey(), flashEntry.getValue());
32.}
33.session.removeAttribute(FLASH_SESSION_KEY);
34.}
35.}
36.}
37.
38.//process the chain
39.chain.doFilter(request, response);
40.
41.//store any flash scoped params in the user's session for the
42.//next request
43.if (request instanceof HttpServletRequest) {
44.HttpServletRequest httpRequest = (HttpServletRequest) request;
45.Map flashParams = new HashMap();
46.Enumeration e = httpRequest.getAttributeNames();
47.while (e.hasMoreElements()) {
48.String paramName = (String) e.nextElement();
49.if (paramName.startsWith("flash.")) {
50.Object value = request.getAttribute(paramName);
51.paramName = paramName.substring(6, paramName.length());
52.flashParams.put(paramName, value);
53.}
54.}
55.if (flashParams.size() > 0) {
56.HttpSession session = httpRequest.getSession(false);
57.session.setAttribute(FLASH_SESSION_KEY, flashParams);
58.}
59.}
60.}
61.
62.public void init(FilterConfig filterConfig) throws ServletException {
63.//no-op
64.}
65.
66.public void destroy() {
67.//no-op
68.}
69.}

You can see from the listing, that the filter stores the flash scoped parameters in a session scoped map called FLASH_SESSION_KEY. Before the request is processed by the filter chain, the flash attributes are retrieved from the session and added to request scope; after the chain has finished, any new flash scoped parameters are stored in a new session scoped map.

Note that the attributes have the ‘flash.‘ prefix removed when added to the session scoped map – this not only ensures that they can be accessed using their ’short name’, but that they are only kept in the session for one extra request.

This approach is not Spring specific and should work with and Java Web framework. I’ve omitted the configuration for the filter as there are loads of examples of how to do this on the Internet already.