请求乱码
客户端发送请求常用方式是GET、POST
GET请求
- 使用代码转换的方式
- 修改tomcat配置文件server.xml的方式
代码转换
url
http://localhost:8080/HelloServlet/h5?username=王学武
servlet类
1 public class HelloServlet5 extends HttpServlet { 2 3 @Override 4 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 5 String username = req.getParameter("username"); 6 //get请求过来的数据,在url地址栏上就已经经过编码了,所以我们取到的就是乱码 7 System.out.println("username===" + username); 8 9 //tomcat收到了这批数据,getParameter 默认使用ISO-8859-1去解码 10 //先让文字回到ISO-8859-1对应的字节数组 , 然后再按utf-8组拼字符串 11 username = new String(username.getBytes("ISO-8859-1"), "UTF-8"); 12 System.out.println("username===" + username); 13 } 14 15 }
console输出
username===çå¦æ¦ username===王学武
修改tomcat配置文件
直接在tomcat里面做配置,在tomcat里面做设置处理 conf/server.xml 加上URIEncoding="utf-8",以后get请求过来的数据永远都是用UTF-8编码
<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" URIEncoding="utf-8"/>
servlet类
1 public class HelloServlet5 extends HttpServlet { 2 @Override 3 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 4 String username = req.getParameter("username"); 5 System.out.println("username===" + username); 6 } 7 }
console输出
username===王学武
POST请求
设置请求体里面的文字编码
request.setCharacterEncoding("UTF-8");
servlet类
1 public class HelloServlet5 extends HttpServlet { 2 3 @Override 4 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 5 req.setCharacterEncoding("utf-8"); 6 String name = req.getParameter("username"); 7 System.out.println("设置请求体编码后:name==="+name); 8 } 9 10 }
console输出
设置请求体编码后:name===王五
如果在获取参数后设置req.setCharacterEncoding("utf-8");就不会起作用
1 public class HelloServlet5 extends HttpServlet { 2 3 @Override 4 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 5 String name = req.getParameter("username"); 6 System.out.println("设置请求体编码前:name==="+name); 7 System.out.println("-------------------------"); 8 9 req.setCharacterEncoding("utf-8"); 10 name = req.getParameter("username"); 11 System.out.println("设置请求体编码后:name==="+name); 12 } 13 14 }
console输出
设置请求体编码前:name===çäº ------------------------- 设置请求体编码后:name===çäº
对GET方式不起作用
url
http://localhost:8080/HelloServlet/h5?name=王学武
servlet类
1 public class HelloServlet5 extends HttpServlet { 2 3 @Override 4 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 5 req.setCharacterEncoding("utf-8"); 6 String name = req.getParameter("name"); 7 System.out.println("name==="+name); 8 } 9 10 }
console输出
name===çå¦æ¦
响应乱码
响应输出有两种方式
- 字符流
- 字节流
1 public class HelloServlet5 extends HttpServlet { 2 3 @Override 4 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 5 //以字符流的方式写数据 6 // resp.getWriter().write("<h3>response...</h3><br/>"); 7 // resp.getWriter().write("<h3>response...</h3>"); 8 // resp.getWriter().flush(); 9 //以字节流的方式写数据 10 resp.getOutputStream().write("response...".getBytes()); 11 } 12 13 }
注:getWriter(),outPutStream在进行文件写入操作只能用一个,否则会抛出java.lang.IllegalStateException: getWriter() has already been called for this response
字符流输出乱码
1 public class HelloServlet5 extends HttpServlet { 2 3 @Override 4 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 5 //以字符流的方式写数据 6 PrintWriter out = resp.getWriter(); 7 //out.write("<h3>响应输出数据</h3><br/>"); 8 9 //1. 指定输出到客户端的时候,这些文字使用UTF-8编码 10 resp.setCharacterEncoding("UTF-8"); 11 //2. 直接规定浏览器看这份数据的时候,使用什么编码来看。 12 resp.setHeader("Content-Type", "text/html; charset=UTF-8"); 13 14 out.write("<h3>响应输出数据</h3>"); 15 } 16 17 }
原因:必须在resp.getWriter()之前设置字符编码操作
1 public class HelloServlet5 extends HttpServlet { 2 3 @Override 4 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 5 6 //1. 指定输出到客户端的时候,这些文字使用UTF-8编码 7 resp.setCharacterEncoding("UTF-8"); 8 //2. 直接规定浏览器看这份数据的时候,使用什么编码来看。 9 resp.setHeader("Content-Type", "text/html; charset=UTF-8"); 10 11 //以字符流的方式写数据 12 PrintWriter out = resp.getWriter(); 13 out.write("<h3>响应输出数据</h3>"); 14 } 15 16 }
修改servlet类的代码
1 public class HelloServlet5 extends HttpServlet { 2 3 @Override 4 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 5 6 //1. 指定输出到客户端的时候,这些文字使用UTF-8编码 7 resp.setCharacterEncoding("UTF-8"); 8 //2. 直接规定浏览器看这份数据的时候,使用什么编码来看。 9 resp.setHeader("Content-Type", "text/html; charset=UTF-8"); 10 11 //以字符流的方式写数据 12 PrintWriter out = resp.getWriter(); 13 out.write("<h3>响应输出数据</h3>"); 14 } 15 16 }
字节流输出
1 public class HelloServlet5 extends HttpServlet { 2 @Override 3 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 4 //1. 指定浏览器看这份数据使用的码表 5 resp.setHeader("Content-Type", "text/html;charset=UTF-8"); 6 7 //2. 指定输出的中文用的码表 8 resp.getOutputStream().write("响应输出数据".getBytes("UTF-8")); 9 } 10 }
其实,无论是字节流输出还是字符流输出,通用写法:
- response.setContentType("text/html;charset=UTF-8")
- 写输出数据即可