Reading Parameters From a Form
Servlets often use standard HTML forms. The container parses the form input, and makes the names and values of the parameters available in the request object passed to the servlet.
The getParameter() function takes a parameter name and returns the value of the parameter, or an empty string if the request does not contain a parameter by that name. For example, the code below finds the value of the username parameter:
std::string username = request.getParameter("username");
Some parameters contain more than one value, for example, the results from a checkbox. The function getParameterValues() returns an std::list containing the values submitted for a parameter. If the request does not contain a parameter by that name or the parameter has no value, getParameterValues() returns an empty list.
The code sample below shows one way to process a parameter with more than one value:
std::list<std::string> choices = request.getParameterValues("choices");
list::iterator iter = choices.begin();
while (iter != choices.end()) {
std::string choiceValue = *iter;
// process choiceValue
iter++;
}
The
getParameterNames() function returns a
rwsf::Enumeration containing the names of all parameters in the request. The code below processes each parameter in the request:
rwsf::Enumeration<std::string> names =
request.getParameterNames();
while(names.hasMoreElements()) {
std::string name = names.nextElement();
std::list<std::string> values = request.getParameterValues(name);
list::iterator iter = values.begin();
while (iter != values.end()) {
std::string value = *iter;
// process value for name
}
}