Retrieval of form fields name at Server end


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

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

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 
		// 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
			//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" "">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>

<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">


3) Press “CTRL”+ F11



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s