You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Joseph S <jk...@selectacast.net> on 2006/11/08 00:36:48 UTC

How do I set the default response encoding

My problem: I want to set the default output encoding to UTF-I if the 
browser supports it, for all my jsps and servlets.  I could put <%@ page 
contentType="text/html;charset=utf-8" %> in all my jsps, but I don't 
want to have to put that in all jsps, and I only want to set utf8 if the 
  request header Accept-Charset has utf-8 in it.  Where is this set?

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


RE: How do I set the default response encoding

Posted by Tim Lucia <ti...@yahoo.com>.
You can use a filter:

/* 
 * Copyright (C) 2005 Tim Lucia. 
 * 
 * This software is distributed under the GNU General Public License (GPL).
 */
package tim.lucia;

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;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * J2EE "Filter" to modify responses from the front controller, ensuring
that 
 * the encoding of the response is always set consistently.
 * 
 * @author tim.lucia
 */
public class EncodingFilter implements Filter
{
    private static final String ENCODING = "UTF-8";
    private String encoding = null;
    
    /**
     * Container startup notification
     */
    public void init(FilterConfig arg0) throws ServletException 
    {
        encoding = arg0.getInitParameter("encoding");
        if (null == encoding) {
        	encoding = ENCODING;
        }
    }

    /**
     * Process the container's filter request.
     * @param request - Request object
     * @param response - response object
     * @param chain - next filter in the chain.
     */    
    public void doFilter(ServletRequest request, ServletResponse response,
                         FilterChain chain)
        throws IOException, ServletException 
    {
    	request.setCharacterEncoding(encoding);

    	// move on to the next
    	chain.doFilter(request, response);
    }
}

-----Original Message-----
From: Joseph S [mailto:jks@selectacast.net] 
Sent: Tuesday, November 07, 2006 6:37 PM
To: Tomcat Users List
Subject: How do I set the default response encoding

My problem: I want to set the default output encoding to UTF-I if the 
browser supports it, for all my jsps and servlets.  I could put <%@ page 
contentType="text/html;charset=utf-8" %> in all my jsps, but I don't 
want to have to put that in all jsps, and I only want to set utf8 if the 
  request header Accept-Charset has utf-8 in it.  Where is this set?

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org




---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org