首页 > 代码库 > 解决HttpServletResponse输出的中文乱码问题

解决HttpServletResponse输出的中文乱码问题

 

 

主要有这三行代码就可以了。

request.setCharacterEncoding("utf-8");
response.setHeader("Content-type", "text/html;charset=UTF-8");
response.setCharacterEncoding("utf-8");

 

public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		request.setCharacterEncoding("utf-8");
		response.setHeader("Content-type", "text/html;charset=UTF-8");
		response.setCharacterEncoding("utf-8");
		SpdDao dao=new SpdDaoImpl();
		List<Spd> list=dao.getAllSpds();
//		json
		Gson gson=new Gson();		 		
		if(list!=null){
		String json = gson.toJson(new returnJson(true,"OK",list));
		PrintWriter out = response.getWriter();
		out.print(json);
		}else{
			String json = gson.toJson(new returnJson(false,"ERROR"));
			PrintWriter out = response.getWriter();
			out.print(json);
			
			    
		}
		

  

解决HttpServletResponse输出的中文乱码问题