Session Driven Back Buttons

One of the biggest issues I encountered while refactoring my projects to work without the need of JavaScript, is directing the user back to the page they were previously on. This is especially important for form submission and canceling. I look around the web and mostly saw comments directing programmers to use JavaScript to handle back. However, the point of the exercise is to not require JavaScript for anything.

I then spent some time looking at Server Frameworks (like Spring and Rails) that include form managers, before I came up with a light-weight, session-driven solution (example written in JAVA) that I am now using on my projects. These two methods should be added to your base controller, or servlet equivalent, that has access to HttpServletRequest:

Example 1: Server History Code

private void setupCache(HttpServletRequest request) {
	Boolean noCache = "T".equalsIgnoreCase(request.getParameter(request, "noCache"));
	Integer cacheMaxLength = 3;

	if (! noCache && "get".equalsIgnoreCase(request.getMethod())) {
		List<String> h = (List<String>) request.getSession().getAttribute("HISTORY");
		String url = request.getRequestURI() + "?" + request.getQueryString();

		// ensure history cache exists
		if (null == h) {
			h = new ArrayList<String>();
		// remove cache greater than max size
		else if (cacheMaxLength < h.size()) {

		// prevent double caching on refresh
		if (0 == h.size() || ! h.get(0).equals(url)) {
			request.getSession().setAttribute("HISTORY", h);

protected String getHistory(int i) {
	List<String> h = (List<String>) request.getSession().getAttribute("HISTORY");
	if (null == h || i >= h.size()) {return null;}
	return h.get(h.size() - i - 1);

All you need to do is call "setupCache" at the beginning of all your controllers that you want in the history, and then call "getHistory" with the index of the history item you wish to forward to (1 is the last page) to retrieve that pages URL. If you ever want a page to not cache, then pass it the query parameter "&noCache=T". I also dont cache "POST" requests, because they are form submission. Assuming neither of these conditions are true, we then add the current URL to the List object that holds our history on the session (mapped as HISTORY&rsquot;).

When adding to the cache, we limit the cache size to 3, because you do not want the session object per user to grow indefinitely, and I generally only care about the last page. However, you could easily increase this to 5 or 10 by replacing the constant value of "cacheMaxLength". Lastly, there is no reason to cache the same URL twice, as this usually happens when the user is refreshing the same page, so we ignore duplicate URLs.

Retrieving a URL from the history cache, is as easy as calling "getHistory" with an index; generally, 0 = current page, 1 = last page, 2 = page before last, etc. To do a redirect, you can simply do the following:

Example 2: Response Redirect

String url = getHistory(1);

So to summarize, all your view requests are cache by their URL, but only the current and last 2 pages, and you can prevent a page from being cached by passing the "noCache" parameter equal to "T". In order to retrieve the last page, simply call "getHistory(1)" for the URL.