记录--JSP-Servlet-Action-页面向后台URL传参带的中文乱码

2 篇文章 0 订阅

测试环境:Win7x64,apache-tomcat-7.0.82(x64),Chrome 65

页面一般设置UTF-8编码以适应国际化,容器Tomcat也应设置UTF-8编码。

----------我是分割线----------记录开始----------

后台Action中:
String encod=request.getCharacterEncoding();
System.out.println("----当前request编码为-----"+encod);
//查询得编码为:UTF-8,而JSP页面编码方式本来就是设置了UTF-8,居然还是得到乱码的参数!

----以下参考各种方法 (大概测试结果)---

解法 1(测试有效):
String newRoleName = request.getParameter("newRoleName");
try {
        System.out.println("--转换编码前:"+newRoleName);
String newName = new String(newRoleName.getBytes("ISO8859_1"),"UTF-8");//不管三七二十一,直接转码,不推荐使用,仅作理论测试
System.out.println("--转换编码后:"+newName);
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}


解法 2(测试无效):
直接在ACTION中设置编码格式再取参数出来:
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");


解法 3(测试无效):
通过配置TOMCAT来解决此问题,具体解决方法如下:
在tomcat的server.xml里,找到Connector连接设置:
<Connector port="8080" maxThreads="150" minSpareThreads="25" maxSpareThreads="75" enableLookups="false" redirectPort="8443" acceptCount="100"  debug="0" connectionTimeout="20000"  disableUploadTimeout="true" useBodyEncodingForURI="true" URIEncoding="utf-8">
其中  useBodyEncodingForURI="true" URIEncoding="utf-8" 就是设置URI编码格式,然后在每个Jsp页面添加如下代码:
<%@ page pageEncoding="utf-8"%>
<%@ page contentType="text/html;charset=utf-8"%>
<%request.setCharacterEncoding("utf-8");%>



解法 4 (测试有效):
jsp页面设置编码格式为 UTF-8。
<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
页面的js中:
var rolename = $("#newRoleName").val();//jquery取文本框的内容
window.location.href="saveRole?newRoleName="+encodeURI(encodeURI(rolename));//编码后发送参数到后台Action

//具体的encodeURI()原理请自行百度/Google


后台Action解码:
//import java.net.URLDecoder;
String newRoleName0 = request.getParameter("newRoleName");
String newRoleName="";
try {
System.out.println("---解码前------"+newRoleName0);
newRoleName = URLDecoder.decode(newRoleName0, "UTF-8");//用URLDecoder的decode()解码方法
System.out.println("---解码后------"+newRoleName);
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}


解法 5 (测试有效):
jsp页面设置编码格式为 UTF-8。
Tomcat的apache-tomcat-7.0.82\conf\ server.xml找到<Connector……标签组,在组标签内添加: URIEncoding="UTF-8"。
<!-- 下面是普通http访问标签设置 -->
 < Connector port=" 80" protocol="HTTP/1.1"    connectionTimeout="20000"     redirectPort="8443"  URIEncoding="UTF-8"/>


-----------附注-------------

对于页面form表单的提交,post方式的表单的action路径可带参数,get方式的表单action路径带的参数会在提交时被过滤掉。下面例子测试提交para参数到名为hello的Servlet/Action中。

<a href="<%=application.getContextPath()%>/hello?para=中文测试(a链接)textChineseWord">a链接-传参到后台</a><br/><br/>

<form id="f1" action="<%=application.getContextPath()%>/hello" method="get">
<input type="hidden" name="para" value="form隐藏button-中文测试para-get"/><!-- get方式的form表单,必须要这样设置提交参数,不能手动附到action的请求路径后面-->
<input type="submit" value="get方式提交"/>
</form><br/>

<form id="f2" action="<%=application.getContextPath()%>/hello?para=form-post中文测试" method="post">
<input type="hidden" name="para2" value="form隐藏button-中文测试para-post"/>
<!-- post方式的form表单域内参数如para2的value,后台取值前要先设置编码格式 request.setCharacterEncoding("utf-8");否则中文乱码 -->
<input type="submit" value="post方式提交"/>
</form>

后台 System.out.println(request.getParameter("para")); //打印参数para的值。


-------附:Servlet/Action 中向页面Write内容:------

response.setContentType("text/html;charset=utf-8"); //设置响应内容编码格式
response.getWriter().write("<h1>内容从Servlet-Action返回</h1><br/>"); //输出到页面的html内容

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值