Added a Filter to get first crack at setting the character encoding of the response and request. Apparently it's best if this is done before any request parameters are ever read or the response InputStream is ever accessed.

This commit is contained in:
travismccauley 2003-10-30 00:30:02 +00:00
parent fb45cb42c3
commit bc918b1c12

View file

@ -0,0 +1,71 @@
package org.thdl.lex;
import java.io.IOException;
import java.util.Enumeration;
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 javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.thdl.users.*;
/**
* Description of the Class
*
* @author travis
* @created October 21, 2003
*/
public class CharEncFilter implements Filter
{
/**
* Description of the Method
*
* @param config Description of the Parameter
* @exception ServletException Description of the Exception
*/
public void init( FilterConfig config ) throws ServletException
{
}
/**
* Description of the Method
*
* @param request Description of the Parameter
* @param response Description of the Parameter
* @param chain Description of the Parameter
* @exception IOException Description of the Exception
* @exception ServletException Description of the Exception
*/
public void doFilter( ServletRequest request, ServletResponse response, FilterChain chain ) throws IOException, ServletException
{
if ( request instanceof HttpServletRequest && response instanceof HttpServletResponse )
{
HttpServletResponse res = (HttpServletResponse) response;
HttpServletRequest req = (HttpServletRequest) request;
res.setContentType( "text/html; charset=UTF-8;" );
req.setCharacterEncoding( "UTF-8" );
chain.doFilter( request, response );
}
else
{
throw new ServletException( "Filter only applicable to HTTP and HTTPS requests" );
}
}
/**
* Description of the Method
*/
public void destroy() { }
//helper methods
}