3

Hei. I am using the POST method for sending some information from a JSP to a Servlet. I cannot understand why when I send through the POST method a "+" character, it will be replace with a space character. Example: when I type the following String: 4+5 -> the Servlet will return 4 5; it replaces all the "+" signs. How can I fix this thing? I really need the "+" characters to be visible because after that I need to evaluate the expressions .

Steffi
  • 867
  • 5
  • 14
  • 29
  • 1
    "+" is a special character. In an URL, you need to encode it replacing it with "%2B". Here is a list a special chars replacements: http://www.blooberry.com/indexdot/html/topics/urlencoding.htm – Benoit Courtine Apr 22 '12 at 18:45
  • @BenoitCourtine The data are being sent via POST, not as part of a URL. However, the post is most likely being sent with content-type of application/x-www-form-urlencoded, which makes your basic point still valid. – Ted Hopp Apr 22 '12 at 18:51

4 Answers4

3

Form variables are sent URL encoded. The "+" plus character is (one) URL encoding of a space.

See also: AJAX POST and Plus Sign ( + ) -- How to Encode?

If you want to send a literal plus sign, you would need to URL encode it either through Javascript or hard-coded "%2B".

Community
  • 1
  • 1
wrschneider
  • 17,913
  • 16
  • 96
  • 176
1

You need to URLEncode your data before sending it to the server. The server is trying to decode unencoded data -- the result is that + is decoded to a space.

Travis Webb
  • 14,688
  • 7
  • 55
  • 109
1

The servlet is evidently expecting the data to arrive with URL encoding, as described in the W3 document on HTTP form submission. You need to either change your content-type for the POST or (better) encode the data you are sending. You can encode the "+" signs as "%2B".

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
1

When encoding URLs, the + character indicates a space. If you need to use this character in a URL, you'll have to escape it like this:

4+5

Becomes

4%2B5
Óscar López
  • 232,561
  • 37
  • 312
  • 386