After a lot of trial and error I still can\'t figure out the problem. The JSP, servlet, and database are all set to accept UTF-8 encoding, but even still whenever I use requ
That can happen if request and/or response encoding isn't properly set at all.
For GET requests, you need to configure it at the servletcontainer level. It's unclear which one you're using, but for in example Tomcat that's to be done by URIEncoding
attribute in
element in its /conf/server.xml
.
For POST requests, you need to create a filter which is mapped on the desired URL pattern covering all those POST requests. E.g. *.jsp
or even /*
. Do the following job in doFilter()
:
request.setCharacterEncoding("UTF-8");
chain.doFilter(request, response);
For HTML responses and client side encoding of submitted HTML form input values, you need to set the JSP page encoding. Add this to top of the JSP (you've probably already done it properly given the fact that displaying UTF-8 straight form DB works fine).
<%@page pageEncoding="UTF-8" %>
Or to prevent copypasting this over every single JSP, configure it once in web.xml
:
*.jsp
UTF-8
For source code files and stdout (IDE console), you need to set the IDE workspace encoding. It's unclear which one you're using, but for in example Eclipse that's to be done by setting Window > Preferences > General > Workspace > Text File Encoding to UTF-8.
Do note that HTML tags are ignored when page is served over HTTP. It's only considered when page is opened from local disk file system via
file://
. Also specifying is unnecessary as it already defaults to response encoding used during serving the HTML page with the form. See also W3 HTML specification.