jsp中文传值乱码问题

来源:互联网 发布:澳门赌场网络平台 编辑:程序博客网 时间:2024/05/09 15:44
<script type="text/javascript"><!--google_ad_client = "pub-1654710028568330";/* 728x90, 创建于 08-5-19 */google_ad_slot = "5790075209";google_ad_width = 728;google_ad_height = 90;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script><script type="text/javascript"><!--google_ad_client = "pub-1654710028568330";/* 728x15, 创建于 08-5-19 */google_ad_slot = "1395130186";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

 我的问题是:页面编译均为utf-8,表单传值是出现乱码。
网上的方法无法解决,于是,我就新建了一个JSP文件,只让他实现中文的传值功能。发现只要在
request.getParameter("")前面加上一句话request.setCharacterEncoding("UTF-8");就可以了。
这下明白了,因为页面是utf-8编码的所以在取得值之前设置request.setCharacterEncoding("UTF-8")。

同理,如果页面是GB2312编译的话,应该在取得传过来值之前加上request.setCharacterEncoding("GB2312")(个人推测,没有实验,不过理论上不会有错)。

注意:这句话应该放在所有取值语句的前面,否则无效。
例:    request.setCharacterEncoding("UTF-8");
           String id=request.getParameter("id");
           String action=request.getParameter("action");
           String title=request.getParameter("title");
这样有效,
但:    String id=request.getParameter("id");
           request.setCharacterEncoding("UTF-8");         
           String action=request.getParameter("action");
           String title=request.getParameter("title");
就没有用了。

<script type="text/javascript"><!--google_ad_client = "pub-1654710028568330";/* 728x90, 创建于 08-5-19 */google_ad_slot = "5790075209";google_ad_width = 728;google_ad_height = 90;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script><script type="text/javascript"><!--google_ad_client = "pub-1654710028568330";/* 728x15, 创建于 08-5-19 */google_ad_slot = "1395130186";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击