title: Spring Encoding with CharacterEncodingFilter in web.xml
body: <h3>Encoding on stackoverflow.com</h3>
  <p><code>{|}~€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ¡¢£¤¥¦§¨©ª«¬®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎ     ±ÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö±øùúûüýþŸ▲►▼◄■□▣▤▥▦▧▨▩▪▫◊○●☺☻☼€¢£¥¤♀♂♂♠♤♣♧♥&quot;&quot;♡♦★☆⌂№☎☏♨☜☞♩♪♫♬♭†‡←↑→↓↔↕↖↗↘↙×÷+-Ω√¼½¾⅓⅔⅛⅜⅜⅝%‰¹²³</code></p>
  <h3>Endcoding on my site:</h3>
  <p><code>{|}~???????????????????????????¡¢£¤¥¦§¨©ª«¬®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎ ±ÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö±øùúûüýþ?????¦???????????????¤?¢£¥¤????????&quot;&quot;????¦???????????????????????×÷+-Ov¼½¾??????%?¹²³</code></p>
  <h3>Solution:</h3>
  <pre><code>&lt;#ftl attributes={&quot;content_type&quot;:&quot;text/html&quot;} encoding=&quot;UTF-8&quot;/&gt;
  </code></pre>
  <p>and put this into my HttpsCoookieFilter:</p>
  <pre><code>        request.setCharacterEncoding(&quot;UTF-8&quot;);
          response.setCharacterEncoding(&quot;UTF-8&quot;);
          response.setContentType(&quot;text/html; charset=UTF-8&quot;);
  </code></pre>
  <p>Aparently somehow I (ab)use a HttpServlet instead of a Freemarker to generate HTML content using out.write() so I added the above.</p>
  <p>Here is the servlet source now. Any tips on how to change it are more than welcome:</p>
  <pre><code>public class HttpsCookieFilter implements Filter {
  private static Logger log = Logger.getLogger(HttpsCookieFilter.class);
  
  @Override
  public void destroy() {
  }
  
  @Override
  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
  
      final HttpServletRequest req = (HttpServletRequest) request;
      final HttpServletResponse res = (HttpServletResponse) response;
  
          req.setCharacterEncoding(&quot;UTF-8&quot;);
          res.setCharacterEncoding(&quot;UTF-8&quot;);
          res.setContentType(&quot;text/html; charset=UTF-8&quot;);
  
      final HttpSession session = req.getSession(false);
  
      if (session != null) {
          setCookie(req, res);
      }
      try {
          chain.doFilter(req, res);
      } catch (IllegalStateException e) {
          log.warn(&quot;HttpsCookieFilter redirect problem! &quot;, e);
      }
  }
  
  @Override
  public void init(FilterConfig arg0) throws ServletException {
  }
  
  private void setCookie(HttpServletRequest request, HttpServletResponse response) {
      Cookie cookie = new Cookie(&quot;JSESSIONID&quot;, request.getSession(false).getId());
      cookie.setMaxAge(-1);
      cookie.setPath(getCookiePath(request));
      cookie.setSecure(false);
      response.addCookie(cookie);
  }
  
  private String getCookiePath(HttpServletRequest request) {
      String contextPath = request.getContextPath();
      return contextPath.length() &gt; 0 ? contextPath : &quot;/&quot;;
  }
  }
  </code></pre>
  <p>Now the UTF-8 is working everywhere ;)  Thank you BalusC !!!</p>