コード例 #1
0
 @Override
 public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
     throws IOException, ServletException {
   req.setCharacterEncoding("UTF-8");
   resp.setCharacterEncoding("UTF-8");
   resp.setLocale(Locale.getDefault());
   chain.doFilter(req, resp);
 }
コード例 #2
0
ファイル: Charsets.java プロジェクト: zkoss/zk
  /** Sets up the charset for the request and response based on
   * {@link #getPreferredLocale(HttpSession,ServletRequest)}. After setting up, you shall invoke
   * {@link #cleanup} before exiting.
   *
   * <pre><code> final Object old = setup(request, response, null);
   * try {
   *	  ....
   * } finally {
   *    cleanup(request, old);
   * }
   *
   * <p>It is OK to call this method multiple time, since it is smart
   * enough to ignore redundant calls.
   *
   * <p>{@link CharsetFilter} actually use this method to setup
   * the proper charset and locale. By mapping {@link CharsetFilter} to
   * all servlets, the encoding charset could be prepared correctly.
   * However, if you are writing libraries to be as independent of
   * web.xml as possible, you might choose to invoke this method directly.
   *
   * @param sess the session to look for the preferred locale. Ignored if null.
   * @param charset the response's charset. If null or empty,
   * response.setCharacterEncoding won't be called, i.e., the container's
   * default is used.
   * @return an object that must be passed to {@link #cleanup}
   */
  public static final Object setup(
      HttpSession sess, ServletRequest request, ServletResponse response, String charset) {
    if (hasSetup(request)) // processed before?
    return Objects.UNKNOWN;

    final Locale locale = getPreferredLocale(sess, request);
    response.setLocale(locale);
    if (charset != null && charset.length() > 0) {
      try {
        if (Servlets.isServlet24()) {
          response.setCharacterEncoding(charset);
        } else {
          // don't access 2.4 API: setCharacterEncoding, getContentType
          response.setContentType(";charset=" + charset);
        }
      } catch (Throwable ex) {
        try {
          final String v = response.getCharacterEncoding();
          if (!Objects.equals(v, charset))
            log.warn("Unable to set response's charset: " + charset + " (current=" + v + ')', ex);
        } catch (Throwable t) { // just in case
        }
      }
    }

    if (request.getCharacterEncoding() == null) {
      charset = response.getCharacterEncoding();
      try {
        request.setCharacterEncoding(charset);
      } catch (Throwable ex) {
        final String v = request.getCharacterEncoding();
        if (!Objects.equals(v, charset))
          log.warn(
              "Unable to set request's charset: "
                  + charset
                  + " (current="
                  + v
                  + "): "
                  + Exceptions.getMessage(ex));
      }
    }

    markSetup(request, true);
    return Locales.setThreadLocal(locale);
  }
コード例 #3
0
 /** Sets request encoding. */
 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
     throws IOException, ServletException {
   if (request.getCharacterEncoding() == null) {
     // set response locale according to request locale
     response.setLocale(request.getLocale());
     // if fmt tags specified output encoding already, use it
     if (request instanceof HttpServletRequest) {
       HttpSession ses = ((HttpServletRequest) request).getSession(false);
       if (ses != null) {
         String encoding = (String) ses.getAttribute("javax.servlet.jsp.jstl.fmt.request.charset");
         if (encoding != null) {
           request.setCharacterEncoding(encoding);
         }
       }
     }
   }
   chain.doFilter(request, response);
 }
コード例 #4
0
 public void setLocale(Locale locale) {
   response.setLocale(locale);
 }