解決方法為:1、找到request和response對象,加入「setCharacterEncoding=UTF-8」即可;2、找到tomcat設定檔server.xml,新增「URIEncoding="utf-8"」即可。
本教學操作環境:windows7系統、javascript1.8.5版、Dell G3電腦。
1.物件request response 物件setCharacterEncoding=UTF-8
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> <% //解决post/get 请求中文乱码的方法 request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>代码段标签</title> </head> <body> <%! int name; int password; %> <% String name=request.getParameter("username"); String password =request.getParameter("pwd"); out.println("hello "+name+" success! "+"<br/>"); out.println("密码泄露 "+password); %> </body>
2. 方法二(比較簡單)
找到tomcat 設定檔server.xml ,加入code:URIEncoding="utf-8"
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" URIEncoding="utf-8" redirectPort="8443" /> <!-- URIEncoding="utf-8" 解决get/post 请求 中文乱码 -->
【推薦學習:javascript高階教學】
以上是javascript中string亂碼怎麼辦的詳細內容。更多資訊請關注PHP中文網其他相關文章!