JAVA常見中文問題的解決方案是怎樣的呢,很多新手對此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
成都創(chuàng)新互聯(lián)公司專注于沙河口企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站,商城網(wǎng)站建設(shè)。沙河口網(wǎng)站建設(shè)公司,為沙河口等地區(qū)提供建站服務(wù)。全流程按需定制網(wǎng)站,專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,成都創(chuàng)新互聯(lián)公司專業(yè)和態(tài)度為您提供的服務(wù)
JAVA常見中文問題的解決方法
以下解決方案是筆者在日常生活中遇到的,希望能對你解決JAVA中文問題有所幫助。
1.在jsp頁面首部加上<%@page contenttype="”text/html;charset=GB2312“%">
在servlet中使用httpServlerResponse.setContentTpye(“text/html; charset=GB2312”);可以避免一些中文問題
2.使用JDBC連接MySQL數(shù)據(jù)庫時(shí),連接字符串寫成如下形式可以避免一些中文問題:
jdbc://mysql://hostname:port/DBname?user=username&password=pwd&
useUnicode=true&characterEncoding= iso-8859-1
如果是以數(shù)據(jù)源的方式連接數(shù)據(jù)庫在配置文件中使用:
url
jdbc:mysql://hostname:port/DBname? &useUnicode=true&characterEncoding=iso-8859-1
注意要使用&替換&符號,否則XML文件在解析的時(shí)候會(huì)出錯(cuò)。
3.從數(shù)據(jù)庫讀出的數(shù)據(jù)有可能是亂碼,遇到這種問題可以用如下方法解決:
String desc = rs.getString(“desc”);
desc = new String(desc.getBytes(“ISO-8859-1”),”GB2312”);
4.某個(gè)頁面提交中文內(nèi)容給Servlet,Servlet要對提交的內(nèi)容進(jìn)行轉(zhuǎn)碼工作才能正確接收數(shù)據(jù),
通常我們是在servlet中增加以下代碼來解決問題。
httpServlerRequest.setCharacterEncoding(“GB2312”);
5. 在struts中,對資源文件進(jìn)行轉(zhuǎn)碼,使用JDK字帶的轉(zhuǎn)碼工具:
>native2ascii -encoding BG2312 Myresource.properties Myresource_zh.properties
6.在struts中擴(kuò)展org.apache.struts.action.RequestProcessor類,并覆寫其中的processPreprocess()方法:
package com.mypro.action;
public class MyProRequestProcessor extends RequestProcessor
{
protected boolean processPreprocess (HttpServletRequest request,
HttpServletResponse response)
{
try
{
request.setCharacterEncoding(“GB2312”);
//other code
}
catch(Exception e){}
return true;
}
}
寫完上面代碼別忘了修改struts-config.xml:
7. 用filter實(shí)現(xiàn)(推薦)
package com.kefeiannan;
import java.io.IOException;
import javax.servlet.*;
public class SetCharacterEncodingFilter implements Filter
{
protected String encoding = null;
protected FilterConfig filterConfig = null;
protected boolean ignore = true;
public void destroy() {
this.encoding = null;
this.filterConfig = null;
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException {
if (ignore || (request.getCharacterEncoding() == null)) {
String encoding = selectEncoding(request);
if (encoding != null)
request.setCharacterEncoding(encoding);
}
chain.doFilter(request, response);
}
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
String value = filterConfig.getInitParameter("ignore");
if (value == null)
this.ignore = true;
else if (value.equalsIgnoreCase("true"))
this.ignore = true;
else if (value.equalsIgnoreCase("yes"))
this.ignore = true;
else
this.ignore = false;
}
protected String selectEncoding(ServletRequest request) {
return (this.encoding);
}
}
配置你站點(diǎn)下的web.xml,在后面加上
Set Character Encoding
com.kefeiannan.SetCharacterEncodingFilter
encoding
UTF-8
Set Character Encoding
/*
看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)的支持。
網(wǎng)站標(biāo)題:JAVA常見中文問題的解決方案是怎樣的呢
URL分享:http://aaarwkj.com/article24/igcjje.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司、自適應(yīng)網(wǎng)站、手機(jī)網(wǎng)站建設(shè)、網(wǎng)站收錄、網(wǎng)站策劃、動(dòng)態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)