The <jsp:include> tag lets you pass parameters to the include file—a useful capability if your application takes user input.
Here's an example of a <jsp:include> tag that passes two parameters, currency and amount , to a file called order.jsp:
<jsp: include page="order.jsp" flush="true"> <jsp:param name="currency" value="FF"/> <jsp:param name="amount" value="18"/> </jsp:include>
In the include file, order.jsp, you can retrieve the first parameter's value with the following statement:
String exchange = request.getParameter("currency");
The <jsp:include> tag sends all parameters to the include file as strings. This can be a problem if the code in your include file expects other data types, such as integers or floating point numbers. The solution is to convert the string values to the desired data type using available Java methods. For example, the order.jsp page expects the amount value to be an integer. You can use the parseInt method of the Integer class to convert the value to an integer, as follows:
int units = Integer.parseInt(request.getParameter("amount"));
You can pass user input to the include file by making the value attribute of the <jsp:param> tag dynamic. For example, if you get user input from two HTML form fields named money and number, you can pass the values to the include file by writing the following <jsp:include> tag:
<jsp: include page="order.jsp" flush="true"> <jsp:param name="currency" value="<%=request.getParameter("money")%>"/> <jsp:param name="amount" value="<%=request.getParameter("number")%>"/> </jsp:include>
In this example, the server retrieves the form values with the request.getParameter() method and writes them in the <jsp:param> tags. You add slashes to each getParameter argument to escape the quotation marks. Without the slashes, the compiler reads the value attribute of each <jsp:param> tag as two separate strings, one starting with " < " and ending with " ( ", the other starting with " ) " and ending with " > ".
Once the server writes the form parameters into the <jsp:param> tags, it runs the <jsp:include> tag, passing the retrieved values to the include file for further processing.
Admin on Dec 16, 2018 at 12:12 am
If you like dEexams.com and would like to contribute, you can write your article here or mail your article to firstname.lastname@example.org . See your article appearing on the dEexams.com main page and help others to learn.