java中亂碼的解決方案
對於Java,由於預設的編碼方式是UNICODE,所以用中文也易出問題,常見的解決是:
String s2 = new String(s1.getBytes(“ISO-8859-1”),”GBK”);
1、utf8解決JSP中文亂碼問題
推薦教學:《java學習》
#一般說來在每個頁面的開始處,加入:<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% request.setCharacterEncoding("UTF-8"); %>
String msg = request.getParameter("message"); String str=new String(msg.getBytes("ISO-8859-1"),"UTF-8"); out.println(st);
<filter> <filter-name>Set Character Encoding</filter-name> <filter-class>filters.SetCharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>GBK</param-value> </init-param> </filter> <filter-mapping> <filter-name>Set Character Encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
URIEncoding=”GBK”
<connector port="80" maxThreads="150" minSpareThreads="25" maxSpareThreads="75" enableLookups="false" redirectPort="8443" acceptCount="100" debug="0" connectionTimeout="20000" disableUploadTimeout="true" URIEncoding="GBK" />
<%@ page contentType="text/html; charset=GBK"%>
function addFracasReport() { var url="controler?actionId=0_06_03_01&actionFlag=0010"; var urlmsg="&reportId="+fracasReport1.textReportId.value; //故障报告表编号 var xmlHttp=Common.createXMLHttpRequest(); xmlHttp.onreadystatechange = Common.getReadyStateHandler(xmlHttp, eval("turnAnalyPage")); xmlHttp.open("POST",url,true); xmlHttp.setRequestHeader( " Content-Type " , " application/x-www-form-urlencoded); xmlHttp.send(urlmsg); } #p#
public static String UTF_8ToGBK(String str) { try { return new String(str.getBytes("UTF-8"), "GBK"); } catch (Exception ex) { return null; } } public static String UTF8ToGBK(String str) { try { return new String(str.getBytes("UTF-16BE"), "GBK"); } catch (Exception ex) { return null; } } public static String GBK(String str) { try { return new String(str.getBytes("GBK"),"GBK"); } catch (Exception ex) { return null; } } public static String getStr(String str) { try { String temp_p = str; String temp = new String(temp_p.getBytes("ISO8859_1"), "GBK"); temp = sqlStrchop(temp); return temp; } catch (Exception e) { return null; } }
以上是java中亂碼的解決辦法的詳細內容。更多資訊請關注PHP中文網其他相關文章!