cancel
Showing results for 
Search instead for 
Did you mean: 

[SOLVED] UTF-8 encoding problems

danizmax
Champ in-the-making
Champ in-the-making
Hi,

I have created a action that is harvesting metadata from other instances over webservice. Anyway i got problems with encoding, when reading XML data:


source = new URL(urlStr);      
in = new BufferedReader(new InputStreamReader(source.openStream(), "UTF-8"));
while ((inputLine = in.readLine()) != null) {
   input += inputLine;
}

Any specific character like ščćž, displays like "????". Even logger doesn't display these chars correctly if print them in-compiled like:


logger.error("ščćž");

The strange thing is that Alfresco client handles these chars with no problem.
I tested my code localy as a normal program and it works fine, so is Alfresco configured wrong, or Tomcat?

regards, DAniel
1 REPLY 1

danizmax
Champ in-the-making
Champ in-the-making
I have solved th problem! I had to add a filter because POSTs were not properly encoded.

The filter looks like this:


import java.io.IOException;

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 EncodingFilter implements Filter {

   private String encoding;
   private FilterConfig filterConfig;

/**
* @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
*/
public void init(FilterConfig fc) throws ServletException {
   this.filterConfig = fc;
   this.encoding = filterConfig.getInitParameter("encoding");
}

/**
* @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
*/
public void doFilter(ServletRequest req, ServletResponse resp,
   FilterChain chain) throws IOException, ServletException {
   req.setCharacterEncoding(encoding);
   chain.doFilter(req, resp);
}

/**
* @see javax.servlet.Filter#destroy()
*/
public void destroy() {
}

}


Then you have to add following to TOMCAT_HOME/conf/web.xml:


<!– filter za UTF-8–>
<filter>
    <filter-name>EncodingFilter</filter-name>
        <filter-class>yout.package.EncodingFilter</filter-class>
        <init-param>
          <param-name>encoding</param-name>
          <param-value>UTF-8</param-value>
        </init-param>
    </filter>

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

regards!