JAVA常見中文問題的解決方案是怎樣的呢-創(chuàng)新互聯(lián)

JAVA常見中文問題的解決方案是怎樣的呢,很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

創(chuàng)新互聯(lián)專注于成都網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站、網(wǎng)頁設(shè)計(jì)、網(wǎng)站制作、網(wǎng)站開發(fā)。公司秉持“客戶至上,用心服務(wù)”的宗旨,從客戶的利益和觀點(diǎn)出發(fā),讓客戶在網(wǎng)絡(luò)營銷中找到自己的駐足之地。尊重和關(guān)懷每一位客戶,用嚴(yán)謹(jǐn)?shù)膽B(tài)度對(duì)待客戶,用專業(yè)的服務(wù)創(chuàng)造價(jià)值,成為客戶值得信賴的朋友,為客戶解除后顧之憂。

JAVA常見中文問題的解決方法

以下解決方案是筆者在日常生活中遇到的,希望能對(duì)你解決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

注意要使用&替換&符號(hào),否則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要對(duì)提交的內(nèi)容進(jìn)行轉(zhuǎn)碼工作才能正確接收數(shù)據(jù),

通常我們是在servlet中增加以下代碼來解決問題。

httpServlerRequest.setCharacterEncoding(“GB2312”);

5. 在struts中,對(duì)資源文件進(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)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)-成都網(wǎng)站建設(shè)公司行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)的支持。

名稱欄目:JAVA常見中文問題的解決方案是怎樣的呢-創(chuàng)新互聯(lián)
網(wǎng)頁鏈接:http://www.muchs.cn/article22/ihecc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站設(shè)計(jì)、ChatGPT微信小程序、App設(shè)計(jì)、面包屑導(dǎo)航、Google

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名