为什么jsp使用URL传中文参数会乱码
答案:5 悬赏:50 手机版
解决时间 2021-11-20 08:38
- 提问者网友:容嬷嬷拿针来
- 2021-11-19 16:38
为什么jsp使用URL传中文参数会乱码
最佳答案
- 五星知识达人网友:鸽屿
- 2021-11-19 17:48
在页面中对URL进行编码,最好不要在URL中传递中文参数否则会出现乱码
1.在页面中对你的URL进行编码
使用------encodeURI(你要使用的中文参数值)如:...?username"+encodeURI(“小甜甜")
2.在后台通过解码来接收该中文参数
使用----String name = new String(request.getParameter("username ").getBytes("iso8859-1"),"utf-8")
3.最好不要在URL中传递中文参数
1.在页面中对你的URL进行编码
使用------encodeURI(你要使用的中文参数值)如:...?username"+encodeURI(“小甜甜")
2.在后台通过解码来接收该中文参数
使用----String name = new String(request.getParameter("username ").getBytes("iso8859-1"),"utf-8")
3.最好不要在URL中传递中文参数
全部回答
- 1楼网友:渡鹤影
- 2021-11-19 20:11
传递的时候需要设置编码格式为utf-8,
获取值的时候也需要设置编码格式为utf-8
获取值的时候也需要设置编码格式为utf-8
- 2楼网友:白昼之月
- 2021-11-19 18:52
因为这种提交是get方式提交,下面附上Get方式的乱码问题的解决办法:
可以在每次发生请求之前对URL进行编码:例如:Location.href="/encodeURI"(http://localhost/test/s?name=中文&sex=女);
可以在每次发生请求之前对URL进行编码:例如:Location.href="/encodeURI"(http://localhost/test/s?name=中文&sex=女);
- 3楼网友:时间的尘埃
- 2021-11-19 18:34
超链接提交数据,是以get方式提交的
1
2
3
加入 以下代码是无效的,它是用来解决post方式的乱码问题
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
而解决get方式乱码,最方便的方法是在tomcat文件中的server.xml文件进行配置
1
2
3
配置 connectionTimeout="20000"
redirectPort="8443" URIEncoding="utf-8" />
rsp.setContentType("text/html;charset=UTF-8");设置编码格式
resp.setHeader("Content-type","text/html;charset=UTF-8");
req.setCharacterEncoding("UTF-8");设置请求过程中的编码格式在doget中不起作用
在dopost中设置获取表单数据的编码格式是UTF-8
req.setCharacterEncoding("UTF-8");
resp.setContentType("text/html;charset=UTF-8");
<%
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
%>
1
2
3
加入 以下代码是无效的,它是用来解决post方式的乱码问题
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
而解决get方式乱码,最方便的方法是在tomcat文件中的server.xml文件进行配置
1
2
3
配置
redirectPort="8443" URIEncoding="utf-8" />
rsp.setContentType("text/html;charset=UTF-8");设置编码格式
resp.setHeader("Content-type","text/html;charset=UTF-8");
req.setCharacterEncoding("UTF-8");设置请求过程中的编码格式在doget中不起作用
在dopost中设置获取表单数据的编码格式是UTF-8
req.setCharacterEncoding("UTF-8");
resp.setContentType("text/html;charset=UTF-8");
<%
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
%>
- 4楼网友:风格不统一
- 2021-11-19 18:19
需要在接收的时候转码,因为地址栏的参数会转成iso-8859-1
接收的时候转成utf-8就好了。
参考代码:
String strPtname = request.getParameter("ptname"); //获取参数的值
strPtname = new String(strPtname.getBytes("ISO-8859-1"), "UTF-8");//转码
试试看吧
接收的时候转成utf-8就好了。
参考代码:
String strPtname = request.getParameter("ptname"); //获取参数的值
strPtname = new String(strPtname.getBytes("ISO-8859-1"), "UTF-8");//转码
试试看吧
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯