| Author |
request.getParameterMap() - getting the Strings - a better way?
|
Sam Szabo
Greenhorn
Joined: Oct 04, 2006
Posts: 9
|
|
Hi folks - attempting to simplify here pls. exuse if not clear, let me know - I want to pass a Map of all the request parameters from my Servlet code into a method in another object, so they can be moshed and inserted into DB. So I have come up with something like this in the servlet processor: public class savingStuffProcessor { public String execute (HttpServletRequest request) { ... myUtils.saveStuff(id, request.getParameterMap()); ... } } and in the DB container: public String saveStuff(int id, java.util.Map params) { .... hibernateDAO.setAddress(((String[])params.get("address"))[0]); .... } which does work, so fine... but.... My question is, this syntax seems overcomplicated. Is there a better simpler way to accomplish what I am trying to do? I could use request.getParameter() for each value passing along as seperate args - but I kinda need all the 25 or so form field values in the saveStuff() function and functions with 25+ arguments scare me... should I just pass along the whole HttpServletRequest object to saveStuff()? how is this usually done, or is it not? the hibernateDAO.setAddress() method expects a String, I can't change that.
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56233
|
|
Originally posted by Sam Szabo: should I just pass along the whole HttpServletRequest object to saveStuff()? how is this usually done, or is it not?
No, that would introduce a dependency on the servlet layer where it is not needed and not appropriate. Any of passing the map, extracting the info into separate parameters, or creating a data transfer object to hold the data is far preferable to passing servlet-specific structures.
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
Sam Szabo
Greenhorn
Joined: Oct 04, 2006
Posts: 9
|
|
Thanks for the reply:
creating a data transfer object
I understand the first two options you mention - but any pointers on how I "create a data transfer object"? you mean like a generic Object object? dto.address = request.getParameter("address"); dto.city = request.getParameter("city"); and so forth? or something else? thanks
|
 |
Christophe Verré
Sheriff
Joined: Nov 24, 2005
Posts: 14672
|
|
Yes, that's right. Looking more like dto.setAddress(request.getParameter("address"));
|
[My Blog]
All roads lead to JavaRanch
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56233
|
|
|
Yes, they generally follow the bean pattern and do not include any processing. Their purpose is to transfer data in an agnostic manner (in other words, not tied to a particular UI, or to a particular DB, or a particular framework, or a particular anything).
|
 |
Sam Szabo
Greenhorn
Joined: Oct 04, 2006
Posts: 9
|
|
thanks, what you are telling me is in line with the code I was building off of. The original dealt with a form with 3 fields, but mine has 30, so I tried to find something smoother. If someone wants to add a field to the registration form, right now we have to change FIVE files but I will try to resist re-inventing the wheel. thanks guys, always learning.
|
 |
Stefan Evans
Bartender
Joined: Jul 06, 2005
Posts: 1005
|
|
You might consider using the jakarta commons BeanUtils package to help with the population of beans from a request properties map. You would just use the method BeanUtils.populate(myBean, request.getParameterMap());
|
 |
Sam Szabo
Greenhorn
Joined: Oct 04, 2006
Posts: 9
|
|
interesting Stefan thanks I just read this: http://jakarta.apache.org/commons/beanutils/apidocs/org/apache/commons/beanutils/package-summary.html#conversion which sounds good - almost exactly what I had in mind - but if I may extend this thread - where would you do form validation using this? do it before you call BeanUtils.populate(), in the processor? [ November 01, 2006: Message edited by: Sam Szabo ]
|
 |
 |
|
|
subject: request.getParameterMap() - getting the Strings - a better way?
|
|
|