Problem description: When we output Chinese to the page (html/jsp) or read the Chinese on the page, garbled characters appear.
The main reason for this problem is because: the default encoding format of the browser and the compiler is different.
Just let the browser and the compiler use the same rules for encoding and decoding.
Note: Not all encoding rules support Chinese, the encoding "UTF-8" is used here.
Code demo:
package ziyang; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class CeShi extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { / / Solve the garbled text of the read page request.setCharacterEncoding("UTF-8"); / / Solve the Chinese garbled input to the page response.setContentType("text/html;charset=UTF-8"); //Read an input element in the page, its name="user" System.out.println(request.getParameter("user")); response.getWriter().write("Momada"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }