Handling Form Data from Servlets
by, 11-28-2011 at 07:23 PM (2209 Views)
A feature which is of immense benefits to all programmers working with servlets is that all form data parsing is handled automatically. Rather than needing to build your own called for parsing form data, you only need to call the getParameter method of the Http- ServletRequest, providing the relevant parameter name as an argument. You use getParameter in the same way whether data is sent by GET or by POST. The servlet knows which request method was used and calls the proper method to handle the request. The method returns a String corresponding to the URL-decoded value of the first occurrence of that parameter name. If there is no parameter, null is returned. If there is a parameter but it has no value, an empty string is returned and if the parameter has multiple values, you need to call getParameterValues. The getParameterValues similar to getParameter, returns null or an empty string dependent on whether the parameter exists (empty string) or not (null value). Finally remember that parameter names are case so you cannot do the following:
For this: request.getParameter("FooParam");
Finally, when you’re debugging your application it is often useful to get a full list of parameters, In that case, call getParameterNames in order to get this list. It is provided in the form of an Enumeration which you can then loop through to get the actual names needed for every parameter provided by the HTTP Request.