I want to say that it is too boring for the diffrent character rule such as ISO-8656-1,UTF-8,GB2312 . I find I always can't use them very facility. I find a article how to solve character trouble. Actually the problem in jsp or servlet is as three aspact:
解決JSP開發Web程序中的中文問題
這段時間經常看到有人問到web開發中怎么中文總是?號。原因其實很簡單,因為大家大多用的是tomcat服務器,而tomcat服務器的默認編碼為 iso-8859-1(西歐字符)。就是因為iso-8859-1(西歐字符)編碼造成了我們經常看到?號。
方法一:最簡單也是用的最多的方法。
<%@ page language="java" pageEncoding="GBK" %>
或者<%@ page contenttype="text/html;charset=gbk";>這里可以用gb2312或者gbk,只是gbk比gb2312支持跟多的字符。
這個方法用于jsp頁面中的中文顯示。
方法二:使用過濾器。
過濾器使用主要針對表單提交,插入數據庫的數據都是?號。這也是應為tomcat不按request所指定的編碼進行編碼,還是自作主張的采用默認編碼方式iso-8859-1編碼。
編寫一個SetCharacterEncodingFilter類。
import java.io.IOException;
import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse;
public class SetCharacterEncodingFilter implements Filter { protected String encoding = null; protected FilterConfig filterConfig = null; protected boolean ignore = true;
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 this.ignore=false; }
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { // TODO 自動生成方法存根 if (ignore || (request.getCharacterEncoding() == null)) { String encoding = selectEncoding(request); if (encoding != null) request.setCharacterEncoding(encoding); } chain.doFilter(request, response); }
public void destroy() { // TODO 自動生成方法存根 this.encoding = null; this.filterConfig = null; }
protected String selectEncoding(ServletRequest request) { return (this.encoding); } } |
然后再web.xml加上
<!-- Set Character Encoding--> <filter> <filter-name>Set Character Encoding</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>Set Character Encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- Set Character Encoding--> |
使用過濾器的好處很多,特別是項目之中。
而且在使用國際化時就更有用了,只要在頁面指定 <%@ page language="java" pageEncoding="UTF-8" %>,服務器就會根據本地Locale來顯示正確的字符集。
所以我特別推薦使用過濾器。
方法三:修改tomcat的server.xml文件中URIEncoding。
<Connector debug="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方法在處理編碼時是一樣的。
posted on 2006-03-14 09:38
Felix 閱讀(96)
評論(0) 編輯 收藏