日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

Servelt中文乱码问题处理

發(fā)布時(shí)間:2024/3/12 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Servelt中文乱码问题处理 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1、request與response亂碼

?在Servlet中設(shè)置編碼方式,如下:

request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); 2、輸出jsp頁(yè)面亂碼

?場(chǎng)景:在Login.jsp中,當(dāng)用戶輸入中文時(shí),輸出的頁(yè)面內(nèi)容會(huì)出現(xiàn)亂碼;

?處理:在Servlet中,在獲取用戶輸入信息時(shí),進(jìn)行中文編碼,如下:

String username =new String(request.getParameter("username").getBytes("ISO-8859-1"),"utf-8");

3、tomcat亂碼

?在server.xml中添加URIEncoding="UTF-8",useBodyEncodingForURI="true"編碼方式,如下:

<Connector port="8888" protocol="HTTP/1.1"connectionTimeout="20000"redirectPort="8443" URIEncoding="UTF-8" useBodyEncodingForURI="true"/>

4、使用Servlet過濾器設(shè)置統(tǒng)一編碼

??新建ServletSetEncoding過濾器類實(shí)現(xiàn)Filter接口中的方法,如下:

public class ServletSetEncoding implements Filter {private FilterConfig configer;private String charset = "UTF-8";public void init(FilterConfig config) throws ServletException {System.out.println("*** 過濾器初始化 ***");// 獲取web.xml中Servlet攔截器配置字符編碼配置信息String str = config.getInitParameter("encoding");System.out.println("Encoding:"+str);if (str != null) {charset = str;}}public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException {System.out.println("*** 過濾器執(zhí)行 ***");// 設(shè)置編碼request.setCharacterEncoding(charset);response.setContentType("text/html;charset='" + charset + "'");chain.doFilter(request, response);}public void destroy() {configer = null;System.out.println("*** 過濾器銷毀 ***");}} ? 在web.xml配置過濾器,如下:

<filter><filter-name>ServletSetEncoding</filter-name><filter-class>com.test.filter.ServletSetEncoding</filter-class><init-param><param-name>encoding</param-name><param-value>UTF-8</param-value></init-param> </filter> <filter-mapping><filter-name>ServletSetEncoding</filter-name><url-pattern>/*</url-pattern> </filter-mapping>










總結(jié)

以上是生活随笔為你收集整理的Servelt中文乱码问题处理的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。