我使用一个小的HTML表单来处理对tomcat服务器(servlet)的请求。如果我使用“GET”请求,我实现的“SetCharacterEncodingFilter”工作得非常好,结果显示了我所有的德国“变形金刚”。 但是如果我使用POST请求(不幸的是我需要这样做),所有的“变音符号”看起来都非常有趣; - )
HTML部分如下所示:
<form id="form1" name="form1" method="POST"
accept-charset="uft-8"
action="http://localhost:8080/foo">
<p>
<label for="textfield"></label>
<textarea name="text" id="text"
cols="45" rows="5"></textarea>
</p>
</form>
Servlet部分:
protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException, JSONException, Exception {
response.setContentType("text/html;charset=UTF-8");
String querytext = request.getParameter("text");
...
...
有人可以帮忙吗?
提前致谢!
答案 0 :(得分:0)
不要在响应上设置字符集。这对客户端发送给您的内容没有影响。它只会影响您发送回客户端的内容。
而是尝试在之前设置字符集 获取参数:
request.setCharacterEncoding("UTF-8") // or ISO-8859-1, you have to check
String querytext = request.getParameter("text");
你得到的字符集取决于最初发送给客户端的HTTP标头,因此浏览器通常会尊重这一点并使用相同的字符集进行POST。
答案 1 :(得分:0)
对于GET请求,您需要设置URIEncoding =&#34; UTF-8&#34; on servercat中的tomcat连接器标记;对于POST请求,charset过滤器应该可以工作,但它必须是配置的第一个过滤器。见:http://wiki.apache.org/tomcat/FAQ/CharacterEncoding#Q3