1. 程式人生 > >SpringMVC繫結引數中的亂碼解決方法(Post與Get)

SpringMVC繫結引數中的亂碼解決方法(Post與Get)

post解決方法:

在web.xml中配置如下引數,由於在javaweb中執行順序是listen——>filter——>servlet,在將請求傳遞給springmvc的前端控制器的時候,filter會先處理,其中下面的處理就是處理請求過來post的引數的亂碼問題。

<filter>     <filter-name>CharacterEncodingFilter</filter-name>     <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>     <init-param>         <param-name>encoding</param-name>         <param-value>utf-8</param-value>     </init-param> </filter> <filter-mapping>     <filter-name>CharacterEncodingFilter</filter-name>     <url-pattern>/*</url-pattern> </filter-mapping>

Servlet中:

 //設定伺服器輸出的編碼為UTF-8     response.setCharacterEncoding("UTF-8");

    //告訴瀏覽器輸出的內容是html,並且以utf-8的編碼來檢視這個內容。     response.setContentType("text/html; charset=utf-8"); ---------------------   

get亂碼解決方法:

è¿éåå¾çæè¿°

修改tomcat中的conf下的server.xml的配置檔案,其中新增URIEncoding="utf-8"(推薦)

<Connector connectionTimeout="20000"             port="8080"             protocol="HTTP/1.1"             redirectPort="8443"            URIEncoding="utf-8"/>  或者

在程式碼中使用轉碼的方式(不推薦)

String userName = new String(request.getParamter("userName").getBytes("ISO8859-1"),"utf-8")