0

Is there some way to get all of the parameters contained in a HttpServletRequest object into a single String?

Olean
  • 1

3 Answers3

1
request.getQueryString(); // To get into a single string
request.getParameterMap(); // to get into a map of key-value pairs
Basanth Roy
  • 6,272
  • 5
  • 25
  • 25
0

There are a lot of frameworks that will do this sort of thing for you. It depends a lot on what technologies you are employing. Personally I prefer Spring for the simple reasons that it emcompasses pretty much everything I've needed to do. The only downside is that there's a lot to learn :-)

drekka
  • 20,957
  • 14
  • 79
  • 135
0

This at first sight simple problem is complicated by the fact that getParameterMap() returns a Map<java.lang.String,java.lang.String[]>, so attempts to toString() the return value don't give the desired result.

In case you would need the parameters as a JSON string anyways, or don't mind an extra dependency, this is a very simple solution using Jackson:

String asJson = new ObjectMapper().writeValueAsString(request.getParameterMap());

for

parm1=abc&parm=cde&parm3=fgh&parm3=ijk

it produces

{"parm1":["abc"],"parm":["cde"],"parm3":["fgh","ijk"]}
fvu
  • 32,488
  • 6
  • 61
  • 79
  • why wouldn't request.getQueryString() work? THat is what the OP is asking. – Basanth Roy Jul 12 '11 at 01:00
  • @rational it works, depending on what OP wants to do with the output string it may very well be sufficient. I just wanted to point out an alternative solution with some extra capabilities, like json structured output and grouped representation of parameters that occur more than once. – fvu Jul 12 '11 at 01:04