Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here's a filter that I wrote to force UTF-8 encoding:</p> <pre><code>public class CharacterEncodingFilter implements Filter { private static final Logger log = Logger.getLogger( CharacterEncodingFilter.class.getName() ); boolean isConnectorConfigured = false; public void init( FilterConfig filterConfig ) throws ServletException {} public void doFilter( ServletRequest request, ServletResponse response, FilterChain chain ) throws IOException, ServletException { request.setCharacterEncoding( "utf-8" ); response.setCharacterEncoding( "utf-8" ); if( ! isConnectorConfigured ) { isConnectorConfigured = true; try { //I need to do all of this with reflection, because I get NoClassDefErrors otherwise. --jsb Field f = request.getClass().getDeclaredField( "request" ); //Tomcat wraps the real request in a facade, need to get it f.setAccessible( true ); Object req = f.get( request ); Object connector = req.getClass().getMethod( "getConnector", new Class[0] ).invoke( req ); //Now get the connector connector.getClass().getMethod( "setUseBodyEncodingForURI", new Class[] {boolean.class} ).invoke( connector, Boolean.TRUE ); } catch( NoSuchFieldException e ) { log.log( Level.WARNING, "Servlet container does not seem to be Tomcat, cannot programatically alter character encoding. Do this in the Server.xml &lt;Connector&gt; attribute instead." ); } catch( Exception e ) { log.log( Level.WARNING, "Could not setUseBodyEncodingForURI to true on connector" ); } } chain.doFilter( request, response ); } public void destroy() {} </code></pre> <p>}</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload