《常见JSP中文乱码及解决方法.docx
文本预览下载声明
常见JSP中文乱码及解决方法 当我们在学习JSP开发时,中文乱码是个比较常见的问题,其根源是:Web容器默认的字符处理编码是ISO-8859-1。下面我们来详细看看如何去解决这个问题。 JSP开发应用是,中文乱码是个比较常见的问题,其根源是:Web容器默认的字符处理编码是ISO-8859-1。 实例一、JSP页面显示时html ?head title中文乱码——JSP页面显示时/title ?/head ?body center br/ h1木兰辞拟古决绝词柬友/h1 p人生若只如初见,何事秋风悲画扇。/p p等闲变却故人心,却道故人心易变。/p p骊山语罢清宵半,泪雨霖铃终不怨。/p p何如薄幸锦衣郎,比翼连枝当日愿。/p /center ?/body /html 运行结果:解决方法:为其指定中文字符集,html前加%@ page contentType=text/html;charset=gb2312 % 实例二、JSP页面传递中文参数时注册页面:%@ page contentType=text/html;charset=gb2312 % html ?head title中文乱码——JSP页面传递中文参数时/title ?/head ?body h2申请账号:/h2 form action=userMsg.jsp method=POST p邮箱:nbsp;input type=textname=email id=email/p/ p昵称:nbsp;input type=textname=nickname id=nickname/p/ p密码:nbsp;input type=passwordname=password id=password/p/ p性别:nbsp;input type=radioname=sex id=sexvalue=男 / 男? input type=radio name=sexid=sex value=女 / 女p/ textarea ?name=introductionid=introduction rows=5 cols=27一句话介绍自己.../textarea pinput type=submitvalue=提交申请/p /form ?/body /html 个人信息页面:%@ page contentType=text/html;charset=gb2312 % html ?head title中文乱码——JSP页面传递中文参数时 /title ?/head ?body center h2用户信息:/h2 % String email = request.getParameter(email); % % String nickname = request.getParameter(nickname); % % String password = request.getParameter(password); % % String sex = request.getParameter(sex); % % String introduction = request.getParameter(introduction);% p邮箱:nbsp;% out.print(email); %p/ p昵称:nbsp;% out.print(nickname); %p/ p密码:nbsp;% out.print(password); %p/ p性别:nbsp;% out.print(sex); %p/ p个人介绍:%out.print(introduction); %/p /center ?/body /html 运行结果:解决方法:修改个人信息页面如下%@ page contentType=text/html;charset=gb2312 % html ?head title中文乱码——JSP页面传递中文参数时 /title ?/head ?body h2用户信息:/h2 % String email = newString(request.getParameter(email).getBytes(ISO-8859-1), gb2312);% % String nickname = newString(request.getParameter(nickname).getBytes(ISO-8859-1), gb2312);% % String password = newString(request.getParameter(password).getBytes(ISO-8859-1), gb2312);% % String se
显示全部