Summary of JSP scrambling

Keywords: MySQL encoding Java Database

;

Specify the encoding format on the jsp page to ensure that it is consistent with the character set of mysql:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 

Step 1: make sure the project code is UTF-8

Step 2: make sure that the mysql database code is utf-8. Take mysql as an example, go to the mysql directory, modify in my.ini file, and restart the mysql service


Step 3: if spring is used, add a filter to web.xml

(if you don't process form directly in jsp page, and you use servlet to process form as I do, then you need to add a filter, mainly because tomcat's web publisher is used.)

<filter>  
        <filter-name>characterEncodingFilter</filter-name>  
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
        <init-param>  
            <param-name>encoding</param-name>  
            <param-value>UTF-8</param-value>  
        </init-param>  
        <init-param>  
            <param-name>forceEncoding</param-name>  
            <param-value>true</param-value>  
        </init-param>  
 </filter>  
 <filter-mapping>  
        <filter-name>characterEncodingFilter</filter-name>  
        <url-pattern>/*</url-pattern>  
 </filter-mapping>

Step 4: set in controller/action

request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8"); 

Note: the coding format of the added data page and the displayed data page should be consistent, otherwise there will be no garbled code added to the database, and the garbled code will appear from the database display.

Solve the problem of database table scrambling
Set all code documents to utf8 format. At the same time, in the linked database file (. java), make the following modifications:

String url="jdbc:mysql://localhost:3306/my?characterEncoding=UTF-8";

In the conversion character encoding file

//Convert the passed string into a byte array and determine its encoding
byte[] b=value.getBytes("ISO-8859-1");
//Construct a new string with byte array and specify its encoding
cn=new String(b,"UTF-8");

tomcat compile receive garbled code
In general, there are three possible problems when disorderly code appears:
1.jsp page is garbled, and there is no unified coding method

<%@ page contentType="text/html; charset=UTF-8"%>

2. The jump between JSP and Servlet (or action) appears Chinese garbled code, and there is no unified coding method for receiving or responding

    request.setCharacterEncoding("UTF-8");
    response.setCharacterEncoding("UTF-8");
    response.setContentType("text/html;charset=UTF-8");

3. The overall coding mode of the project is not unified or the 8080 port coding mode in tomcat's server.xml is not defined

<Connector port="8080"  redirectPort="8443"
connectionTimeout="20000" disableUploadTimeout="true"  URIEncoding="UTF-8"/>

Posted by gigabyt3r on Sun, 03 May 2020 21:19:25 -0700