request获取中文乱码的问题
生活随笔
收集整理的這篇文章主要介紹了
request获取中文乱码的问题
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
亂碼問題解決:* POST請求亂碼 :request.setCharacterEncoding("utf-8"); * GET請求亂碼解決方案一:修改tomcat/conf/server.xml <Connector port="80" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="utf-8"/>* 必須有修改tomcat服務(wù)器配置文件權(quán)限解決方案二:逆向編解碼username = URLEncoder.encode(username, "ISO8859-1");username = URLDecoder.decode(username, "utf-8");解決方案三:簡寫的方式(推薦使用)username = new String(username.getBytes("ISO-8859-1"),"utf-8");* request獲取中文數(shù)據(jù)亂碼(總結(jié):)* post提交* 設(shè)置request緩沖區(qū)的編碼request.setCharacterEncoding("utf-8"); * get提交* String構(gòu)造方法username = new String(username.getBytes("ISO-8859-1"),"utf-8");
處理中文亂碼
post
? ?setCharacterEncoding? //放在getParameter前才有效
get
? ?new String(str.getBytes(“ISO-8859-1”),”utf-8”)
? ?設(shè)置tomcat Connector URIEncoding=“utf-8”
package cn.itcast.request;import java.io.IOException; import java.util.Arrays; import java.util.Map; import java.util.Set;import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;/*** 獲取請求參數(shù)* @author Administrator**/ public class RegServlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {/*** request獲取中文的亂碼* post請求(經(jīng)常使用)* setCharacterEncoding(String env) 設(shè)置request的緩沖區(qū)的編碼* get請求 * username = new String(username.getBytes("ISO-8859-1"),"UTF-8");*/// 設(shè)置request緩沖區(qū)的編碼(一定要在request.getParameter("username");之前)// request.setCharacterEncoding("UTF-8");// 獲取內(nèi)容,做其他操作// 獲取姓名String username = request.getParameter("username");// 解析get方式亂碼的問題username = new String(username.getBytes("ISO-8859-1"),"UTF-8");}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doGet(request, response);}}?
總結(jié)
以上是生活随笔為你收集整理的request获取中文乱码的问题的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 获取请求参数
- 下一篇: 转发和重定向和request域对象