Saturday, May 4, 2013
Today I will tell you how you can get the parameter values submitted by a client to a server using servlets. Here you will see different process of extracting the form parameters depending on your requirements. Here we will use getParameter() , getParameterValues() , getParameterNames() and getParameterMap() methods.
getParameter() : This method returns only a single value associated with the parameter name. This is generally used when you have only one value for a parameter.
getParameterValues() : It returns an array of all values associated with a parameter name. This is used when you have multiple values for a parameter.
getParameterNames() : It returns an Enumeration<String> which are the different parameter names. Now you can use individual name and extract the values. This is used when you dont know the param names. But in reality you will always know them from beforehand.
getParameterMap() : It returns a Map of parameter names mapped to all values associated with each parameter. This is the most general case.
--------------------------------------------------------------------------------------------------------------------------
import java.util.Map;
import java.util.Set;
import java.util.Iterator;
import java.util.Enumeration;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.annotation.WebServlet;
@WebServlet("/paramReader")
public class ParamReader extends HttpServlet{
public void doGet(HttpServletRequest req,HttpServletResponse res){
//used when one parameter has only one value
String fn=req.getParameter("fn");
//used when one parameter has more than one vakue
String str[]=req.getParameterValues("fn"); //getting all values
//used when parameter names are unknown
Enumeration<String> en=req.getParameterNames(); //getting all names
while(en.hasMoreElements())
req.getParameter(en.nextElement()); //getting vakue
//used to get whole map
Map<String,String[]> m=req.getParameterMap();
Set<String> s=m.keySet(); //key set means param names
Iterator<String> it=s.iterator();
while(it.hasNext())
m.get(it.next()); //param values
}
}
--------------------------------------------------------------------------------------------------------------------------
DOWNLOAD the source from Mediafire
DOWNLOAD the source from 4shared
getParameter() : This method returns only a single value associated with the parameter name. This is generally used when you have only one value for a parameter.
getParameterValues() : It returns an array of all values associated with a parameter name. This is used when you have multiple values for a parameter.
getParameterNames() : It returns an Enumeration<String> which are the different parameter names. Now you can use individual name and extract the values. This is used when you dont know the param names. But in reality you will always know them from beforehand.
getParameterMap() : It returns a Map of parameter names mapped to all values associated with each parameter. This is the most general case.
--------------------------------------------------------------------------------------------------------------------------
Servlet Source Code
--------------------------------------------------------------------------------------------------------------------------import java.util.Map;
import java.util.Set;
import java.util.Iterator;
import java.util.Enumeration;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.annotation.WebServlet;
@WebServlet("/paramReader")
public class ParamReader extends HttpServlet{
public void doGet(HttpServletRequest req,HttpServletResponse res){
//used when one parameter has only one value
String fn=req.getParameter("fn");
//used when one parameter has more than one vakue
String str[]=req.getParameterValues("fn"); //getting all values
//used when parameter names are unknown
Enumeration<String> en=req.getParameterNames(); //getting all names
while(en.hasMoreElements())
req.getParameter(en.nextElement()); //getting vakue
//used to get whole map
Map<String,String[]> m=req.getParameterMap();
Set<String> s=m.keySet(); //key set means param names
Iterator<String> it=s.iterator();
while(it.hasNext())
m.get(it.next()); //param values
}
}
--------------------------------------------------------------------------------------------------------------------------
Download Links
--------------------------------------------------------------------------------------------------------------------------DOWNLOAD the source from Mediafire
DOWNLOAD the source from 4shared
Labels:J2EE
Subscribe to:
Post Comments
(Atom)
Total Pageviews
Followers
Labels
- Algorithms (7)
- Annotation (3)
- Files (6)
- Generics (3)
- Graphics2D (5)
- Graphics2D-Images (7)
- Inheritance (2)
- J2EE (9)
- Java 8 (4)
- Java FAQs (19)
- JDBC (3)
- Networking (2)
- Packages (1)
- Reflection (4)
- Security (7)
- Sorting (2)
- Swing (3)
- Threads (3)
- Utils (3)
Popular Posts
-
Today I will show you how you can implement Bankers algorithm in Java. The Banker's algorithm is a resource allocation and deadlock a...
-
------------------------- UPDATE ------------------------- I have updated the code on request of some followers so that they can directly...
-
Today I am going to show how to convert a postfix expression to an infix expression using stack in Java. In an earlier post here we ...
-
Today in this article I will tell you how to convert an infix expression to postfix expression using stack. This is an important applicat...
-
--------------------UPDATE------------------- I have updated my post so that now it can detect IE 11. This modification was necessary as t...
-
Today I am going to show you how you can generate and validate captcha. A CAPTCHA (an acronym for "Completely Automated Public Turin...
-
Today I am going to post a program that will be able to produce all the mColorings of a given graph G. What is mColoring : The problem st...
-
Today in this article I will show you how to create or develop a Tower of Hanoi game in Java. The Tower of Hanoi is a famous problem tha...
0 comments:
Post a Comment