1.浏览器到服务器可能存在的乱码问题(request乱码)
首先知道浏览器使用什么编码----->Servlet以什么样的编码解析,假设浏览器的编码为UTF-8,在传输完成二进制代码后,服务器端需要以该编码解析
(1)浏览器端
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
(2)服务器端解析的三种方式
1)request.setCharacterEncoding("UTF-8");//只对post提交有效
String name = request.getParameter("username");
name="姓名";
但是这种方式只对post提交方式才有效,如果想对get方式也有效,则需添加server.xml的connector标签useBodyEncodingForURI="true" (method was also used for the parameters from the URL. The default value is false)
2)先将解析出来的乱码再转为二进制,然后再指定编码
name = new String(name.getBytes("ISO-8859-1"),"UTF-8");//步骤多,慢
3)修改服务器的连接器配置文件信息,即server.xml下的connector标签,添加ERIEncoding="UTF-8"将默认的iso-8859-1默认编码改为UTF-8
2.服务器到浏览器可能存在的乱码问题(response-->browser乱码)
(1)服务器端(以指定的的编码写入HttpServletResponse中去)
1).response.getOutputStream().write("中华人民共和国是".getBytes("UTF-8"));
2). response.setCharacterEncoding("UTF-8");
response.getWriter().write("今天");//以什么样的码表写入,默认ISO-8859-1编码
(2)浏览器端
1).response.setHeader("Content-type", "text/html;charset=UTF-8");
2).response.getOutputStream().write("<meta http-equiv='content-type' content='text/html;charset=UTF-8' />".getBytes());
response.setHeader("Content-type", "text/html;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8"); //这种方法是上两种步骤的简写
3.jsp字符乱码问题
jsp文件以什么编码保存至硬盘,对应的Servlet又以什么编码打开
<%@ page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>
pageEncoding="UTF-8" //以什么方式保存至硬盘上
contentType="text/html; charset=UTF-8" //以什么方式打开
4.URLEncoder网络传输
/* * 在客户端使用URLEncoder.encode()对中文路径进行编码,在网络进行传输 * %E3%80%8AJavaScript+%E8%AF%AD%E8%A8%80.chm */ File file = new File("d:/api/《JavaScript 语言参考》中文版.chm"); String filename = file.toString().substring(file.toString().lastIndexOf("\\")+1); conn.setRequestProperty("filename", URLEncoder.encode(filename,"utf-8"));
/* * 在服务器端,使用URLDecoder.decode()对传输的数据进行解码 * 得到《JavaScript 语言参考》中文版.chm */ String str = request.getHeader("filename"); String filename = URLDecoder.decode(str, "utf-8");
总之,以什么方式写,以什么方式读
/*
* Servlet乱码问题
* 按什么码表写,按什么码表读
* http://blog.csdn.net/strawberry2013
* 2013-6-05
*/
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ServletDemo2 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//读取从浏览器到服务器的数据
String name = request.getParameter("username");
name = new String(name.getBytes("ISO-8859-1"),"UTF-8");//将乱码的数据重新以指定编码解析
System.out.println(name);
request.setCharacterEncoding("UTF-8");//只对post提交有效
String name1 = request.getParameter("username");
System.out.println(name1);
//从服务器到浏览器的数据传输
// 以指定的的编码写入HttpServletResponse中去
/*1).*/response.getOutputStream().write("中华人民共和国是".getBytes("UTF-8"));
/*2).*/response.setCharacterEncoding("UTF-8");
response.getWriter().write("今天");
// 告诉浏览器以哪种码表打开
/*1).*/response.setHeader("Content-type", "text/html;charset=UTF-8");
/*2).*/response.getOutputStream().write("<meta http-equiv='content-type' content='text/html;charset=UTF-8' />".getBytes());
//通过写入的meta标签设置浏览器识别的码表
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-type", "text/html;charset=UTF-8");
//response.setContentType("text/html;charset=UTF-8"); //这种方法是上两种步骤的简写
//response.getWriter().write("今天"+response.getCharacterEncoding()); //会将字符以默认ISO-8859-1编码
//会以设置的码表写入response中去,如果没有指定码表会以默认的码表写入
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}