Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

request.getParameterMap() - getting the Strings - a better way?

 
Sam Szabo
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64824
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sam Szabo
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that's right.
Looking more like dto.setAddress(request.getParameter("address"));
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64824
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1712
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic