【收集整理】request获取参数为中文乱码解法方法
来源:互联网 发布:企业邮箱域名要备案吗 编辑:程序博客网 时间:2024/05/20 14:42
1. 转换字符编码方式
例如: 获取中文信息的用户名文本框(UserName)的值,利用如下代码:
将
<%=request.getParameter("UserName")%>
改为
<%=new String(request.getParameter("UserName").getBytes("iso-8859-1"), "gb18030") %>
得到UserName文本框的中文值。
缺点:每次传值都要进行转换,适用于简单一句传值的页面中.
2. 利用SetCharacterEncoding方法
在所有需要获取参数的命令之前加上:
<%request.setCharacterEncoding("GB18030"); %>
注意:1和2两种方式不能同时使用。
缺点:每个需要传中文值的页面都需要添加此句。
3. 利用JavaBean
在JavaBean中写个字符编码转换类及方法,然后通过此类的方法对需要转换的字符进行转换。
(1) 设置方法如下(在JavaBean中):
public class CharactorEncoding {
public String toString(String str) {
String text="";
if(str != null && !str.equals("")) {
try {
text = new String(str.getBytes("iso8859-1"), "GB18030");
}
catch (Exception e) {
e.printStackTrace();
}
}
return text;
}
}
(2) 传递文件如下(在jsp页面中):
<form action="student.jsp" method="post">
姓名:<input type="text" name="name"/>
<br/>
<input type="submit" value="提交"/> <input type="reset" value="取消"/>
</form>
如果在姓名中添加中文,则需要代码转换。
(3) 调用方法如下(在jsp页面中):
<jsp:useBean id="student" class="zck.Bean" />
<jsp:setProperty property="*" name="student"/><!--直接传递过来的参数不能识别中文 -->
<jsp:useBean id="encoding" class="zck.CharactorEncoding"/>
<%=encoding.toString(student.getName()) %>
而调用
<%=student.getName() %>时,英文可以正常传递过来,而中文则出现乱码。
4. 未完待续....
- 【收集整理】request获取参数为中文乱码解法方法
- json传递中文乱码解决方法以及解决request.getParameter()获取参数为乱码的问题
- JavaEE中request对象的请求参数为中文,获取时出现乱码问题的解决
- request.getParameter() 获取中文乱码
- 再战http request get参数中文乱码
- request.getParameter(“参数名”) 中文乱码解决方法
- request.getParameter(“参数名”) 中文乱码解决方法
- request.getParameter(“参数名”) 中文乱码解决方法
- request.getParameter(“参数名”) 中文乱码解决方法
- request url参数中文乱码问题
- request.getParameter(“参数名”) 中文乱码解决方法
- request.getParameter(“参数名”) 中文乱码解决方法
- request.getParameter(“参数名”) 中文乱码解决方法
- HTML get 方法获取中文参数乱码问题
- 【工作笔记0010】asp.net后台Request.QueryString获取的url中文参数乱码解决方案
- 后台获取ajax传参, request.getParameter(“参数名”) 中文乱码解决方法
- jsp使用request获取post传递来的参数时出现中文乱码
- request.getparameter() 获取中文出现乱码 问题
- 几种排序算法的实现
- Linux各版本root本地密码破解方法
- c and c++ static key word
- Java IO _File 类
- word2007使用
- 【收集整理】request获取参数为中文乱码解法方法
- 硬盘检测工具MHDD修复硬盘无法识别故障
- Building Coder(Revit 二次开发) - 复制 Legend 组件
- zoj 2001 倒置输出
- 常用,易用,好看的第三方控件(.NET)
- ubuntu 安装 vmtools 方法
- videobalance 调整一个视频流的亮度、对比度、饱和度。
- 希尔(Shell)排序的C++实现代码
- 系统无法识别移动硬盘五种可能