JSP Enumeration

Ads
 

JSP Enumeration

Enumeration, a concept of core java is an object that implements the Enumeration interface generate a series of elements, one at a time.

Enumeration, a concept of core java is an object that implements the Enumeration interface generate a series of elements, one at a time.

JSP Enumeration

        

Enumeration, a concept of core java is an object that implements the Enumeration interface generate a series of elements, one at a time. Each call to the nextElement method ( ) will return the successive elements of the series.

Understand with Example

In this section we are going to display the days of week using the Enumeration in jsp. An Enumeration object generates a series of elements, one at a time which is used for passing through a collection. You can see in the given example that we have used the method add() of class Vector in order to append the days name to the end of this Vector.

v.elements()-The method returns an enumeration of the elements of the vector.

e.hasMoreElements() - This method returns true, until the last object has been returned by nextElement().

e.nextElement()- This method returns the next object in the collection.

Here is the code of enumerations.jsp

<%@ page import="java.util.*"%>
<html>
<body>
<% 
Enumeration e;
Vector v = new Vector();
v.add("Sunday");
v.add("Monday");
v.add("Tuesday");
v.add("Wednesday");
v.add("Thursday");
v.add("Friday");
v.add("Saturday");
out.println("<b>Days of Week:</b>"); 
e = v.elements();
while (e.hasMoreElements())
out.println(e.nextElement()); 
%>
</body>
</html>

Output will be displayed as:

Download Source Code:

 

Ads

Ads