1. jsp header <% @ page Language = "Java" contenttype = "text/html; charset = UTF-8" pageencoding = "UTF-8" %>
2. Request. setcharacterencoding ("UTF-8"); character encoding of the request submitted on the JSP page
3. the header information of the JSP page Processing request is the same as that of the first and second
4. transcode the request on the page
<% String err = request.getParameter("err"); byte[] b = err.getBytes("iso-8859-1"); err = new String(b,"UTF-8"); response.setCharacterEncoding("utf-8"); out.println(err); System.out.println(err);%>
5. Pay attention to the location relationship between response. setcontenttype ("text/html; charset = UTF-8"); and printwriter out = response. getwriter,
Remember to put printwriter out = response. getwriter (); behind response. setcontenttype ("text/html; charset = UTF-8"); otherwise, the encoding will be invalid.
The problem is fixed. I don't know why. If I don't transcode it, garbled characters will occur. The reason is unclear! I am so happy now. I feel like this world is wonderful ~
The following is an example of the input username for Ajax verification. Ajax will pass the incorrect Chinese information to the Ajax. jsp page. Ajax. jsp will accept the information and return the Chinese information to Ajax.
Index. jsp
<% @ Page Language = "Java" contenttype = "text/html; charset = UTF-8" pageencoding = "UTF-8" %> <% string Path = request. getcontextpath (); string basepath = request. getscheme () + ": //" + request. getservername () + ":" + request. getserverport () + path + "/"; request. setcharacterencoding ("UTF-8"); %> <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en"> <HTML>
Ajax. jsp processing requests
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><% String err = request.getParameter("err"); byte[] b = err.getBytes("iso-8859-1"); err = new String(b,"UTF-8"); response.setCharacterEncoding("utf-8"); out.println(err); System.out.println(err);%>