基于tomcat8 編寫字符編碼Filter過(guò)濾器無(wú)效問(wèn)題的解決方法
同事遇到編碼問(wèn)題時(shí)想做一個(gè)解決全站的字符編碼過(guò)濾器,過(guò)濾器類和配置如下:
過(guò)濾器類:
<span style="font-size:12px;">package com.chaoxing.newspaper.web.filter;
import java.io.IOException;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class CharacterEncodingFilter implements Filter {
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
System.out.println("過(guò)濾器執(zhí)行");
final HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
//解決post請(qǐng)求的中文亂碼
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
chain.doFilter((ServletRequest) Proxy.newProxyInstance(this.getClass().getClassLoader(), request.getClass().getInterfaces(), new InvocationHandler(){
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
String methodname = method.getName(); //拿到當(dāng)前的方法
if(methodname.equals("getParameter")){
//執(zhí)行request.getparameter獲取結(jié)果
String value = (String) method.invoke(request, args);
if(value==null){
return null;
}
if(!request.getMethod().equalsIgnoreCase("get")){ //判斷是為get請(qǐng)求
return value;
}
System.out.println(value+"|||||");
//轉(zhuǎn)換編碼返回
value = new String(value.getBytes("UTF-8"),"UTF-8");
System.out.println(value+"=======");
return value;
}
//交給request執(zhí)行請(qǐng)求
return method.invoke(request, args);
}
} ), res);
}
public void init(FilterConfig fConfig) throws ServletException {
System.out.println("過(guò)濾器初始化");
}
@Override
public void destroy() {
System.out.println("過(guò)濾器完成");
}
}</span>
過(guò)濾器配置:
<span style="font-size:12px;"> <!--解決全站亂碼的filter --> <filter> <filter-name>CharacterEncoding</filter-name> <filter-class>com.XXX.web.filter.CharacterEncodingFilter</filter-class> </filter> <filter-mapping> <filter-name>CharacterEncoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping></span>
但是在前端form的提交方式是get的情況下,得到的仍然是亂碼。
剛開始一直糾結(jié)于過(guò)濾器是不是寫錯(cuò)了,編碼集是不是寫錯(cuò)了,但是經(jīng)過(guò)測(cè)試,并沒(méi)有錯(cuò),過(guò)濾器正常運(yùn)行,
最后把value.getByte("iso-8859-1","utf-8") 改為 value.getByte("utf-8","utf-8")后臺(tái)就能得到值,不再是亂碼了。最后百度了下,
說(shuō)tomcat8默認(rèn)的編碼集是utf-8,tomcat8之前默認(rèn)的編碼集是iso-8859-1,恍然大悟......
總結(jié):tomcat7及其版本之前默認(rèn)的編碼集是iso-8859-1,tomcat8默認(rèn)的字符集已經(jīng)是UTF-8,所以不再需要為request.getParameter()的結(jié)果進(jìn)行轉(zhuǎn)碼,不需要編碼編碼過(guò)濾器類了
以上這篇基于tomcat8 編寫字符編碼Filter過(guò)濾器無(wú)效問(wèn)題的解決方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring實(shí)現(xiàn)數(shù)據(jù)庫(kù)讀寫分離詳解
這篇文章主要介紹了Spring?實(shí)現(xiàn)數(shù)據(jù)庫(kù)讀寫分離,大多數(shù)系統(tǒng)都是讀多寫少,為了降低數(shù)據(jù)庫(kù)的壓力,可以對(duì)主庫(kù)創(chuàng)建多個(gè)從庫(kù),從庫(kù)自動(dòng)從主庫(kù)同步數(shù)據(jù),程序中將寫的操作發(fā)送到主庫(kù),將讀的操作發(fā)送到從庫(kù)去執(zhí)行,需要的朋友可以參考下2024-01-01
在Java中解析JSON數(shù)據(jù)代碼示例及說(shuō)明
這篇文章主要介紹了在Java中解析JSON數(shù)據(jù)的相關(guān)資料,文中講解了如何使用Gson和Jackson庫(kù)解析JSON數(shù)據(jù),并展示了如何將日期時(shí)間字符串轉(zhuǎn)換為時(shí)間戳,通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-03-03
SpringBoot整合canal實(shí)現(xiàn)數(shù)據(jù)同步的示例代碼
本文主要介紹了SpringBoot整合canal實(shí)現(xiàn)數(shù)據(jù)同步,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
Springboot自動(dòng)裝配之注入DispatcherServlet的實(shí)現(xiàn)方法
這篇文章主要介紹了Springboot自動(dòng)裝配之注入DispatcherServlet,Springboot向外界提供web服務(wù),底層依賴了springframework中的web模塊來(lái)實(shí)現(xiàn),那么springboot在什么時(shí)機(jī)向容器注入DispatcherServlet這個(gè)核心類的呢?帶著這個(gè)問(wèn)題一起通過(guò)本文學(xué)習(xí)吧2022-05-05
java比較兩個(gè)list是否相同equals的代碼詳解
在本篇文章里小編給大家分享的是關(guān)于java比較兩個(gè)list是否相同equals的代碼詳解,有需要的朋友們可以參考學(xué)習(xí)下。2020-02-02

