Java servlet sessions within multiple iframes

1.2k views Asked by At

I have a site, call it www.main.com with four iframes which loads www.iframe.com/foo into each of those iframes. Upon initial load of www.iframe.com/foo i make a call to config.bar() from the servlet (foo) to load some user data into the session. Before i make the call to config.bar() i'm checking to see if the config is loaded into the session already and if so, skip the fetch from config.bar() and load from the session.

My problem is, since the session is not yet established in foo, i am getting 4 unique session id's, thus making 4 calls to config.bar(). When i refresh the www.main.com page, the four iframes load again but are assigned the sessionid of the first iframe to load (from the previous request).

I tried moving the logic to a filter hoping that it would right things but it had no effect. Is it possible to load a servlet multiple times within iframes with a different url and have only one session id at time of initial load?

Here's some snippets to demonstrate what i'm trying to do:

www.iframe.com/foo:

//--FooController.java
@Controller
class FooController
{
    @RequestMapping(value = "/foo", method = RequestMethod.GET)
    public String foo(Model model, HttpServletRequest request)
    {
        Config c = request.getSession().getAttribute("config");
        reqest.setAttribute("bazz", bazz.doMagic(c));
        return "foo";
    }
}

//--ConfigFilter.java
public class ConfigFilter implements Filter
{
    @Autowired
    private ConfigService cs;

    public void init(FilterConfig config) throws ServletException
    {
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws java.io.IOException, ServletException
    {
        HttpServletRequest req = (HttpServletRequest) request;

        HttpSession sess = req.getSession();

        //first request these are different for each iframe
        //second request they all match
        System.out.println("sess="+sess.getId());

        //only fetch config if not in session    
        if(sess.getAttribute("config") == null) {
            sess.setAttribute("config", cs.getCfg());

        // Pass request back down the filter chain
        chain.doFilter(request,response);
    }

    public void destroy()
    {
    }
}

www.main.com:

<html>
<body>
    <iframe src="http://www.iframe.com/foo?page=1"></iframe>
    <iframe src="http://www.iframe.com/foo?page=2"></iframe>
    <iframe src="http://www.iframe.com/foo?page=3"></iframe>
    <iframe src="http://www.iframe.com/foo?page=4"></iframe>
</body>
</html>
0

There are 0 answers