Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
  * Licensed to the Apache Software Foundation (ASF) under one or more
  * contributor license agreements.  See the NOTICE file distributed with
  * this work for additional information regarding copyright ownership.
  * The ASF licenses this file to You under the Apache License, Version 2.0
  * (the "License"); you may not use this file except in compliance with
  * the License.  You may obtain a copy of the License at
  *
  *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 package org.apache.catalina.filters;
 
 

Example filter that sets the character encoding to be used in parsing the incoming request, either unconditionally or only if the client did not specify a character encoding. Configuration of this filter is based on the following initialization parameters:

  • encoding - The character encoding to be configured for this request, either conditionally or unconditionally based on the ignore initialization parameter. This parameter is required, so there is no default.
  • ignore - If set to "true", any character encoding specified by the client is ignored, and the value returned by the selectEncoding() method is set. If set to "false, selectEncoding() is called only if the client has not already specified an encoding. By default, this parameter is set to "false".

Although this filter can be used unchanged, it is also easy to subclass it and make the selectEncoding() method more intelligent about what encoding to choose, based on characteristics of the incoming request (such as the values of the Accept-Language and User-Agent headers, or a value stashed in the current user's session.

 
 public class SetCharacterEncodingFilter extends FilterBase {
 
     // ----------------------------------------------------- Instance Variables
 
    
The default character encoding to set for requests that pass through this filter.
 
     private String encoding = null;
     public void setEncoding(String encoding) { this. = encoding; }
     public String getEncoding() { return ; }


    
Should a character encoding specified by the client be ignored?
 
     private boolean ignore = false;
     public void setIgnore(boolean ignore) { this. = ignore; }
     public boolean isIgnore() { return ; }
 
 
     // --------------------------------------------------------- Public Methods
 

    
Select and set (if specified) the character encoding to be used to interpret request parameters for this request.

Parameters:
request The servlet request we are processing
response The servlet response we are creating
chain The filter chain we are processing
Throws:
java.io.IOException if an input/output error occurs
javax.servlet.ServletException if a servlet error occurs
 
     @Override
     public void doFilter(ServletRequest requestServletResponse response,
                          FilterChain chain)
         throws IOExceptionServletException {
 
         // Conditionally select and set the character encoding to be used
         if ( || (request.getCharacterEncoding() == null)) {
             String characterEncoding = selectEncoding(request);
             if (characterEncoding != null) {
                 request.setCharacterEncoding(characterEncoding);
             }
         }
 
        // Pass control on to the next filter
        chain.doFilter(requestresponse);
    }
    // ------------------------------------------------------ Protected Methods

    
Select an appropriate character encoding to be used, based on the characteristics of the current request and/or filter initialization parameters. If no character encoding should be set, return null.

The default implementation unconditionally returns the value configured by the encoding initialization parameter for this filter.

Parameters:
request The servlet request we are processing
    protected String selectEncoding(ServletRequest request) {
        return this.;
    }
New to GrepCode? Check out our FAQ X