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 .:: | ||
Comments
4 Responses to “Example on getParameterNames() method of Servlet Request Object”
what is the use of tag in web.xml ?
sir how can we print entire page throgh servlet
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
Its so simple to understand