1

Lately I have been working on creating a few HTTP services using a Tomcat 7.0 server. The code below is a html file that I used to test my service.

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>              
                <form id="fm" method="post" action="http://localhost:8080
/DataService/ProductData?category=Ser + bø&location=Herlev##Vejle">
                    <input type="submit" value="Submit">
                </form>
</body>
</html>

My problem is regarding the category parameter "Ser + bø". On the server side it is read as "Ser bø" with a three spaces instead of a space,"+",space. As far as I know the problem has likely to do something with encoding. I am using UTF-8 for the request. I have also change the configuration for the Tomcat server so that it uses UTF-8. Any ideas as to what I am doing wrong?

Cœur
  • 37,241
  • 25
  • 195
  • 267

2 Answers2

0

Modifying your form action URL to pass parameters to back-end is not the right way of dealing with forms. The best way to avoid all of that is just to use a separate form parameters instead:

<form id="fm" method="post" action="http://localhost:8080
/DataService/ProductData?category=Ser %2B bø&location=Herlev##Vejle">
   <input type="hidden" name="category" value="Ser %2B bø">
   <input type="hidden" name="location" value="Herlev##Vejle">
   ...
</form>

That's because URL has it's own encoding rules, based on RFC 3986. Plus stands for space there. So you should encode your URL prior to passing it to the server, either using JSP or JavaScript. Encoded + is %2B:

<form id="fm" method="post" action="http://localhost:8080
/DataService/ProductData?category=Ser %2B bø&location=Herlev##Vejle">

In case that it is a user input you will have to use JavaScript to encode it, as described here.

Community
  • 1
  • 1
Oleg Mikheev
  • 17,186
  • 14
  • 73
  • 95
0

The + is the URL encoded representation of a space. That's why you got it back as a space. You basically need to URL-encode the individual parameter names and values.

Given that you're running Tomcat, you're most likely also running JSP. In JSP, you can create properly encoded URLs with JSTL <c:url> and <c:param> as follows:

<c:url var="formActionURL" value="http://localhost:8080/DataService/ProductData">
    <c:param name="category" value="Ser + bø" />
    <c:param name="location" value="Herlev##Vejle" />
</c:url>
<form action="#{formActionURL}">
    ...
</form>
Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555