Newsletter

Example on getParameterNames() method of Servlet Request Object

Servlets » on Jan 19, 2013 { 4 Comments } By Sivateja

So far we have worked on getParameter(), let us see how to use getParameterNames() method. With getParameter() we are able to find parameter values by passing parameter name, just like..

In html:
<input type=”text” name=”n1″>

In Java:
String val=req.getParameter(“n1”);

Parameter name – n1
Parameter value – val

If a client send the data to the servlet, that data will be available in the object of HttpServletRequest interface. In case of getParameter() method we have to pass input parameter name and it will give the value. If you are not aware of input parameter name ? or if you have 50+ input values its really tedious to use getParameter() method.

That’s why this getParameterNames() came into picture 🙂

Directory Structure

Files Required

  • index.html
  • OngetParameterNames.java
  • web.xml

index.html

<font face="verdana" size="2px">
    <form action="onPM" method="post">
        Name:<input type="text" name="name">
        Country:<input type="text" name="country">
        <input type="submit" value="Submit">
     </form>
</font>

OngetParameterNames.java

package java4s;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class OngetParameterNames extends HttpServlet  
{
    protected void doPost(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException
	{
    	PrintWriter pw=res.getWriter();
		res.setContentType("text/html");

		Enumeration en=req.getParameterNames();

		while(en.hasMoreElements())
		{
			Object objOri=en.nextElement();
			String param=(String)objOri;
			String value=req.getParameter(param);
			pw.println("Parameter Name is '"+param+"' and Parameter Value is '"+value+"'");
		}		
			pw.close();	
	}
}

web.xml

<web-app>

   <servlet>
     <servlet-name>onParameterNames</servlet-name>
     <servlet-class>java4s.OngetParameterNames</servlet-class>
   </servlet>

   <servlet-mapping>
     <servlet-name>onParameterNames</servlet-name>
     <url-pattern>/onPM</url-pattern>
   </servlet-mapping>

   <welcome-file-list>
     <welcome-file>index.html</welcome-file>
  </welcome-file-list>

</web-app>

Output

 

​ ​​

You Might Also Like

  ::. About the Author .::

Java4s_Author
Sivateja Kandula - Java/J2EE Full Stack Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.

Comments

4 Responses to “Example on getParameterNames() method of Servlet Request Object”
  1. Debendra says:

    what is the use of tag in web.xml ?

  2. Rajveer says:

    sir how can we print entire page throgh servlet

  3. dev says:

    Dear sir,

    I want to develop one project related to digital signature verification ,I do’t know how it works
    please sir help me I want to solve this problem early as possible.

    Thanks&Regards
    Dev Mishra

  4. Nilesh Pawar says:

    Its so simple to understand

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Spring Boot Hibernate Spring
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2024 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.