Retrieval of form fields name at Server end

Purpose:

    How to retrieve names of fields, instead of values (from the form) at the Server Side?

Steps:
1) Create “MyServlet.java”, as servlet file (don’t know how to create a servlet file in Eclipse? Refer to our post: “ Servlet in Eclipse IDE“)

import java.io.*;
import java.util.Enumeration;
import javax.servlet.*;
import javax.servlet.http.*;
 
public class MyServlet extends HttpServlet
{
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		response.setContentType("text/html"); 
		// informing the client that which format of data/response will be send
		PrintWriter out= response.getWriter(); 
		
		Enumeration e1= request.getParameterNames();
		// will retrieve all text fields names, from the HTML form using "getParameterNames()" method
		// and store in Enumeration type "e1"
		//Enumeration is an interface, whose objects has capability to store in Array form
		
		out.print("<br> The names are <br>");
		
		while(e1.hasMoreElements()) // returns TRUE/FALSE, if value is present or not
		{
			out.print("<br>"+e1.nextElement()); 
			//returns the next value from "e1"
		}
	}
}

2) Create “index.html”, as HTML file (you can refer: “How to add HTML file in your Servlet Program using Eclipse“)

<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<form action="MyServlet" method="get">
Here are 4 fields, with 4 different names
1<input type="text" name="num"><br> 
2<input type="text" name="num1"><br> 
3<input type="text" name="num2"><br> 
4<input type="text" name="num3"><br> 
<input type="Submit" value="Submit">

</form>
</body>
</html>

3) Press “CTRL”+ F11
4

OUTPUT
1
2