在web.xml中使用CharacterEncodingFilter进行Spring编码 [英] Spring Encoding with CharacterEncodingFilter in web.xml

查看:323
本文介绍了在web.xml中使用CharacterEncodingFilter进行Spring编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

{|}〜€,ƒ...†‡‰ŠœžŸ¡¢¤£¥|§¨©ª«¬®¯°±²³'μ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍαÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö±øùúûüýþŸ▲►▼◄■□▣▤▥▦ ▧▨▩▪▫◊○●☺☻☼€¢£¥¤♀♂♂♠♤♣♧♥♡♦★☆⌂№☎☏♨☜☞♩♪♫♬♭†‡←↑→↓↔↕ ↖↗↘↙×÷+-Ω√¼½¾⅓⅔⅛⅜⅜⅝%‰¹²³

{|}〜 ¨©ª«¬®¯°±²³'μ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍαÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö±øùúûüýþ??????| ???????????????¢¤ £¥¤????? ?????÷+-Ov¼½¾??? <%>

{|}~???????????????????????????¡¢£¤¥¦§¨©ª«¬®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎ ±ÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö±øùúûüýþ?????¦???????????????¤?¢£¥¤????????""????¦???????????????????????×÷+-Ov¼½¾??????%?¹²³

<#ftl attributes={"content_type":"text/html"} encoding="UTF-8"/>

并将其放入我的HttpsCoookieFilter:

and put this into my HttpsCoookieFilter:

        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=UTF-8");

Aparently不知何故我(ab)使用HttpServlet而不是Freemarker来生成HTML内容使用out.write ()所以我添加了上面的。

Aparently somehow I (ab)use a HttpServlet instead of a Freemarker to generate HTML content using out.write() so I added the above.

现在是servlet源。任何关于如何改变它的提示是受欢迎的:

Here is the servlet source now. Any tips on how to change it are more than welcome:

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("UTF-8");
        res.setCharacterEncoding("UTF-8");
        res.setContentType("text/html; charset=UTF-8");

    final HttpSession session = req.getSession(false);

    if (session != null) {
        setCookie(req, res);
    }
    try {
        chain.doFilter(req, res);
    } catch (IllegalStateException e) {
        log.warn("HttpsCookieFilter redirect problem! ", e);
    }
}

@Override
public void init(FilterConfig arg0) throws ServletException {
}

private void setCookie(HttpServletRequest request, HttpServletResponse response) {
    Cookie cookie = new Cookie("JSESSIONID", 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() > 0 ? contextPath : "/";
}
}

现在UTF-8无处不在你BalusC!

Now the UTF-8 is working everywhere ;) Thank you BalusC !!!

推荐答案

当字符到字节转换器/写入器本身知道字符集时,通常使用问号实际上 编码在字符集必须解码的字符集的中。如果解码字符集不支持原始编码中的特定字符,那么它将转换为问号。

Question marks are typically used when the character-to-byte converter/writer is by itself aware of the charset the characters are actually encoded in and of the charset the characters have to be decoded to. If the decoding charset doesn't support the particular character in the original encoding, then it's converted to a question mark.

在具有数据库后端的平均web应用程序中,有两个地方可能发生这种情况:

In the average webapplication with a database backend, there are two places where this can happen:


  1. 当用户提交的数据即将在DB中插入/更新时。

  2. 当HTTP响应正文即将写入并发送到客户端时。

在这两种情况下,只使用TCP / IP网络,只能理解字节,服务器和客户端通常都知道字符集使用在两边。在其他所有情况下,您都会看到 Mojibake

In both cases a TCP/IP network is been used which only understand bytes and both the server and the client are usually aware of the charset used on the both sides. In all other cases you would have seen Mojibake instead.

要涵盖第一种情况,您需要确保数据库和表已配置为使用UTF-8。通常在 CREATE 期间指定。这里是一个MySQL方言的例子。

To cover the first case, you need to ensure that the DB and the table is been configured to use UTF-8. You usually specify that during CREATE. Here's an example in MySQL dialect.

CREATE DATABASE db_name CHARACTER SET utf8;
CREATE TABLE tbl_name (...) CHARACTER SET utf8;

对于某些JDBC驱动程序,例如MySQL,您还需要 指示驱动程序本身使用UTF-8。

With some JDBC drivers, such as the MySQL one, you also need to instruct the driver itself to use UTF-8.

jdbc:mysql://localhost:3306/db_name?useUnicode=yes&characterEncoding=UTF-8

要覆盖第二种情况下,你需要以确保响应写入程序已被指示使用UTF-8将字符解码为字节。当使用JSP作为视图时,只需在每个 JSP页面(也包括)中添加以下内容就足够了(它不仅设置响应编码,还隐式设置正确的响应头) 。

To cover the second case, you need to ensure that the response writer is been instructed to use UTF-8 to decode the characters to bytes. When using JSPs as view, just adding the following to top of every JSP page (also the includes) ought to be sufficient (it not only sets the response encoding, but also implicitly sets the right response header).

<%@ page pageEncoding="UTF-8" %>



另请参阅:



查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆