总结下JavaWeb应用里正确显示中文需要的设置

1.前台页面需要加的设置:

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

html标签后加上<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>如下

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

2.后台数据库字段(一般是varchar类型)需要设置Character set为utf8.

3.前台如果通过Ajax方式传递中文到Action,js里需要用encodeURI函数将其编码,Action里接到后,要用URLDecoder.decode(str, "utf-8")给它转回来。

   前台以post方式提交的表单编码格式默认为ISO-8859-1的编码格式,可能为中文的话需要转码,如下所示
   String name=new String(request.getParameter("name").getBytes("ISO8859-1"),"UTF-8");

4.后台要将中文通过PrintWriter传到页面上,要对Response进行下面设置

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

目前就是这些,以后有了新情况再补充。

2017年1月12日11:15:58

2017年8月24日11:47:48修改

原文地址:https://www.cnblogs.com/heyang78/p/6277378.html