SpringMVC中post get亂碼解決

2021-07-25 17:33:49 字數 1213 閱讀 3997

由於中介軟體為tomcat,使用者請求和返回的響應都需要經過tomcat,tomcat預設編碼為iso-8859-1和我們程式的編碼(一般為utf-8)不同,導致post、get請求亂碼。

post亂碼解決方案:

在web.xml中新增編碼過濾器來解決,可以自己編寫過濾器,最常用的方法是使用spring提供的編碼過濾器:

在web.xml中增加如下配置(要注意的是它的位置一定要是第乙個執行的過濾器)

characterencodingfilterfilter-name>

org.springframework.web.filter.characterencodingfilterfilter-class>

encodingparam-name>

utf-8param-value>

init-param>

filter>

characterencodingfilterfilter-name>

/*url-pattern>

該過濾器要做的其實就是強制為所有請求和響應設定編碼格式:

request.setcharacterencoding(「utf-8」);

response.setcharacterencoding(「utf-8」);

get亂碼解決方案

針對get方式的亂碼問題,由於引數是通過url傳遞的,所以上面通過request設定的編碼格式是不起作用的

方法①

修改tomcat目錄 的conf/server.xml檔案,新增uriencoding="utf-8"

只需增加 uriencoding="utf-8" 這一句,然後重啟tomcat即可。

方法②對引數進行重新編碼:

string username new

string(request.getparamter("username").getbytes("iso8859-1"),"utf-8")

iso8859-1是tomcat預設編碼,需要將tomcat編碼後的內容按utf-8編碼

通過以上對post和get的處理,媽媽再也不用擔心我們遇到亂碼了!

安裝驅動使python指令碼訪問postgessql

使用python指令碼訪問postgressql 需要安裝驅動程式 在windows下安裝 psycopg2 2.2.2.win32 py2.6 pg9.0.1 release.exe 就可以了 color blue tar zxf psycopg2 2.2.2.tar.gz cd psycopg2...

vue使用vscode跨域請求post,get

建立axios request.js import axios from axios 引入 axios import baseurl from api baseurl 使用環境變數 模式的方式定義基礎url 建立 axios 例項 const service axios.create export ...

SpringMVC中controller的跳轉

方式一 使用modelandview return new modelandview redirect tolist 這樣可以重定向到tolist這個方法 方式二 返回string return redirect tolist 方式一 自己手動拼接url new modelandview redir...