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?

4

2 回答 2

0

修改表单操作 URL 以将参数传递给后端不是处理表单的正确方法。避免所有这些的最好方法就是使用单独的表单参数:

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

这是因为 URL 有它自己的编码规则,基于 RFC 3986。Plus代表那里的空间。因此,您应该在将 URL 传递到服务器之前对其进行编码,使用 JSP 或 JavaScript。编码+%2B

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

如果它是用户输入,则必须使用 JavaScript 对其进行编码,如此所述。

于 2013-01-21T10:33:05.360 回答
0

+是空间的 URL 编码表示。这就是为什么你把它作为一个空间回来了。您基本上需要对各个参数名称和值进行 URL 编码。

鉴于您正在运行 Tomcat,您很可能也在运行 JSP。在 JSP 中,您可以使用JSTL 创建正确编码的 URL,<c:url>如下<c:param>所示:

<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>
于 2013-01-21T10:49:33.787 回答