x

JSP Action Tag

PREV 

JSP supports two action tags <jsp:forward> and <jsp:include>.<jsp:forward> action is used to transfer control to a new page. The processing of the current page is discarded.


It takes an attribute called page which is set to the URL of the new page. The usage of <jsp:forward> is shown below –

<jsp:forward page=”./newurl.jsp”/>

Lets say a web site gets users from USA and Canada. The site wants to show different home pages for the users of two countries. In this case the JSP page can use <jsp:forward> to transfer the control to Canada or US-specific home page.

<% java.util.Locale loc = request.getLocale();
String country= loc.getCountry();
if(country.equals(“US”)) {%>
<jsp:forward page=”./usapage.jsp”/>
<%} else { if(country.equals(“CA”)) {%>
<jsp:forward page=”./capage.jsp”/>
<%}
}%>

Forward action supports passing of arguments. The example below illustrates this.

<jsp:forward page=”nextpage.jsp”>
<jsp:param name=”country” value=”usa”/>
</jsp:forward>

The code fragment sets the parameter country to usa, and passes that to nextpage.jsp . nextpage.jsp can then access the country parameter via the implicit request object.

Include action:

The <jsp:include> action is used to include the contents of another page. Control is transferred to the included page and then returned back to the top level page.It includes two attributes: page attribute to specify the URL of included JSP file, and flush attribute to specify the top level page’s contents should be flushed (sent to the client). The flush attribute takes boolean value as input. If flush is true, then the contents of top level page are sent to the client, before control is transferred to the included page. A sample usage of include action is illustrated below –


<jsp:include page=”pagefragment.jsp” flush=”true”/>

Though include directive and include action have similar behavior, there are some subtle differences. These are explained in the table below –

Include Directive Include action
The syntax is <include file=”navbar.jsp”%> The syntax is <jsp:include page=”navbar.jsp” flush=”true”/>
The Java code for included page fragment is merged into the Java code for main top level page The included page gets compiled into a separate Java file. The control gets transferred to the included file.
JSP container may not automatically recompile if the included file is changed. If the included file is changed, JSP file will be compiled automatically.
include directive has access to all variables defined in the top level page. It is possible to specify arguments using <jsp:param>
The generated Java file size is larger, as included file’s code is added to all places where file fragment is included. The generated Java file is smaller, as only one copy of included Java file is generated.
Usage of include directive is generally efficient wrt response time. Usage of include action can lead to a small increase in response time as a new request and response from the server is generated for included file.

PREV 



Like it? Please Spread the word!