Snippets code from my daily experience

June 22, 2007

Using UTF-8 with Apache and Jboss

Filed under: apache_httpd,java,jboss,utf-8 — dafi @ 2:26 am

I’ve found a strange problem with my first real UTF-8 web application…

The urls sent in GET didn’t decoded properly and I was very astonished, my code was written correctly but the problem was present.

BTW the problem wasn’t on my code but in Jboss (in Tomcat embedded instance).

The connector must have the attribute URIEncoding otherwise the webapp never will work.

Due to the fact I use mod_jk (ie AJP13 protocol) this lifesaver attribute must be added in AJP13 connector, too

I love when problems disappear modifing only a configuration file :-)

For sanity check I’ve added UTF-8 support also in Apache Server but I suspect this isn’t really necessary ;-)

In http.conf be sure to have the AddDefaultCharset directive set as shown below

AddDefaultCharset utf-8

About these ads

2 Comments

  1. I had very similar problem with JBoss + myfaces application and after a long fight (I tried page encoding, URIEncoding, filters etc.) I ended up with a conclusion that my application doesn’t use UTF-8 in all of it’s parts – java strings are encoded in cp1250 because I made and compiled it without UTF-8 support.

    Now I think that there is too late to change it (need solution very fast) so it would be full UTF-8 (now UTF-8 is on database side, JSF and in communication by XML-RPC and HTTP with data sources).

    What I did to make it work is a patch to the filter (it’s not very good and clean solution but it works):

    @SuppressWarnings(“unchecked”)
    public void doFilter(ServletRequest request, ServletResponse response,
    FilterChain chain)
    throws IOException, ServletException {

    if (ignore || (request.getCharacterEncoding() == null)) {
    String encoding = selectEncoding(request);
    if (encoding != null) {
    request.setCharacterEncoding(encoding);
    // PATCH START
    Set keyset = (Set) request.getParameterMap().keySet();
    for (String key : keyset) {
    Object value = request.getParameterMap().get(key);
    if (value instanceof String) {
    String newVal = new String (((String)value).getBytes(“cp1250″));
    request.getParameterMap().put(key, newVal);
    }
    }
    // PATCH END
    }
    }
    chain.doFilter(request, response);
    }

    It changes encoding of request parameters to CP1250.
    Probably there is better solution (and the best one is use of UTF-8 in java files encoding and compile project with UTF-8 support).

    Maybe this comment will help somebody. Or someone will put here better quick solution to the problem.

    Comment by Zbyszek — August 28, 2008 @ 1:18 pm

  2. I didn’t test this for a long time and now I think it doesn’t work well but maybe I’m on the good way.
    ParameterMap (org.apache.catalina.util.ParameterMap) can be locked – after request parameters are parsed.
    Now I’m trying with checking if it’s locked.

    Something like:

    ParameterMap x = (ParameterMap) request.getParameterMap();
    if (x.isLocked()) {
    // …
    }

    Comment by Zbyszek — August 28, 2008 @ 1:54 pm


RSS feed for comments on this post.

The Rubric Theme. Create a free website or blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: