1. JSP page to write Chinese
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
Ensure that contentType, pageEncoding and browser encoding are all UTF-8, so there will be no garbled characters
2. Form submission in Chinese
<% request.setCharacterEncoding("UTF-8"); %> <%= request.getParameter("message") %>
Before using request to get parameters, set the encoding first.
3. Chinese submitted through the get method
<% request.setCharacterEncoding("UTF-8"); String msg = new String(request.getParameter("message").getBytes("iso-8859-1"), "UTF-8"); %> <%= msg %>
The get mode Chinese is encoded with Iso-8859-1 by default.
The first method: first decode Chinese, and then use UTF-8 encoding.
The second method: configure useBodyEncodingForURI="true" in tomcat's server.xml file ;
Then set request.setCharacterEncoding("UTF-8");
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" useBodyEncodingForURI="true" />