নিচে একটি জেনেরিক উদাহরণ দেওয়া হলো যা getParameterNames() মেথড ব্যবহার করে HttpServletRequest-এ서 উপলব্ধ সকল ফর্ম প্যারামিটার পড়ে। এই মেথডটি একটি Enumeration রিটার্ন করে যেটিতে প্যারামিটার নামগুলো অনির্দিষ্ট ক্রমে থাকে।
এনামারেশন পাওয়ার পর, আমরা স্ট্যান্ডার্ড পদ্ধতিতে এনামারেশন-এ লুপ চালাতে পারি, hasMoreElements() মেথড ব্যবহার করে কবে থামবে তা নির্ধারণ করতে এবং nextElement() মেথড ব্যবহার করে প্রতিটি প্যারামিটার নাম পাওয়া যায়।
উদাহরণ: main.jsp
<%@ page import = "java.io.*,java.util.*" %>
<html>
<head>
<title>HTTP Header Request Example</title>
</head>
<body>
<center>
<h2>HTTP Header Request Example</h2>
<table width = "100%" border = "1" align = "center">
<tr bgcolor = "#949494">
<th>Param Name</th>
<th>Param Value(s)</th>
</tr>
<%
Enumeration paramNames = request.getParameterNames();
while(paramNames.hasMoreElements()) {
String paramName = (String)paramNames.nextElement();
out.print("<tr><td>" + paramName + "</td>\n");
String paramValue = request.getParameter(paramName);
out.println("<td> " + paramValue + "</td></tr>\n");
}
%>
</table>
</center>
</body>
</html>
নোট: মূল কোডে request.getHeader(paramName) ব্যবহার করা হয়েছে, যা ভুল। সঠিকভাবে request.getParameter(paramName) ব্যবহার করা উচিত।
Hello.htm
<html>
<body>
<form action = "main.jsp" method = "POST" target = "_blank">
<input type = "checkbox" name = "maths" checked = "checked" /> Maths
<input type = "checkbox" name = "physics" /> Physics
<input type = "checkbox" name = "chemistry" checked = "checked" /> Chem
<input type = "submit" value = "Select Subject" />
</form>
</body>
</html>
আউটপুট উদাহরণ
Reading All Form Parameters
| Param Name | Param Value(s) |
|---|---|
| maths | on |
| chemistry | on |