Use the get method to pass parameters and report the 400 status code solution

Preface: I checked many methods on the Internet, such as adding attribute codes to the server.xml file, and adding codes to the end of the catalina.properties file. After testing, it was still useless (tears~~~), well, let’s go straight to the topic.

Error reason

 When we jump to the webpage, the browser reports 400, usually because the parameter type or name passed by the front end and the parameters received by the background

The attribute type or name of the entity class does not match, that is, the foreground and background match errors

Testing process

 

Here we include Chinese characters in the parameter CLASS to be passed in the servlet01 file

 Received normally in servlet02 file

It is found that when the browser runs and clicks on the hyperlink, an error is reported

 Modify the value of CLASS in servlet01 to 3212

 Jump successfully!

Test summary: We found that different parameters will produce different access results when passed.

After querying the data, I found that Tomcat7 and above versions strictly follow the RFC 3986 specification for access analysis. The RFC 3986 specification defines that only English letters (a-zA-Z), numbers (0-9), -_.~4 special characters and all reserved characters are allowed in URLs (RFC3986 specifies the following characters as reserved characters: ! * ' ( ) ; : @ & = + $ , / ? # [ ]), and my web server (tomcat) It is version 9.0, so an error is reported when passing Chinese characters.

Solution


1. Replace Tomcat with a lower version to avoid this problem (not recommended)

2. Remove special characters in the URL (not recommended)

3. We define the encoding for the special characters on the URL as shown in the figure:

 

Solved successfully! 

--------------------------------------------------------------------------------------------------------------------------------

2022_10_31_supplement

Solution

4. Add the attribute useBodyEncodingForURI="true" under the Connector node of the server.xml (the encoding of the currently uploaded page will be used as URL encoding. Note: you must set request.setCharacterEncoding("..."); that is, the decoding method of the URL parameter. The decoding method must be consistent with the encoding method format set on the page, otherwise it will be garbled)

---------------------------------------------------------------------------------------------------------------------------------

Note: The article is the author's notes, please leave a comment if you have any questions!

Guess you like

Origin blog.csdn.net/weixin_62222095/article/details/126989604