国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Java > 正文

解決Java J2EE亂碼問(wèn)題的方法

2019-11-26 14:26:18
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

亂碼是j2ee中一個(gè)比較常見(jiàn)的問(wèn)題。遇到一兩個(gè)問(wèn)題的情況下,可以用new String(request.getParameter(xxx).getBytes("ISO-8859-1"),"UTF-8")來(lái)解決。遇到多的情況下,就最好用過(guò)濾器。
過(guò)濾器只需要注意2個(gè)地方即可――類和web.xml
1.在web.xml上面的發(fā)布如下:

<fileter>   <!-- 類名 -->   <filter-name>SetCharsetEncodingFilter</filter-name>   <!-- 類的路徑 -->   <filter-class>SetCharacter</filter-class>   <init-param>     <param-name>encoding</param-name>     <param-value>utf-8</param-value>   </init-param>   <filter-mapping>     <filter-name>SetCharsetEncodingFilter</filter-name>     <!-- 設(shè)置所有的文件遇到過(guò)濾器都要被攔截 -->     <url-pattern>/*</url-pattern>   </filter-mapping> </fileter> 

2、過(guò)濾類

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 SetCharacter implements Filter {   protected String encoding = null;   protected FilterConfig filterConfig = null;   protected boolean ignore = true;   public void init(FilterConfig arg0) throws ServletException {     this.encoding = arg0.getInitParameter("encoding");     String value = arg0.getInitParameter("imnore");     if (value == null) {       this.ignore = true;     } else if (value.equalsIgnoreCase("true")) {       this.ignore = true;     } else if (value.equalsIgnoreCase("yes")) {       this.ignore = true;     }   }    public void doFilter(ServletRequest arg0, ServletResponse arg1,       FilterChain arg2) throws IOException, ServletException {     if (ignore || (arg0.getCharacterEncoding() == null)) {       String encoding = selectEncoding(arg0);       if (encoding != null)         arg0.setCharacterEncoding(encoding);     }     arg2.doFilter(arg0, arg1);   }    private String selectEncoding(ServletRequest arg0) {     return (this.encoding);   }    public void destroy() {     this.encoding = null;     this.filterConfig = null;   }  }

在web.xml文件中,以下語(yǔ)法用于定義映射:
1、以“/”開(kāi)頭和以“/*”結(jié)尾的是用來(lái)做路徑映射。
2、以前綴“*.”開(kāi)頭的是用來(lái)做擴(kuò)展映射。
3、以“/”是用來(lái)定義default servlet映射。
4、剩下的都是用來(lái)定義詳細(xì)映射。比如:/aa/bb/cc.action

以上就是解決Java J2EE亂碼問(wèn)題的思路,分享給大家,希望大家遇到類似問(wèn)題可以順利解決。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 沧州市| 商南县| 桂林市| 清新县| 安丘市| 定州市| 祥云县| 株洲市| 凤山市| 乐都县| 广平县| 石城县| 冕宁县| 耒阳市| 中牟县| 济源市| 浦北县| 江门市| 汪清县| 平定县| 介休市| 和田市| 晋城| 武功县| 万荣县| 宁波市| 靖宇县| 南靖县| 佛学| 陇南市| 汾西县| 房山区| 客服| 稻城县| 巴中市| 闸北区| 怀柔区| 河池市| 岐山县| 塔河县| 望都县|