當前位置:文書都 >

事務文書 >工作總結 >

總結解決JSP開發Web程序中文顯示三種方法

總結解決JSP開發Web程序中文顯示三種方法

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

總結解決JSP開發Web程序中文顯示三種方法

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

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

方法二:使用過濾器

過濾器使用主要針對表單提交,插入數據庫的數據都是?號。這也是應為tomcat不按request所指定的編碼進行編碼,還是自作主張的`採用默認編碼方式iso-8859-1編碼。

編寫一個SetCharacterEncodingFilter類。

ception;

er;

erChain;

erConfig;

letException;

letRequest;

letResponse;

publicclassSetCharacterEncodingFilterimplementsFilter{

protectedStringencoding=null;

protectedFilterConfigfilterConfig=null;

protectedbooleanignore=true;

publicvoidinit(FilterConfigfilterConfig)throwsServletException{

erConfig=filterConfig;

ding=nitParameter("encoding");

Stringvalue=nitParameter("ignore");

if(value==null)

re=true;

elseif(lsIgnoreCase("true"))

re=true;

else

re=false;

}

publicvoiddoFilter(

ServletRequestrequest,ServletResponseresponse,FilterChainchain)

throwsIOException,ServletException{

//TODO自動生成方法存根

if(ignore (haracterEncoding()==null)){

Stringencoding=selectEncoding(request);

if(encoding!=null)

haracterEncoding(encoding);

}

lter(request,response);

}

publicvoiddestroy(){

//TODO自動生成方法存根

ding=null;

erConfig=null;

}

protectedStringselectEncoding(ServletRequestrequest){

return(ding);

}

}

然後再加上

<!--SetCharacterEncoding-->

<filter>

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

<filter-class>haracterEncodingFilter</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-->

使用過濾器的好處很多,特別是項目之中。

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

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

方法三:修改tomcat的文件中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中獲取中文就會出現?號。但在tomcat4.1版本沒有問題,因為tomcat4.1的post和get方法在處理編碼時是一樣的。

標籤: 中文 web jsp
  • 文章版權屬於文章作者所有,轉載請註明 https://wenshudu.com/shiwuwenshu/gongzuozongjie/ry077d.html
專題