Newsletter |
Example on getParameterValues() method of Servlet Request
Servlets » on Jan 28, 2013 { 9 Comments } By Sivateja
The method getParameterValues() will generally came into picture if there is a chance of getting multiple values for any input parameter, this method will retrieve all of it values and store as string array.
Syntax
String[] values = getParameterValues(“Input Parameter”);
Directory Structure
Files Required
- index.html
- OngetParameterValues.java
- web.xml
index.html
<font face="verdana" size="2px"> <form action="onGPV" method="post"> Habits : <input type="checkbox" name="habits" value="Reading">Reading <input type="checkbox" name="habits" value="Movies">Movies <input type="checkbox" name="habits" value="Writing">Writing <input type="checkbox" name="habits" value="Singing">Singing <input type="submit" value="Submit"> </form> </font>
OngetParameterNames.java
package java4s; import java.io.IOException; import java.io.PrintWriter; import java.util.Iterator; import java.util.Map; import java.util.Set; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class OngetParameterValues extends HttpServlet { protected void doPost(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException { PrintWriter pw=res.getWriter(); res.setContentType("text/html"); String[] values=req.getParameterValues("habits"); pw.println("Selected Values..."); for(int i=0;i<values.length;i++) { pw.println("<li>"+values[i]+"</li>"); } pw.close(); } }
web.xml
<web-app> <servlet> <servlet-name>ongetParameterValues</servlet-name> <servlet-class>java4s.OngetParameterValues</servlet-class> </servlet> <servlet-mapping> <servlet-name>ongetParameterValues</servlet-name> <url-pattern>/onGPV</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
9 Responses to “Example on getParameterValues() method of Servlet Request”
What is the difference between getParameterMap() and getParameterValues() ?
sir i am doing the same thing but the checkboxes are dynamically generated and want to get the values of several checkboxes to other servlet.
Please help as soon as possible
How can i store those values in database
Helpful code.I was stuck since long.
By the snippet
String[] values=req.getParameterValues(“habits”);
It solved.
What changes need to be done if I have multiple html files.Suppose I have to jump from one html to another depending on user choice and then call the .java file then the above code doesnot work. So please let me know what changes are to be done.
Explanation of each and every topic is simply superb sir.
thank you sir
nice very useful for begin
How can i store those values in database
great very useful for me…..thank you ,..so mach