【Java】【jquery】ajax垃圾问题

时间:2023-03-09 21:10:17
【Java】【jquery】ajax垃圾问题

1、暗示HTML、JSP文件本身使用UTF-8格公式

2、HTML的head加:

<META http-equiv="Content-Type" content="text/html; charset=UTF-8">

3、JSP文件头添加

<%@ page contentType="text/html;charset=utf-8" pageEncoding="UTF-8"%>

4、当中第2、3点也可通过Filter实现:

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {


    HttpServletRequest req = (HttpServletRequest) request;

    if (req.getHeader("X-Requested-With") != null && req.getHeader("X-Requested-With").equalsIgnoreCase("XMLHttpRequest")) {


        request.setCharacterEncoding("utf-8");

    } else {

        request.setCharacterEncoding("gbk");

    }

    chain.doFilter(request, response);

}

5、在你的tomcat或者jboss的server.xml文件里Connector标签下加上:URIEncoding="UTF-8"

这一点是关键、一般中间件默认的编码格式是iso8859-1、iso8859-1能显示中文。可是对中文的支持不是非常好,有些生僻字无法显示。

假设没有这一步的设置就须要採用下面方法转码:new String(request.getParameter("chnlName").getBytes("iso8859-1"),"utf-8")




版权声明:本文博主原创文章。博客,未经同意不得转载。