午夜国产狂喷潮在线观看|国产AⅤ精品一区二区久久|中文字幕AV中文字幕|国产看片高清在线

    解決JSP開發(fā)Web程序中文顯示三種方法
    來源:易賢網(wǎng) 閱讀:1010 次 日期:2015-01-27 14:56:28
    溫馨提示:易賢網(wǎng)小編為您整理了“解決JSP開發(fā)Web程序中文顯示三種方法”,方便廣大網(wǎng)友查閱!

    方法一:最簡單也是用的最多的方法<%@ page language="java" pageEncoding="GBK" %>

    或者<%@ page contenttype="text/html;charset=gbk";>這里可以用gb2312或者gbk,只是gbk比gb2312支持跟多的字符。

    這個方法用于jsp頁面中的中文顯示。

    方法二:使用過濾器

    過濾器使用主要針對表單提交,插入數(shù)據(jù)庫的數(shù)據(jù)都是?號。這也是應(yīng)為tomcat不按request所指定的編碼進(jìn)行編碼,還是自作主張的采用默認(rèn)編碼方式iso-8859-1編碼。

    編寫一個SetCharacterEncodingFilter類。

    importjava.io.IOException; 

    importjavax.servlet.Filter; 

    importjavax.servlet.FilterChain; 

    importjavax.servlet.FilterConfig; 

    importjavax.servlet.ServletException; 

    importjavax.servlet.ServletRequest; 

    importjavax.servlet.ServletResponse; 

    publicclassSetCharacterEncodingFilterimplementsFilter{ 

    protectedStringencoding=null; 

    protectedFilterConfigfilterConfig=null; 

    protectedbooleanignore=true; 

    publicvoidinit(FilterConfigfilterConfig)throwsServletException{ 

    this.filterConfig=filterConfig; 

    this.encoding=filterConfig.getInitParameter("encoding"); 

    Stringvalue=filterConfig.getInitParameter("ignore"); 

    if(value==null) 

    this.ignore=true; 

    elseif(value.equalsIgnoreCase("true")) 

    this.ignore=true; 

    else 

    this.ignore=false; 

    } 

    publicvoiddoFilter( 

    ServletRequestrequest,ServletResponseresponse,FilterChainchain) 

    throwsIOException,ServletException{ 

    //TODO自動生成方法存根 

    if(ignore(request.getCharacterEncoding()==null)){ 

    Stringencoding=selectEncoding(request); 

    if(encoding!=null) 

    request.setCharacterEncoding(encoding); 

    } 

    chain.doFilter(request,response); 

    } 

    publicvoiddestroy(){ 

    //TODO自動生成方法存根 

    this.encoding=null; 

    this.filterConfig=null; 

    } 

    protectedStringselectEncoding(ServletRequestrequest){ 

    return(this.encoding); 

    } 

    }

    然后再web.xml加上

    <!--SetCharacterEncoding--> 

    <filter> 

    <filter-name>SetCharacterEncoding</filter-name> 

    <filter-class>com.struts.common.SetCharacterEncodingFilter</filter-class> 

    <init-param> 

    <param-name>encoding</param-name> 

    <param-value>UTF-8</param-value> 

    </init-param> 

    </filter> 

    <filter-mapping> 

    <filter-name>SetCharacterEncoding</filter-name> 

    <url-pattern>/*</url-pattern> 

    </filter-mapping> 

    <!--SetCharacterEncoding-->

    使用過濾器的好處很多,特別是項(xiàng)目之中。

    而且在使用國際化時就更有用了,只要在頁面指定 <%@ page language="java" pageEncoding="UTF-8" %>,服務(wù)器就會根據(jù)本地Locale來顯示正確的字符集。

    所以我特別推薦使用過濾器。

    方法三:修改tomcat的server.xml文件中URIEncoding

    <Connectordebug="0"acceptCount="100"connectionTimeout="20000"disableUploadTimeout="true" 

    port="80"redirectPort="8443"enableLookups="false"minSpareThreads="25"maxSpareThreads="75" 

    maxThreads="150"maxPostSize="0"URIEncoding="GBK"> 

    </Connector>

    這個方法主要針對從url中獲取字符串的問題。

    在tomcat5.0及以上版本,post和get方法在處理編碼時有所不同。如果你在url中獲取中文就會出現(xiàn)?號。但在tomcat4.1版本沒有問題,因?yàn)閠omcat4.1的post和get方法在處理編碼時是一樣的。

    更多信息請查看IT技術(shù)專欄

    更多信息請查看腳本欄目
    易賢網(wǎng)手機(jī)網(wǎng)站地址:解決JSP開發(fā)Web程序中文顯示三種方法
    由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

    2025國考·省考課程試聽報(bào)名

    • 報(bào)班類型
    • 姓名
    • 手機(jī)號
    • 驗(yàn)證碼
    關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機(jī)站點(diǎn) | 投訴建議
    工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號
    云南網(wǎng)警備案專用圖標(biāo)
    聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
    咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
    云南網(wǎng)警報(bào)警專用圖標(biāo)