JSP Arraylist Index


 

JSP Arraylist Index

In this current tutorial you will come to know about ArrayList implementation in JSP. ArrayList is a class and a member of Java Collection Framework.It is the resizable-array and permit all element including the null.

In this current tutorial you will come to know about ArrayList implementation in JSP. ArrayList is a class and a member of Java Collection Framework.It is the resizable-array and permit all element including the null.

Description:

ArrayList is a class and a member of Java Collection Framework. It is the resizable-array and permit all element including the null. It is similar to Vector but it is unsynchronized. Iterator return the element from the list in proper sequence.

In this example there are two file index.jsp and postindex.jsp. The index.jsp file take the user input from three different textbox and value to the postindex.jsp, which take all values and placed into the ArrayList object. Through Iterator all the element of the list is shown in proper sequence.

Code for JSP Arraylist index.jsp & postindex.jsp:

index.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

"http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

</head>

<body>

<form action="postIndex.jsp" method="POST">

<table border="1">

<tbody>

<tr>

<td>First Element</td>

<td><input type="text" name="arraylistvalue1" value=""></td>

</tr>

<tr>

<td>Second Element</td>

<td><input type="text" name="arraylistvalue2" value=""></td>

</tr>

<tr>

<td>Third Element</td>

<td><input type="text" name="arraylistvalue3" value=""></td>

</tr>

<tr>

<td></td>

0

<td><input type="submit" value="submit"></td>

</tr>

</tbody>

1

</table>

</form>

</body>

2

</html>

postindex.jsp

<%@page import="java.util.*" contentType="text/html"

3

pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

"http://www.w3.org/TR/html4/loose.dtd">]

4

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></head>

5

<body>

<%

String str1 = request.getParameter("arraylistvalue1");

6

%>

<%

String str2 = request.getParameter("arraylistvalue2");

7

%>

<%

String str3 = request.getParameter("arraylistvalue3");

8

%>

 

<%

9

ArrayList alist = new ArrayList();

alist.add(str1);

alist.add(str2);

0

alist.add(str3);

%>

<table border="1">

1

<thead>

<tr>

<th>Index</th>

2

<th>Values</th>

</tr>

</thead>

3

<%

Iterator iter = alist.iterator();

%>

4

<%!int x = 0;%>

<%

while (iter.hasNext()) {

5

%>

<tbody>

<tr>

6

<td><%=++x%></td>

<td><%=iter.next()%></td>

</tr>

7

<%

}

%>

8

</table>

</body>

</html>

9

Output:

 

0

 

Download this code

Ads