2009-08-06

JAVA Passing Parameters to Another JSP Page

Passing Parameters to Another JSP Page
An include action (see e1054 Including a File in a JSP Page) executes
the included JSP page and appends the generated output onto its own
output stream. Request parameters parsed from the URL's query string
are available not only to the main JSP page but to all included JSP
pages as well. It is possible to temporarily override a request
parameter or to temporarily introduce a new request parameter when
calling a JSP page. This is done by using the jsp:param action.

In this example, param1 is specified in the query string and is
automatically made available to the callee JSP page. param2 is also
specified in the query string but is overridden by the caller. Notice
that param2 reverts to its original value after the call. param3 is a
new request parameter created by the caller. Notice that param3 is
only available to the callee and when the callee returns, param3 no
longer exists. Here is the caller JSP page:

<html>
<head></head>
<body>

<jsp:include page="callee.jsp" />
<jsp:param name="param2" value="value2" />
<jsp:param name="param3" value="value3" />
</jsp:include>

Caller:
param1: <%= request.getParameter("param1") %>
param2: <%= request.getParameter("param2") %>
param3: <%= request.getParameter("param3") %>

</body>
</html>

Here is the JSP page being called:

Callee:
param1: <%= request.getParameter("param1") %>
param2: <%= request.getParameter("param2") %>
param3: <%= request.getParameter("param3") %>

If the example is called with the URL:

http://hostname.com?param1=a&param2=b

the output would be:

Callee:
param1: a
param2: value2
param3: value3


Caller:
param1: a
param2: b
param3: null

0 留言: