0

I have a Servlet as simple as this:

public class MyServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setAttribute("processedName", request.getParameter("name"));
        request.setAttribute("processedQueryString", request.getQueryString());
        request.getRequestDispatcher("index.jsp").forward(request, response);
    }
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

My index.jsp is like this:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Title</title>
</head>
<body>
    <form action="MyServlet" method="get">
        Name: <input type="text" name="name">
        <input type="submit" value="Submit">
    </form>
    <br>
    After: <%=request.getAttribute("processedName") %>
    <br>
    Query String: <%=request.getAttribute("processedQueryString") %>
</body>
</html>

If I type Thomás in the form and submit it, Thom?s is printed.

Both the URL in browser and the query string in JSP show name=Thom%E1s

The browser is correctly encoding the ISO-8859-1 á char as %E1, but for some reason the .getParameter is not decoding it correctly.

I've seen tons of posts like this here when people try to submit non ISO-8859-1 chars (unlike á). Shouldn't Tomcat 8 use ISO-8859-1 as default for decoding?

Strangely, if I put accept-charset="UTF-8" in my form, it works. If I change the method to method="post" it works. If I mix both the accept-charset="UTF-8" and method="post" it doesn't work (but now Thomás is printed!).

It's as if Tomcat is waiting UTF-8 in get method and ISO-8859-1 in post method.

Gustavo
  • 1,332
  • 2
  • 16
  • 39

1 Answers1

0

Default URIEncoding in tomcat 8 is UTF-8 except if the property org.apache.catalina.STRICT_SERVLET_COMPLIANCE is set to true, then it is ISO-8859-1.

In your case you can try to add the following :

 URIEncoding="ISO-8859-1"

in your server.xml file to the Connector element.

See the tomcat documentation for all details.

Cédric Couralet
  • 4,913
  • 1
  • 21
  • 21