当前位置:网站首页>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);
边栏推荐
- 初试为锐捷交换机跨设备型号升级版本(以RG-S2952G-E为例)
- [Jianzhi offer] 6-10 questions
- debug和release的区别
- A complete tutorial for getting started with redis: hyperloglog
- mamp下缺少pcntl扩展的解决办法,Fatal error: Call to undefined function pcntl_signal()
- PS style JS webpage graffiti board plug-in
- Excel shortcut keys - always add
- How to choose a securities company? Is it safe to open an account on your mobile phone
- Redis introduction complete tutorial: slow query analysis
- cout/cerr/clog的区别
猜你喜欢
Redis入门完整教程:Redis Shell
[roommate learned to use Bi report data processing in the time of King glory in one game]
Redis getting started complete tutorial: Geo
S32 Design Studio for ARM 2.2 快速入门
Excel shortcut keys - always add
Redis入门完整教程:有序集合详解
A complete tutorial for getting started with redis: getting to know redis for the first time
C语言快速解决反转链表
Tweenmax emoticon button JS special effect
壁仞科技研究院前沿技术文章精选
随机推荐
[OpenGL] note 29 anti aliasing (MSAA)
UML diagram memory skills
SPH中的粒子初始排列问题(两张图解决)
Pagoda 7.9.2 pagoda control panel bypasses mobile phone binding authentication bypasses official authentication
CTF競賽題解之stm32逆向入門
S32 Design Studio for ARM 2.2 快速入门
How to choose a securities company? Is it safe to open an account on your mobile phone
[crawler] XPath for data extraction
VIM editor knowledge summary
Complete tutorial for getting started with redis: bitmaps
Set up a website with a sense of ceremony, and post it to 1/2 of the public network through the intranet
cout/cerr/clog的区别
企业如何跨越数字化鸿沟?尽在云原生2.0
Sword finger offer 65 Add without adding, subtracting, multiplying, dividing
高通WLAN框架学习(30)-- 支持双STA的组件
浅聊一下中间件
【机器学习】手写数字识别
Redis入门完整教程:Redis Shell
[try to hack] wide byte injection
vim编辑器知识总结