I have a maven project named alpha, compiled by java 1.8.0_121 and built with maven 3.3.3.
This project contains three modules
- alphaCommon
- alphaBusiness
- alphaWeb
each module has its pom.xml and the artifact alpha-1.0.war is generated under alphaWeb/target.
This artifact is then deployed to a local JBoss server EAP 7.1.0 running on Windows 10.
The alpha project uses Struts 1 and Hibernate as frameworks, and the dynamic data are saved and retrieved from a MySQL database (database name: alpha, CHARACTER SET NAME: latin1, COLLATION NAME: latin1_swedish_ci) installed to a local MySQL Server, version number 5.0.95.
There is a web page of the project with a form having a input text field named description where stressed characters à, è, é, ì, ò, ù plus other special caracters such as . – % ^ ‘ and blank character should be accepted.
On submit data, a java method addElement() in a class AjaxServlet.java is invoked; it gets an HttpServletRequest input parameter, checks the format of the input data and if matches the pattern of the allowed characters, the input data are added in a database table.
The method addElement is the following
private String addElement(HttpServletRequest request) {
try {
request.setCharacterEncoding("UTF-8");
} catch (UnsupportedEncodingException ue) {
loggerajax.traceLog(loggerajax.INFO, ue.getMessage());
return "WRONG_CHARACTER_ENCODING";
}
String sdescr = request.getParameter("description");
loggerajax.traceLog(loggerajax.INFO, "description: " + sdescr);
String patternAllowedCharacters = AlphaParameters.getInstance().getParameter("allowed_characters");
Pattern pattern = Pattern.compile(patternAllowedCharacters, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(sdescr);
if (!matcher.matches()) {
return "NO_MATCH";
}
....
return "ROW_ADDED_IN_TABLE";
}
the instruction
loggerajax.traceLog(...);
simply creates and adds in a custom JBoss logfile named alpha.log, the string passed as an input parameter, with INFO level.
The strings returned from the addElement method
- “WRONG_CHARACTER_ENCODING”
- “NO_MATCH”
- “ROW_ADDED_IN_TABLE”
result in a specific message on the web.
The instruction
String patternAllowedCharacters = AlphaParameters.getInstance().getParameter("allowed characters");
simply extracts from a table of the alpha database, named tbl_parameters, the following string
[\.-s%A-Za-z0-9òàìèéù’^]*
and assigned to the patternAllowedCharacters
String variable.
If sdescr
contains a character not found in the above pattern, matcher.matches()
returns false and addElement
returns
“NO_MATCH”; otherwise, the script goes on and “ROW_ADDED_IN_TABLE” is returned, which means successful operation.
Unfortunately, the stressed characters, despite being in the pattern of the allowed ones, aren’t matched!
I googled around and seems as if the problem is due to a bad character encoding, and UTF-8 should be used! I found several solutions which I implemented all together:
-
placed the tag
<meta charset="UTF-8"/>
at the top of all the jsp
pages -
set the character encoding to UTF-8 to the HttpServletRequest request
variable (seerequest.setCharacterEncoding("UTF-8")
in the try
block of the addElement method) -
added in the struts-config.xml (located at
alpha/alphaWeb/src/main/webapp/WEB-INF) the following plugin<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn"> <set-property property="contextConfigLocation" value="/WEB-INF/web-character-encoding.xml"/></plug-in>
-
placed the file web-character-encoding.xml under
alpha/alphaWeb/src/main/webapp/WEB-INF with defined the following bean<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="characterEncodingFilter" class="org.springframework.web.filter.CharacterEncodingFilter"> <property name="encoding" value="UTF-8"/> <property name="forceEncoding" value="true"/> </bean> </beans>
-
added in the web.xml (located at
alpha/alphaWeb/src/main/webapp/WEB-INF) the following filter<filter> <filter-name>characterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
-
added in the pom.xml of the alphaWeb module the following
dependencies<dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>2.5.4</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>2.5.4</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>2.5.4</version> </dependency>
(note: unfortunately I am not allowed to upgrade the version of the project and all of its maven dependencies, nor changing the charset or the collation of the database and tables!)
I checked out the server.log but I get no error; in the alpha.log file, the value of sdescr is the following:
description: ?
the file standalone.xml in the JBoss server, has the following configuration for the alpha.log file:
<profile>
<subsystem xmlns="urn:jboss:domain:logging:3.0">
<console-handler name="CONSOLE">
<level name="INFO"/>
<encoding value="UTF-8"/>
<target name="console"/>
</console-handler>
<file-handler name="ALPHA_FILE">
<level name="DEBUG"/>
<encoding value="UTF-8"/>
<file relative-to="jboss.server.log.dir" path="alpha.log"/>
</file-handler>
<periodic-size-rotating-file-handler name="FILE" autoflush="true">
<level name="DEBUG"/>
<formatter>
<named-formatter name="PATTERN"/>
</formatter>
<file relative-to="jboss.server.log.dir" path="server.log"/>
<rotate-size value="3072k"/>
<max-backup-index value="3"/>
<suffix value=".yyyy-MM-dd"/>
<append value="true"/>
</periodic-size-rotating-file-handler>
<logger category="com.arjuna">
<level name="WARN"/>
</logger>
<logger category="org.jboss.as.config">
<level name="INFO"/>
</logger>
<logger category="sun.rmi">
<level name="WARN"/>
</logger>
<logger category="org.hibernate" use-parent-handlers="false">
<level name="ERROR"/>
</logger>
<logger category="org.apache.struts" use-parent-handlers="false">
<level name="ERROR"/>
</logger>
<logger category="org.springframework" use-parent-handlers="false">
<level name="ERROR"/>
</logger>
<root-logger>
<level name="INFO"/>
<handlers>
<handler name="CONSOLE"/>
<handler name="FILE"/>
</handlers>
</root-logger>
<formatter name="PATTERN">
<pattern-formatter pattern="%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p [%c] (%t) %s%e%n"/>
</formatter>
<formatter name="COLOR-PATTERN">
<pattern-formatter pattern="%K{level}%d{HH:mm:ss,SSS} %-5p [%c] (%t) %s%e%n"/>
</formatter>
</subsystem>
...
</profile>
hope I gave you all information. Thank you very much for any help or useful suggestion!