Java处理中文乱码问题

package servlet;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException; @WebFilter(filterName = "FilterCoding",urlPatterns = "/*")
public class FilterCoding implements Filter {
public void destroy() {
} public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException { HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) resp; // 解决post方式提交中文乱码问题,
// 在tomcat8及以上版本,get方式提交不存在中文乱码问题
if ("post".equalsIgnoreCase(request.getMethod())){
request.setCharacterEncoding("utf-8");
} // 解决响应中文乱码问题
response.setContentType("text/html;charset=utf-8"); // 放行请求与响应
chain.doFilter(request, response);
} public void init(FilterConfig config) throws ServletException { } }

  

上一篇:关于transient和static的序列化和反序列化


下一篇:JavaScript之循环