Foren

Liferay 7.0 form submission UTF-8 encoding issue

Roman S., geändert vor 7 Jahren.

Liferay 7.0 form submission UTF-8 encoding issue

New Member Beiträge: 6 Beitrittsdatum: 13.03.17 Neueste Beiträge
I have a liferay bundle Liferay Community Edition Portal 7.0.3 GA4 (Wilberforce / Build 7003 / August 5, 2016) used along with two hooks - a jsp hook and an action hook.

When a jsp form is submitted, in my processAction I receive 4 character sequences instead of correct utf-8 symbols.

I tried:

1. I added -Dfile.encoding=UTF8 to my Window->Preferences->Java->Installed JREs->My JRE(jdk1.8.0_121)->Default VM parameres.
2. I chose UTF-8 in Window->Preferences->Workspace->Text file encoding->Other.
3. I checked that in my project properties in resources it shows UTF-8 in Text file encoding.
4. I set UTF-8 in Window->Preferences->Workspace->Text->Default encoding.
5. I checked that Connector tag in server.xml of my tomcat server has URIEncoding="UTF-8" in it.
6. I added <%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> and <meta content="text/html; charset=UTF-8" http-equiv="content-type"> to my jsp page.
7. I added the following code to web.xml of my tomcat server:
<filter>
<filter-name>SetCharacterEncoding</filter-name>
<filter-class>
org.apache.catalina.filters.SetCharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
8. I checked that the encoding in the beginning of my processAction method by calling actionRequest.getCharacterEncoding() and it was UTF-8.

Nothing above worked. The only way to get parameters right was getting parameters with the following code:

String parameter = new String (actionRequest.getParameter("parameter").getBytes("iso-8859-1"), "UTF-8");

Could you help me with making liferay to get parameters to my processAction encoded right?
Roman S., geändert vor 7 Jahren.

RE: Liferay 7.0 form submission UTF-8 encoding issue

New Member Beiträge: 6 Beitrittsdatum: 13.03.17 Neueste Beiträge
For those who may be interested. I found an answer to my question.

I figured out that #7 above must be done in a certain way to work: <filter> block must be added before any other <filter> blocks in web.xml. This is important because the documentation for setCharacterEncoding

https://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html#setCharacterEncoding%28java.lang.String%29

says that "This method must be called prior to reading request parameters or reading input using getReader(). Otherwise, it has no effect."

Apparently, if any filter invokes getReader() before org.apache.catalina.filters.SetCharacterEncodingFilter sets the encoding to UTF-8, this piece will have no effect.