request请求post、get方式中文乱码处理

第一种方式:获取以后自己转码


String name = request.getParameter("name");
		String age = request.getParameter("age");
		String aaa = request.getParameter("aaa");
		
		name = new String(name.getBytes("UTF-8"),"UTF-8");
		age = new String(age.getBytes("ISO-8859-1"),"UTF-8");
		aaa = new String(aaa.getBytes("ISO-8859-1"),"UTF-8");
		System.out.println("-----------"+name);
		System.out.println("-----------"+age);
		System.out.println("-----------"+aaa);

第二种:在获取之前设置编码

		//给request设置编码为UTF-8
		//必须在调用所有getParameter之前
		request.setCharacterEncoding("UTF-8");
		
		String name = request.getParameter("name");
		String age = request.getParameter("age");
		String aaa = request.getParameter("aaa");

		System.out.println("-----------"+name);
		System.out.println("-----------"+age);
		System.out.println("-----------"+aaa);

Get()方式


1.利用ISO-8859-1转码的方式:

public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//在Get方式请求中request.setCharacterEncoding("UTF-8")不在起作用
		
		//request.setCharacterEncoding("UTF-8");
		
		String name = request.getParameter("name");
		name = new String(name.getBytes("ISO-8859-1"),"UTF-8");
		
		System.out.println(name);
	}

2.

GET参数不在请求正文中,而是在URL中。所以不能使用request的setCharacterEncodng()来设置GET参数的编码。

处理GET参数编码可以有两种方式:第一种是设置<Connector>元素的URIEncoding属性的值为UTF-8。即conf\server.xml中的<Connector>元素的URIEncoding属性。

一旦设置了这个属性,那么对于GET参数就直接是UTF-8编码的了。但是,<Connector>元素来说,对整个Tomcat都是有效的!

3.

Ø  第三种JavaScript对超链接做URL编码

处理这个问题的办法是把GET请求中的参数使用JavaScript做URL编码,URL编码后的内容就不再是中文了,这样IE6也就不会丢失字节了。

<a href="#" οnclick="click1()">ff</a>
<script type="text/javascript">
function click1(){
	var path = encodeURI(encodeURI("servlet/RequestDemo?namea=任亮"));
	location.href = path;
}
</script>




  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
处理中文乱码问题时,需要注意以下几点: 1. 确认请求的数据编码格式。 2. 对于 POST 请求,需要对请求体进行解码。 3. 对于响应数据,需要设置正确的编码格式。 下面是一个示例代码,演示如何使用 Python Flask 框架编写一个过滤器来解决中文乱码问题: ```python from flask import Flask, request app = Flask(__name__) # 过滤器,处理 POST 请求中文乱码问题 @app.before_request def before_request(): content_type = request.headers.get('Content-Type') if content_type and 'charset' in content_type.lower(): # 获取请求体编码格式 encoding = content_type.split('charset=')[-1] if encoding.lower() != 'utf-8': # 如果请求体编码格式不是 utf-8,则进行解码 request_charset = request.charset or encoding request.data = request.data.decode(request_charset).encode('utf-8') # 路由处理函数 @app.route('/', methods=['POST']) def index(): # 处理请求数据 data = request.form['data'] # 处理响应数据 resp = '你发送的数据为:{}'.format(data) return resp.encode('utf-8') if __name__ == '__main__': app.run() ``` 在上面的代码中,我们使用了 Flask 的 `before_request` 过滤器来处理 POST 请求中文乱码问题。在过滤器中,我们首先获取了请求的 `Content-Type` 头部信息,然后判断该信息中是否包含编码格式的信息。如果包含,则获取编码格式并进行解码,最后将解码后的数据转换为 utf-8 编码,以便后续处理。在路由处理函数中,我们首先获取请求数据,然后处理响应数据并设置正确的编码格式返回。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值