jsp---->中文乱码

中文处理的两种方法

1、对所有的数据进行再编码:ISO8859-1 编码,String类中的getBytes("ISO8859-1") ;

public byte[] getBytes("encoding"),再将 byte 数组重新变为字符串

String name = new String(request.getParameter("uname").getBytes("ISO8859-1"))

2、设置统一字符编码:request.setCharacterEncoding("GBK") ;
public void setCharacterEncoding(String env) throws UnsupportedEncodingException
GBK > GB2312 GBK是中文编码、GB2312 是简体中文编码

<%@ page contentType="text/html;charset=gbk"%>
< html>
< body>
< %
// 接收内容
/*
String name = request.getParameter("uname") ;
byte[] b = name.getBytes("ISO8859-1") ;
name = new String(b) ;
*/
// String name = new String(request.getParameter("uname").getBytes("ISO8859-1")) ;


request.setCharacterEncoding("GBK") ;
String name = request.getParameter("uname") ;
%>
< h1>输入内容为:<%=name%></h1>
< /body>
< /html>
(第二种解决不了struts2中get方式提交表单时中文乱码http://blog.csdn.net/ncepustrong/article/details/7926111

posted on   小强斋太  阅读(171)  评论(0编辑  收藏  举报

编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律

导航

< 2012年8月 >
29 30 31 1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31 1
2 3 4 5 6 7 8
点击右上角即可分享
微信分享提示