user1455346
user1455346

Reputation:

Dumping the contents of a java.util.Map - Not working as expected

I'd like to base this question on the basis of the response posted by @Greg Kopff here

Basically, what I'm trying to do is to DUMP the contents of the Map returned by request.getParameterMap() method to the output.

I'm getting the following weird Map as output(the values of which I believe are the addresses of the object references of either String or String arrays)


    {submit=[Ljava.lang.String;@1fe4169, color=[Ljava.lang.String;@178920a,  
         chek_games=[Ljava.lang.String;@bbfa5c, foo=[Ljava.lang.String;@131de9b}

I have tried the following code -


    public class CoffeeSelect extends HttpServlet {

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Map requestParams = req.getParameterMap();

        resp.getWriter().println(requestParams);
        }

    }

The following is my HTML <form>


    <form action="SelectCofee.Do" method="post">
     <select id="coffe_color" name="color">
        <option value="green">Green</option>
        <option value="red">Red</option>
        <option value="blue">Blue</option>
     </select>

     <br/><br/>
     <input type="checkbox" id="chek_games" name="chek_games" value="chess" />
     <input type="checkbox" id="chek_games" name="chek_games" value="badminton" />
     <input type="checkbox" id="chek_games" name="chek_games" value="cricket" />

     <input type="hidden" id="foo" name="foo" value="bar" />

     <input type="submit" name="submit" value="Submit" />
    </form>

How to deference those object references or let me put it this way; Am I missing something here?

Upvotes: 0

Views: 207

Answers (1)

SJuan76
SJuan76

Reputation: 24885

The [L notation tells that it is the String representation of an String[]. HTTP lets you do something like

http://myserver.com/mypage?myparam=firstvalue&myparam=secondvalue....

(think of multi-selection <select>)

To allow for this, the parameter Map is not Map<String, String> but Map<String, String[]> (I do not know if the implementation really uses generics, it is just an explicit representation).

So, to dump the parameters, you need to either.

a) assume you won't get multiple values for the same param; loop for the param names and use request.getParameter

b) if you accept multiple values, loop for the param names and serialize the array.

Upvotes: 1

Related Questions