当前位置:网站首页>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);
边栏推荐
- 剑指Offer 68 - II. 二叉树的最近公共祖先
- 攻防世界 MISC 高手进阶区 001 normal_png
- PS style JS webpage graffiti board plug-in
- vim编辑器知识总结
- A complete tutorial for getting started with redis: understanding and using APIs
- [machine learning] handwritten digit recognition
- Set up a website with a sense of ceremony, and post it to 1/2 of the public network through the intranet
- A complete tutorial for getting started with redis: redis usage scenarios
- Redis入门完整教程:GEO
- A complete tutorial for getting started with redis: getting to know redis for the first time
猜你喜欢
Qt加法计算器(简单案例)
Redis démarrer le tutoriel complet: Pipeline
The overview and definition of clusters can be seen at a glance
Redis入门完整教程:事务与Lua
[graph theory] topological sorting
Redis introduction complete tutorial: client communication protocol
[sword finger offer] questions 1-5
Redis入门完整教程:有序集合详解
Complete tutorial for getting started with redis: bitmaps
Network namespace
随机推荐
heatmap. JS picture hotspot heat map plug-in
剑指 Offer 68 - I. 二叉搜索树的最近公共祖先
ffmpeg快速剪辑
智力考验看成语猜古诗句微信小程序源码
PS style JS webpage graffiti board plug-in
A complete tutorial for getting started with redis: hyperloglog
C语言快速解决反转链表
mamp下缺少pcntl扩展的解决办法,Fatal error: Call to undefined function pcntl_signal()
Record: how to scroll screenshots of web pages on Microsoft edge in win10 system?
Summary of index operations in mongodb
Async await used in map
【剑指offer】1-5题
Redis入门完整教程:客户端通信协议
9 - class
头文件重复定义问题解决“C1014错误“
字体设计符号组合多功能微信小程序源码
The new version judges the code of PC and mobile terminal, the mobile terminal jumps to the mobile terminal, and the PC jumps to the latest valid code of PC terminal
实战模拟│JWT 登录认证
Redis introduction complete tutorial: Collection details
Detailed explanation of heap sort code