How to pass request parameters to frame pages?

I have a servlet (or JSP) that sets a request attribute and then forwards to a JSP page. This JSP page contains a frame set, where each frame contains another JSP page. When I try to access the request attribute (as a request scope bean) in the JSP pages that represent the frames, it is not available anymore.

I know I could use session scope beans instead, but since the attribute values are only needed while setting up the frame pages, I want to avoid that. How can it be done?

Answer:
The reason you can’t use request attributes to transfer info to the JSPs loaded in separate frames is that request attributes are only available while you process the same request; the frame JSPs are retrieved with separate requests from the one that invokes the servlet.Here the servlet sets the attribute and forwards to the first JSP page. This means this JSP page processes the same request as the servlet, and can access the attribute. But this JSP page just sends back a response that contains the frame set. The browser then makes new requests for the JSP pages that represent each frame. A new request means that the original request attribute is lost.What you can do is let the first JSP generate the frame set so that each frame src attribute include a request parameter with a value created from the request attribute. The parameter is then sent by the browser with the request for each frame JSP page:

<frameset rows="65%,35%">
  <frame src="a.jsp?foo= name="a">
  <frame src="b.jsp?foo= name="b">
  <noframes>
    <body>
      Viewing this page requires a browser capable of displaying frames.
    </body>
  </noframes>
</frameset>

Note that this only works for request attributes of type String (or attributes that can be converted to a meaningful String).

The frame JSP pages can access the parameter value using a scriptlet, useBean/setProperty or a custom action:

<% String foo = request.getParameter("foo"); %>

<jsp:useBean id="pars" class="com.mycomp.MyBean" />
<jsp:setProperty name="pars" property="*" />

<mylib:doSomething param="foo" />

That is, in the same way as a JSP page gets access to any parameter value.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>