0

In my Application I made it to have it in two languages. English and French. In English, its all right. But while Reading the Properties file for French Language it ends up showing mojibake. I have in my properties file like this: example.french.char=é

but it does not show properly in the application. I am viewing my app in Windows. I dont want to replace the French characters by Unicode characters. I want them to be read from properties file as they normally appear.

How can I make it work? Please suggest. Can I make it happen to view the French Characters without using Unicodes?

My properties file is encoded in UTF-8. Please suggest!

I tried the following too..

public class CharacterEncodingFilter implements Filter{

private FilterConfig config;
@Override
public void destroy() {
    setConfig(null);
}

@Override
public void doFilter(ServletRequest request, ServletResponse response,
        FilterChain filter) throws IOException, ServletException {

    request.setCharacterEncoding("UTF-8");

    response.setCharacterEncoding("UTF-8");
    response.setContentType("text/html; charset=UTF-8");

    filter.doFilter(request, response);
}

@Override
public void init(FilterConfig config) throws ServletException {
    setConfig(config);
}

public FilterConfig getConfig() {
    return config;
}

public void setConfig(FilterConfig config) {
    this.config = config;
}
}

and added this in web.xml

<filter>
    <filter-name>charEncoding_Filter</filter-name>
    <filter-class>com.edfx.tum.filter.CharacterEncodingFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>charEncoding_Filter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

but still it shows mojibake.

NDeveloper
  • 3,115
  • 7
  • 23
  • 34

2 Answers2

0

Did you try to set request/response encoding type to UTF-8, with servlet filter class. Here is an example:

import java.io.IOException;
import java.io.Serializable;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class SetCharacterEncodingFilter implements Filter, Serializable {

private static final long serialVersionUID = -4518853484989127891L;

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    request.setCharacterEncoding("UTF-8");
    response.setCharacterEncoding("UTF-8");
    chain.doFilter(request, response);
}

@Override
public void destroy() {

}

@Override
public void init(FilterConfig arg0) throws ServletException {

}
}

Then you register the filter in web.xml

<filter>
    <filter-name>UTF8Charset</filter-name>
<filter-class>your.package.name.SetCharacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>UTF8CharSet</filter-name>
<url-pattern>*</url-pattern>
</filter-mapping>
britulin
  • 310
  • 2
  • 4
0

If your properties file is in UTF-8 you may have an issue as it should be in ISO-8859-1 in the load(InputStream). You will have to load your properties using the load(Reader) where you specify the encoding when constructing the reader.

ehsavoie
  • 3,126
  • 1
  • 16
  • 14