2

I just noticed that a @RequestParam variable that I use to check POST params, such as usernames, or ids, behaves rather strangely when the controller method gets called consecutive times. Rather than returning the current parameter value (for example, "Jason", "Michael", "John"), it returns a concatenated string of all parameter values that the method has been called with before that. It results in the variable having the value of: "Jason,Michael,John", rather than just "John" which was the last one.

I noticed that this strange behavior is per session. When I reduced the session duration to 1 minute only, i noticed that after the session is gone, so are the multiple values.

This thing never happens if I call request.getParameter("username"). Of course, I would like to stick to Spring MVC conventions if possible.

Is this a bug, or something intentional? How can I avoid it?

2
  • could you share your page from where you submit the request. will help analyzing.
    – Saket
    Commented Sep 21, 2011 at 17:06
  • please add the code of your controller class
    – Ralph
    Commented Sep 21, 2011 at 17:13

4 Answers 4

4

This is a bug in your JSP page. You likely have a hidden and an input with the same name. This results in a comma separated list of values.

11
  • That wouldn't necessarily explain why the values keep being concatenated more and more with each call. An input and hidden with the same name would at most just return 2 (or at any rate a fixed number of) values Commented Sep 21, 2011 at 17:08
  • it exactly explains why the values keep getting concatenated. the value in the hidden concats with the value in the input. then repeat. However, this is not guaranteed to be the cause of this question.
    – DwB
    Commented Sep 21, 2011 at 17:25
  • Yes I agree with the first 2 sentences of your comment. But how exactlly would a multi valued parameter trigger the repeat part all by itself? And besides the OP sais that by reducing his servlet session timeout it seems to clear the issue - so I think it's a lot more likely that it's something related to that. Commented Sep 21, 2011 at 17:28
  • 1. hidden has no value, input gets value; no comma separated value string. 2. hidden is populated with the value submitted from the input when page repaints. 3. input gets new value, hidden has the old value. on submit you get a comma separated value string since the input and the hidden have the same name. 4. repeat 2 and 3.
    – DwB
    Commented Sep 21, 2011 at 17:30
  • Dude:"I noticed that this strange behavior is per session. When I reduced the session duration to 1 minute only, i noticed that after the session is gone, so are the multiple values." Commented Sep 21, 2011 at 17:34
1

Try without spring annotations:

Add to your method an attribute named HttpServletRequest

String s = request.getParameter("parameterName")
0

Your attribute might be saved internally by spring in the http session and re-used from there. Do you by any chance, on you spring controller class, have a configuration that would make that parameter session scoped (Either @SessionAttributes("username") on the class, or requireSession=true in your xml declaration of the controller bean)?

Or do you'add it to the model when you rediplay your page from the controller?

0

Faced the same issue while implementing an ajax login and found out that it was caused by the redirect that is triggered after the failing attempts to login. Somehow was accumulating the j_usernamen and j_password parameters.

Not the answer you're looking for? Browse other questions tagged or ask your own question.