I have a problem in my Java webapp.
Here is the code in index.jsp:
<%@page contentType="text/html" pageEncoding="UTF-8" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<% request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Hello World!</h1>
<form action="index.jsp" method="get">
<input type="text" name="q"/>
</form>
Res: <%= request.getParameter("q") %>
</body>
</html>
When I wireshark a request, my browser sends this header:
GET /kjd/index.jsp?q=%C3%A9 HTTP/1.1
...
Accept-Charset: UTF-8,*
And the Tomcat server returns me this:
Content-Type: text/html;charset=UTF-8
But if I send "é"(%C3%A9 in UTF-8) in my form, "??" is displayed instead.
What I understand is that the browser sends an "é" encoded with UTF-8 (the %C3%A9).
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.
In the code, the requests should be decoded with UTF-8:
request.setCharacterEncoding("UTF-8");
But, if I send this url:
http://localhost:8080/kjd/index.jsp?q=%E9
the "%E9" is decocded with ISO-8859-1 and an "é" is displayed.
Why isn't this working? Why requests are decoded with ISO-8859-1?
I've tried it on Tomcat 6 and 7, and on Windows and Ubuntu.
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…