REq,RES编码设置

Posted Cool咔咔

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了REq,RES编码设置相关的知识,希望对你有一定的参考价值。


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;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SetCharacterEncodingFilter implements Filter {

private FilterConfig filterConfig;

public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
}

public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest request;
HttpServletResponse response;
try {
request = (HttpServletRequest) req;
response = (HttpServletResponse) res;
} catch (ClassCastException e) {
throw new ServletException("non-HTTP request or response");
}

String encoding = filterConfig.getInitParameter("encoding");
if(encoding==null){
encoding = "UTF-8";
}

//POST:
request.setCharacterEncoding(encoding);
response.setCharacterEncoding(encoding);
response.setContentType("text/html;charset="+encoding);
chain.doFilter(request, response);
}

public void destroy() {

}

}

以上是关于REq,RES编码设置的主要内容,如果未能解决你的问题,请参考以下文章

Express请求处理管线 随笔

NodeJS中的http模块

在嵌入式Jetty中设置默认字符编码和内容类型

如何为编码的 UI 测试设置文化以从资源文件中获取本地化文本?

node express 设置重定向

Node.js在发送标题后无法设置标题引入res.render()后出错