3

When the text is entered in a text field of JSP containing any '+' symbol in it, it is getting replaced by space when the parameter value is retrieved inside the servlet through request.getParameter("abc").

I read a few blogs and came to know that encoding needs to be done to successfully read the exact text but it is not clear to me. Can someone please help me out on this.

Arunabh
  • 167
  • 1
  • 7
  • 18

2 Answers2

2

+ is illegal inside a parameter value, as it is the defined separator between parameter name-value pairs. You need to URLEncode both parameter names and parameter values before sending.

user207421
  • 305,947
  • 44
  • 307
  • 483
0

Cannot reproduce.

Even with

<form action="rep" method="GET">
    <input name="foo" type="text"/>
</form>

the servlet at rep (even if is is a jsp) receives foo=a%2Bb as query string when I type a+b in the input field.

I suppose you are manually generating the query, and in that case, you must url encode the parameters.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • Adding a link to a url encoding post: http://stackoverflow.com/questions/332872/encode-url-in-javascript – Teddy May 18 '15 at 13:13
  • @Teddy : where did OP speak of javascript ? – Serge Ballesta May 18 '15 at 13:49
  • By your argument, he is manually generating it. I assumed it must be with Java script. Although it could be generated in scriptlet also. – Teddy May 18 '15 at 15:17
  • @Teddy : I think you are right, he must be using JavaScript. My comment was not against you, but more on the question that should precise the context since it is not a simple browser. – Serge Ballesta May 18 '15 at 15:23
  • I was using an AJAX method to submit the form. Wanted to know if there is a way to do it in AJAX or javascript way. I tried using URIComponentEncoder but it didnt work. For time being have replaced + by %2B in the text fields and it has worked for me. But I was looking for a solution in AJAX or javascript way. – Arunabh May 20 '15 at 11:12
  • @Arunabh : so edit your question to say it, and add relevant tags. But I'm afraid your question is just a duplicate than the one proposed by Teddy. – Serge Ballesta May 20 '15 at 15:07