当前位置:网站首页>The Chinese output of servlet server and client is garbled
The Chinese output of servlet server and client is garbled
2022-07-04 23:14:00 【Zuo Mingshui】
1, Browser code - spot - Right click - see - code - Confirmation is utf-8.
2,web Server code ISO-8859-1
Situation 1 、 Forms
Form content :
html
meta http-equiv=”content-type” content=”text/html;charset=utf-8”/
body
h1> Our form
form action=”/,,” method=”post”
user name :input type=”text” name=”username”/
password  :input type=”password” name=”username”
input type=”submit” value=” Submit ”
The way 1,get Submit in the request line .
Method 1,
String s=new String(request.getParameter(“username”).getBytes(“iso-8859-1”),”utf-8”);
// Clearly inform with ios-859-1 receive , To utf8 code
Method 2,
Write tool class
1〉 stay src Zhongjian has a toolkit (com.zys.utils)
2〉 Build a class in the package (MyTools)
3〉 The code is as follows :
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〉 Receive and convert
String s=request.getParameter(“username”);
// Import toolkit
MyTools.getNewString(s);
The way 2,post The submission is encapsulated in the request body .
Only in web Server receiver page :
request.setCharacterEncoding(“UTF-8”);
explain : It is best to post Submit , Large amount of data - Security - convenient .
Situation two 、 Hyperlinks
Test connection
The essence is get Submit .
Situation three 、sendRedirect();
Form submission - The way 1get- Method 2 in 4〉 Receive form submissions username And convert .
String s=request.getParameter(“username”);
// Import toolkit
MyTools.getNewString(s);
// Put the received u Data is transferred to the next page .
response.sendRedirect(“/ Next page address ?uname=”+s);
//s It's Chinese , Therefore, it is also used get Submit Chinese .
// Receive on the next page uname And switch .
// The server is already in Chinese , Also let the client ( browser ) Print out Chinese .
response.setContentType(“text/html;charset=utf-8”);
PrintWriter out=response.getWriter();
String s=request.getParameter(“uname”);
// The server type is Chinese
System.out.prinln(“uname=”+MyTools.getNewString(s));
// The client outputs Chinese
out.prinln(“uname=”+MyTools.getNewString(s));
Situation four 、IE6 Up to , For case 2 and case 3 , There are odd numbers in Chinese .
Method :
Situation two
String info=java.net.URLEncoder.encode(“ Study hard .jpg”,”utf-8”);
Test connection
Situation three
response.sendRedirect(“/ Next page address ?uname=”+info);
边栏推荐
- The initial arrangement of particles in SPH (solved by two pictures)
- P2181 对角线和P1030 [NOIP2001 普及组] 求先序排列
- Redis démarrer le tutoriel complet: Pipeline
- 位运算符讲解
- 【剑指offer】1-5题
- A complete tutorial for getting started with redis: Pipeline
- Google Earth engine (GEE) - tasks upgrade enables run all to download all images in task types with one click
- Excel shortcut keys - always add
- Google Earth engine (GEE) - globfire daily fire data set based on mcd64a1
- 微信公众号解决从自定义菜单进入的缓存问题
猜你喜欢
SPH中的粒子初始排列问题(两张图解决)
Redis démarrer le tutoriel complet: Pipeline
MariaDB的Galera集群-双主双活安装设置
vim编辑器知识总结
The initial arrangement of particles in SPH (solved by two pictures)
Docker镜像的缓存特性和Dockerfile
Google Earth engine (GEE) - globfire daily fire data set based on mcd64a1
D3.js+Three. JS data visualization 3D Earth JS special effect
A complete tutorial for getting started with redis: redis shell
Excel 快捷键-随时补充
随机推荐
qt绘制网络拓补图(连接数据库,递归函数,无限绘制,可拖动节点)
【二叉树】节点与其祖先之间的最大差值
Photoshop批量给不同的图片添加不同的编号
位运算符讲解
heatmap. JS picture hotspot heat map plug-in
Sword finger offer 67 Convert a string to an integer
可观测|时序数据降采样在Prometheus实践复盘
A mining of edu certificate station
Set up a website with a sense of ceremony, and post it to 1/2 of the public network through the intranet
debug和release的区别
Notepad++--编辑的技巧
phpcms付费阅读功能支付宝支付
Redis入门完整教程:Redis使用场景
【图论】拓扑排序
Redis入门完整教程:初识Redis
【ODX Studio编辑PDX】-0.3-如何删除/修改Variant变体中继承的(Inherited)元素
Sword finger offer 65 Add without adding, subtracting, multiplying, dividing
Sword finger offer 68 - ii The nearest common ancestor of binary tree
CTF竞赛题解之stm32逆向入门
Basic knowledge of database