Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to set request encoding in Tomcat?
    primarykey
    data
    text
    <p>I have a problem in my Java webapp.</p> <p>Here is the code in index.jsp:</p> <pre><code>&lt;%@page contentType="text/html" pageEncoding="UTF-8" %&gt; &lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt; &lt;% request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); %&gt; &lt;html&gt; &lt;head&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt; &lt;title&gt;JSP Page&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;h1&gt;Hello World!&lt;/h1&gt; &lt;form action="index.jsp" method="get"&gt; &lt;input type="text" name="q"/&gt; &lt;/form&gt; Res: &lt;%= request.getParameter("q") %&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>When I wireshark a request, my browser sends this header:</p> <pre><code>GET /kjd/index.jsp?q=%C3%A9 HTTP/1.1\r\n ... Accept-Charset: UTF-8,*\r\n </code></pre> <p>And the Tomcat server returns me this:</p> <pre><code>Content-Type: text/html;charset=UTF-8\r\n </code></pre> <p>But if I send "é"(%C3%A9 in UTF-8) in my form, "é" is displayed instead.</p> <p>What I understand is that the browser sends an "é" encoded with UTF-8 (the %C3%A9).</p> <p>But the server interpret this as ISO-8859-1. So the %C3 is decoded as à and %A9 as ©, and then sends back the response encoded in UTF-8.</p> <p>In the code, the requests should be decoded with UTF-8:</p> <pre><code>request.setCharacterEncoding("UTF-8"); </code></pre> <p>But, if I send this url:</p> <pre><code>http://localhost:8080/kjd/index.jsp?q=%E9 </code></pre> <p>the "%E9" is decocded with ISO-8859-1 and an "é" is displayed.</p> <p>Why isn't this working? Why requests are decoded with ISO-8859-1?</p> <p>I've tried it on Tomcat 6 and 7, and on Windows and Ubuntu.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload