当前位置:网站首页>Servlet服务器端和客户端中文输出乱码问题
Servlet服务器端和客户端中文输出乱码问题
2022-07-04 22:44:00 【左明水】
1,浏览器的编码-点-右键-看-编码-确认是 utf-8。
2,web服务器编码 ISO-8859-1
情况一、表单
表单内容:
html
meta http-equiv=”content-type” content=”text/html;charset=utf-8”/
body
h1>我们的表单
form action=”/,,” method=”post”
用户名:input type=”text” name=”username”/
密码 :input type=”password” name=”username”
input type=”submit” value=”提交”
方式1,get提交在请求行里边。
方法1,
String s=new String(request.getParameter(“username”).getBytes(“iso-8859-1”),”utf-8”);
//明确告知以ios-859-1接收,转为utf8编码
方法2,
写工具类
1〉在src中建个工具包(com.zys.utils)
2〉包内建个类(MyTools)
3〉代码如下:
Public class MyTools{
Public static String getNewString(String str){
String newString=”“;
try{
newString=new String(str.getBytes(“ios-8859-1”),”utf-8”);
}catch(Exception e){
e.printStackTrace();
}
return newString;
}
}
4〉接收并转换
String s=request.getParameter(“username”);
//导入工具包
MyTools.getNewString(s);
方式2,post提交封装在请求体里边。
只在web服务器接收方页面中:
request.setCharacterEncoding(“UTF-8”);
说明:最好用post提交,数据量大-安全-方便。
情况二、超链接
测试连接
本质是get提交。
情况三、sendRedirect();
表单提交-方式1get-方法2中4〉接收表单提交的username并转换。
String s=request.getParameter(“username”);
//导入工具包
MyTools.getNewString(s);
//把接收到的u数据传给下一个页面。
response.sendRedirect(“/下一页面地址?uname=”+s);
//s是中文,故又用get提交中文。
//在下一页面接收uname并进行转换。
//服务器已是中文,也让客户端(浏览器)端打印出中文。
response.setContentType(“text/html;charset=utf-8”);
PrintWriter out=response.getWriter();
String s=request.getParameter(“uname”);
//服务器端打出的是中文
System.out.prinln(“uname=”+MyTools.getNewString(s));
//客户端输出中文
out.prinln(“uname=”+MyTools.getNewString(s));
情况四、IE6及以下版本,针对情况二和情况三中,中文奇数个。
方法:
情况二
String info=java.net.URLEncoder.encode(“好好学.jpg”,”utf-8”);
测试连接
情况三
response.sendRedirect(“/下一页面地址?uname=”+info);
边栏推荐
- On-off and on-off of quality system construction
- Redis introduction complete tutorial: detailed explanation of ordered collection
- vim编辑器知识总结
- [machine learning] handwritten digit recognition
- Redis入门完整教程:Pipeline
- 【ODX Studio编辑PDX】-0.2-如何对比Compare两个PDX/ODX文件
- [OpenGL] note 29 anti aliasing (MSAA)
- Attack and defense world misc advanced grace-50
- Complete tutorial for getting started with redis: bitmaps
- JS 3D explosive fragment image switching JS special effect
猜你喜欢
Redis入门完整教程:Pipeline
Hit the core in the advanced area of misc in the attack and defense world
Advanced area of attack and defense world misc 3-11
JS card style countdown days
Advanced area a of attack and defense world misc Masters_ good_ idea
S32 Design Studio for ARM 2.2 快速入门
A complete tutorial for getting started with redis: redis shell
Three stage operations in the attack and defense drill of the blue team
Google Earth engine (GEE) - tasks upgrade enables run all to download all images in task types with one click
智力考验看成语猜古诗句微信小程序源码
随机推荐
Sword finger offer 68 - ii The nearest common ancestor of binary tree
A complete tutorial for getting started with redis: Pipeline
The small program vant tab component solves the problem of too much text and incomplete display
How to choose a securities company? Is it safe to open an account on your mobile phone
Analysis of environmental encryption technology
Redis入门完整教程:集合详解
Redis introduction complete tutorial: client communication protocol
Erik baleog and Olaf, advanced area of misc in the attack and defense world
LabVIEW中比较两个VI
D3.js+Three. JS data visualization 3D Earth JS special effect
qt绘制网络拓补图(连接数据库,递归函数,无限绘制,可拖动节点)
Redis入门完整教程:Redis Shell
[graph theory] topological sorting
Google Earth engine (GEE) - globfire daily fire data set based on mcd64a1
【机器学习】手写数字识别
Attack and Defense World MISC Advanced Area Erik baleog and Olaf
【剑指Offer】6-10题
Redis getting started complete tutorial: hash description
heatmap. JS picture hotspot heat map plug-in
SPH中的粒子初始排列问题(两张图解决)