Given a design goal, create a code snippet using the following standard actions: jsp:include, jsp:forward, and jsp:param.

Action jsp:include.

A jsp:include action provides for the inclusion of static and dynamic resources in the same context as the current page.

Inclusion is into the current value of out. The resource is specified using a relativeURLspec that is interpreted in the context of the web application (i.e. it is mapped).

The page attribute of both the jsp:include and the jsp:forward actions are interpreted relative to the current JSP PAGE, while the file attribute in an include directive is interpreted relative to the current JSP FILE.

An included page cannot change the response status code or set headers. This precludes invoking methods like setCookie. Attempts to invoke these methods will be IGNORED. The constraint is equivalent to the one imposed on the include method of the RequestDispatcher class.

A jsp:include action may have jsp:param subelements that can provide values for some parameters in the request to be used for the inclusion.

Request processing resumes in the calling JSP page, once the inclusion is completed.

The flush attribute controls flushing. If true, then, if the page output is buffered and the flush attribute is given a true value, then the buffer is flushed prior to the inclusion, otherwise the buffer is not flushed. The default value for the flush attribute is false.

Examples:


<jsp:include page="/templates/copyright.html" />

					
The above example is a simple inclusion of an object. The path is interpreted in the context of the Web Application. It is likely a static object, but it could be mapped into, for instance, a servlet via web.xml.

Syntax:


<jsp:include page="urlSpec" flush="true|false" />

					
and

<jsp:include page="urlSpec" flush="true|false">
	{ <jsp:param .... /> }*
</jsp:include>

					
The first syntax just does a request-time inclusion. In the second case, the values in the param subelements are used to augment the request for the purposes of the inclusion.

Table 8.4. jsp:include attributes

AttributeDescription
page The URL is a relative urlSpec. Relative paths are interpreted relative to the current JSP page. Accepts a request-time attribute value (which must evaluate to a String that is a relative URL specification).
flush Optional boolean attribute. If the value is true, the buffer is flushed now. The default value is false.

Action jsp:forward.

A jsp:forward action allows the runtime dispatch of the current request to a static resource, a JSP page or a Java servlet class in the same context as the current page. A jsp:forward effectively terminates the execution of the current page.

The request object will be adjusted according to the value of the page attribute.

A jsp:forward action may have jsp:param subelements that can provide values for some parameters in the request to be used for the forwarding.

If the page output is buffered, the buffer is CLEARED prior to forwarding.

If the page output is buffered and the buffer was flushed, an attempt to forward the request will result in an IllegalStateException.

If the page output was unbuffered and anything has been written to it, an attempt to forward the request will result in an IllegalStateException.

The following action might be used to forward to a static page based on some dynamic condition:


<% String whereTo = "/templates/" + someValue; %>
<jsp:forward page="<%= whereTo %>" />

					

Syntax:


<jsp:forward page="relativeURLspec" />

					
and

<jsp:forward page="urlSpec">
	{ <jsp:param .... /> }*
</jsp:forward>

					

Table 8.5. jsp:forward attributes

AttributeDescription
page The URL is a relative urlSpec. Relative paths are interpreted relative to the current JSP page. Accepts a request-time attribute value (which must evaluate to a String that is a relative URL specification).

Action jsp:param.

The jsp:param element is used to provide key/value information. This element is used in the jsp:include, jsp:forward, and jsp:params elements. A translation error shall occur if the element is used elsewhere.

When doing jsp:include or jsp:forward, the included page or forwarded page will see the original request object, with the original parameters augmented with the new parameters, with NEW VALUES TAKING PRECEDENCE over existing values when applicable. The scope of the new parameters is the jsp:include or jsp:forward call; i.e. in the case of an jsp:include the new parameters (and values) will not apply after the include. This is the same behavior as in the ServletRequest include and forward methods.

For example, if the request has a parameter A=foo and a parameter A=bar is specified for forward, the forwarded request shall have A=bar,foo. Note that the NEW PARAM HAS PRECEDENCE.

Syntax:


<jsp:param name="name" value="value" />

					
This action has two mandatory attributes: name and value. name indicates the name of the parameter, and value, which may be a request-time expression, indicates its value.

Professional hosting     Belorussian informational portal         Free SCBCD 1.3 Study Guide     Free SCDJWS 1.4 Study Guide     SCDJWS 1.4 Quiz     Free IBM Certified Associate Developer Study Guide     Free SCJP 5.0 (Tiger) Study Guide     Free Mock Exam Engine     IBM Test 000-287. Enterprise Application Development with IBM WebSphere Studio, V5.0 Study Guide