• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

scope of jsp:param

 
Jan Sterk
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider the following code:

main JSP:

<jsp: include page="/jsp/includeTest.jsp">
<jsp: param name="test" value="ABC" />
</jsp: include>
in main page: ${ param.test }


/jsp/includeTest.jsp:

in included page: ${ param.test }
<jsp: include page="/jsp/includeTest2.jsp"/>


/jsp/includeTest2.jsp:

in second included page: ${ param.test }


This will output
in included page: ABC
in second included page: ABC
in main page:



There's something not clear about this, or even incorrect, in HFSJ. The Book says that the <jsp: param> will create a request parameter, that is visible for all the components in the webapp that is part of the request. But as the above code shows, it is only available for the duration of the include/forward.

Credits go to Charles Lyon, as I got the concept from his Garner Press mock exam. (Code is not copied from it literally.)
[ August 18, 2008: Message edited by: Jan Sterk ]
 
Charles Lyons
Author
Ranch Hand
Posts: 836
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Request parameters introduced by <jsp:param> only survive between the opening <jsp:include> and the closing </jsp:include> in which they are declared. It is supposed to be a temporary override for an included page (and its subsequently included pages) only.

Think of the container as using an HttpServletRequestWrapper and overriding the getParameter methods to explicitly return the new value. The wrapper is then passed as the request argument to the RequestDispatcher used to perform the include. Using the wrapper means it doesn't alter the original request parameter map, just override a few selected name/value pairs for the duration of the include. When the RequestDispatcher include returns, the wrapper is binned, so we're back to where we started. There's a similar example (except it overrides request headers not query parameters) on pages 140-141 of my book. <jsp:param /> is on p.333 where I also state "it is (temporarily) overridden".
[ August 18, 2008: Message edited by: Charles Lyons ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic