stackoverflow.com的编码方式
{|}~€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ¡¢£¤¥¦§¨©ª«¬®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎ ±ÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö±øùúûüýþŸ▲►▼◄■□▣▤▥▦▧▨▩▪▫◊○●☺☻☼€¢£¥¤♀♂♂♠♤♣♧♥""♡♦★☆⌂№☎☏♨☜☞♩♪♫♬♭†‡←↑→↓↔↕↖↗↘↙×÷+-Ω√¼½¾⅓⅔⅛⅜⅜⅝%‰¹²³
我的网站的编码方式:
{|}~???????????????????????????¡¢£¤¥¦§¨©ª«¬®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎ ±ÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö±øùúûüýþ?????¦???????????????¤?¢£¥¤????????""????¦???????????????????????×÷+-Ov¼½¾??????%?¹²³
解决方案:
<#ftl attributes={"content_type":"text/html"} encoding="UTF-8"/>
并将此放入我的HttpsCookie过滤器中:
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");
显然我在使用HttpServlet生成HTML内容时滥用了它,而不是使用Freemarker和out.write()方法。因此,我添加了上述代码。
现在这里是servlet源码,请提供任何有关如何更改它的建议:
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!!!