Passing Arrays In Jsp Methods

Array is a collection of similar data type. It is one of the simplest data structures. Arrays holds equally sized data elements generally of the similar data type. In an array the index starts from 0.

Passing Arrays In Jsp Methods

Passing Arrays In Jsp Methods

        

Array is a collection of similar data type. It is one of  the simplest data structures. Arrays holds equally sized data elements generally of the similar data type. In an array the index starts from 0. Some arrays can be multidimensional. One and two- dimensional arrays are most commonly used arrays in java. JSP is a technology which enables us to mix html static component with the dynamically generated contents from servlets. In this jsp example we are going to make a program on Arrays. In this example we will mix  the html content i.e. static in nature, when this static content mix with the dynamic contents of jsp by using directive the page will become dynamic. 

In this example of  jsp for passing arrays in Jsp methods, first make a method named Addition(int[] a) of type void which will take an array of type int as its input, it will add the array elements by 2. This method is declared inside the declaration directive. Anything which will declared inside the declaration directive i.e. <%! ----------%> its scope will be applicable in the whole class. Now declare an array of type int inside the scriptlet directive and pass a value in the array.  The logic of the program is written inside the scriptlet directive. This directive is executed each time whenever the request comes for this  jsp page. This tag will be written inside a _jspservice() of HttpJspPage class when the jsp is translated into servlet. Now call the method Addition() inside the scriptlet directive so that the method can be executed and the result will be displayed to the user. To display the output call the implicit object out, which is responsible for displaying the content to the browser. At last save this page as Passing-Arrays-Methods.jsp.

Code of the program is given below:

<HTML>
  <HEAD>
    <TITLE>Passing Arrays In Jsp Methods</TITLE>
  </HEAD>
<BODY>
<table border="1" cellpadding="0" cellspacing="0" width="60%" align="center">
    <tr><td>
	<font  size="6" color ="#000080">Passing Arrays In Jsp Methods</font><br>
    <%!
    void Addition(int [] a)
    {
        for (int i = 0; i < a.length;i++) {
            a[ i ] += 2;
        }
    }
    %>
</td></tr>
<tr><td>
    <%
        int array[] = {2, 4 , 3, 6, 8};
               out.println("Before the call to Addition...<BR>");
        for (int i = 0; i < array.length; i++) {
            out.println("array[" + i + "] = " + array[i] + "<BR>");
        }
        Addition(array);
        out.println("After the call to Addition...<BR>");
        for (int i = 0; i < array.length; i++) {
            out.println("array[" + i + "] = " +
                array[i] + "<BR>");
        }
    %></td>
	</tr>
	</table>
  </BODY>
</HTML>

The output of the program is given below:

Download this example.