Steffi
Steffi

Reputation: 917

POST Method replacing the '+' characters

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 .

Upvotes: 3

Views: 5374

Answers (4)

Óscar López
Óscar López

Reputation: 236112

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

Upvotes: 1

Ted Hopp
Ted Hopp

Reputation: 234847

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".

Upvotes: 1

Travis Webb
Travis Webb

Reputation: 15018

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.

Upvotes: 1

wrschneider
wrschneider

Reputation: 18780

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".

Upvotes: 3

Related Questions