中文乱码
发生中文乱码的情况主要有三种:
1.表单form
(1)post
(2)get
2.超链接
< a href=”http://baidu.com?name=百度”>测试< /a>
3.sendRedirect()发生乱码
response.sendRedirect(“servlet地址?username=”君莫笑”“);
值得注意的是:乱码分为接收方和传送方。服务器(Tomcat)先接受,然后传送给浏览器
通过实例来解决:
简单的用户登录系统来完成。
先建一张表单,有用户名和密码。登陆后会把出现登陆成功字样,而且会显示登录名。
table.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>用户登录</h1>
<form action='/UserLoginOne/LoginClServlet' method='post'>
用户名:<input type='text' name='username'/> <br/>
密 码:<input type='password' name='password'/> <br/>
<input type='submit' value='登录'/> <br/>
</form>
</body>
</html>
密码验证:LoginClServlet.java
public class LoginClServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
request.setCharacterEncoding("utf-8");
//接受用户提交的用户名和密码
String username = request.getParameter("username");
String password = request.getParameter("password");
//检验
if("123".equals(password)){
response.sendRedirect("/UserLoginOne/MainFrame?uname="+username);
}else{
response.sendRedirect("/UserLoginOne/table.html");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
显示页面:MainFrame.java
public class MainFrame extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
request.setCharacterEncoding("utf-8");
String name =request.getParameter("uname");
PrintWriter out = response.getWriter();
out.println("<h1>登录成功Welcome</h1>\n\n\n\n");
out.println("username = "+name);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
试验:
呵呵。。。不争气的东西登录二字就乱码了。。。不要慌
response.setContentType("text/html");
原因主要是没有在这上面申明字符编码,先改为:
response.setContentType("text/html;charset=utf-8");
//把eclipse的编码改为utf-8,默认是GBK
成功了。。
重点:不要把;写成,。本人就因为这个找了一晚上加一早上的错误。。。重新敲了一遍才发现。。*
不过用户名是中文时就乱码了。。。
1.表单——post
先不使用sendRedirect()方法,接收到数据后直接从服务器和浏览器打印
LoginClServlet.java
public class LoginClServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
//接受用户提交的用户名和密码
String username = request.getParameter("username");
String password = request.getParameter("password");
//检验
if("123".equals(password)){
System.out.println("username = "+username);
out.println("username = "+username);
//response.sendRedirect("/UserLoginOne/MainFrame?uname="+username);
}else{
response.sendRedirect("/UserLoginOne/table.html");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
结果
LoginClServlet.java 改为:
public class LoginClServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
request.setCharacterEncoding("utf-8");
//接受用户提交的用户名和密码
String username = request.getParameter("username");
String password = request.getParameter("password");
//检验
if("123".equals(password)){
System.out.println("username = "+username);
out.println("username = "+username);
//response.sendRedirect("/UserLoginOne/MainFrame?uname="+username);
}else{
response.sendRedirect("/UserLoginOne/table.html");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
这样服务器和浏览器都默认以utf-8编码显示,结果:
2.表单——get
把table 改为
直接运行的话肯定还是乱码的
LoginClServlet.java
public class LoginClServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
//接受用户提交的用户名和密码
String username = request.getParameter("username");
String password = request.getParameter("password");
//检验
if("123".equals(password)){
//服务器默认编码是"iso-8859-1",这样就是把编码改为utf-8
String uname = new String(username.getBytes("iso-8859-1"),"utf-8");
System.out.println("username = "+uname);
out.println("username = "+uname);
//response.sendRedirect("/UserLoginOne/MainFrame?uname="+username);
}else{
response.sendRedirect("/UserLoginOne/table.html");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
结果
当然自己干脆写个工具类
MyTools.java
package Tools;
public class MyTools {
public static String GetNewString(String str){
String newstring ="";
try{
newstring = new String(str.getBytes("iso-8859-1"),"utf-8");
}catch(Exception e){
e.printStackTrace();
}
return newstring;
}
}
LoginClServlet.java
import Tools.MyTools;
public class LoginClServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
//接受用户提交的用户名和密码
String username = request.getParameter("username");
String password = request.getParameter("password");
//检验
if("123".equals(password)){
String uname = MyTools.GetNewString(username);
System.out.println("username = "+uname);
out.println("username = "+uname);
//response.sendRedirect("/UserLoginOne/MainFrame?uname="+username);
}else{
response.sendRedirect("/UserLoginOne/table.html");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
3.超链接
和get原理一样,直接用工具类就行了
4.sendRedirect()方法
根据table.html的传送方式看;我的是get.
LoginClServlet.java
public class LoginClServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
//接受用户提交的用户名和密码
String username = request.getParameter("username");
String password = request.getParameter("password");
//检验
if("123".equals(password)){
response.sendRedirect("/UserLoginOne/MainFrame?uname="+username);
}else{
response.sendRedirect("/UserLoginOne/table.html");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
MainFrame.java
public class MainFrame extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println("<h1>登录成功Welcome</h1>\n\n\n\n");
String name =MyTools.GetNewString(request.getParameter("uname"));
System.out.println("username 2= "+name);
out.println("uname 1 = "+name);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
结果:
乱码情况基本就这几种。
*补充:*
下载文件时,提示框可能是乱码:
解决方案:
String temp = java.net.URLEncoder.encode("***(文件名)","utf-8");
response.setHeader("Content-Disposition","attachment;filename"+temp);