解决请求中文乱码问题

将ISO-8859-1转换为UTF-8

Servlet中请求与响应都需要设置UTF-8字符集

处理Post请求中文乱码

方法一:    //setCharacterEncoding()用于将请求体中字符集转换成utf-8
        request.setCharacterEncoding("UTF-8");

方法二://        String utf8Ename=new String(ename.getBytes("iso-8859-1"),"utf-8");
//        String utf8Address=new String(address.getBytes("iso-8859-1"),"utf-8");

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		//setCharacterEncoding()用于将请求体中字符集转换成utf-8
		request.setCharacterEncoding("UTF-8");
		
		String ename=request.getParameter("ename");
		String address=request.getParameter("address");
//		String utf8Ename=new String(ename.getBytes("iso-8859-1"),"utf-8");
//		String utf8Address=new String(address.getBytes("iso-8859-1"),"utf-8");
		System.out.println(ename+":"+address);
	}

处理Get请求中文乱码

修改tomcat配置文件

解决请求中文乱码问题_第1张图片

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String ename=request.getParameter("ename");
		String address=request.getParameter("address");
		String utf8Ename=new String(ename.getBytes("iso-8859-1"),"utf-8");
		String utf8Address=new String(address.getBytes("iso-8859-1"),"utf-8");
		System.out.println( utf8Ename+":"+ utf8Address);
		response.setContentType("text/html;charset=utf-8");
		response.getWriter().println(utf8Ename+":"+utf8Address);
	}

 

 

你可能感兴趣的:(java学习,乱码,servlet,java)