Character Encoding Filter

Web uygulamalarında Türkçe karakter hatasını çözemiyorsunuz aşağıdaki filter örneğini deneyebilirsiniz. Bu örneğe göre tüm request ve response objelerine character encoding olarak “UTF-8″ set ediliyor

    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;
     
    public class CharacterEncodingFilter implements Filter {
     
     public void init(FilterConfig config) throws ServletException {
     }
     
     public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
      request.setCharacterEncoding("UTF-8");
      response.setCharacterEncoding("UTF-8");
      chain.doFilter(request, response);
     }
     
     public void destroy() {
     }
    }

Filter için web.xml tanımı aşağıdaki gibi olmalıdır.

    <filter>
     <filter-name>Encoding Filter</filter-name>
     <filter-class>CharacterEncodingFilter</filter-class>
    </filter>
    <filter-mapping>
     <filter-name>Encoding Filter</filter-name>
     <url-pattern>/*</url-pattern>
    </filter-mapping>
No comments yet.

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>