Garbled code problem of JavaWeb project and how to solve it

When writing codes for projects, I often encounter garbled characters. How can we solve them? Let me explain in detail!

In web projects, the problem we usually encounter when writing code is Chinese garbled characters. Sometimes it is necessary to set the Chinese format encoding of utf-8. If not, garbled characters will appear, which will lead to text or data transmission. Or there is a problem in the tomcat server, and these all require us to adjust the utf-8 Chinese format. Next, I would like to describe to you the problems I encountered in the web project and how I solved them.

Problem Description

In the HttpServlet class, the data is parsed and transmitted to the browser. I set the transmission encoding format of response and request to Chinese format utf-8. The specific situation is as follows: Then use Alibaba’s JSON to parse the dependency
insert image description here
package and import the device into the lib resource. In the management file, how to see the picture below: insert image description here
At the same time, through the test class, I can find that there is no problem here, and the data in json format can be output, but when it is deployed to the tomcat server, a bug occurs and a 500 error is reported, which is tomcat There is a problem with the server, and the display problem occurs when the server encounters garbled characters during operation and cannot continue to execute the code, so this problem is a bit strange and puzzling, but I finally found the root cause through the guidance of others. The specific error is as follows:insert image description here
insert image description here

Cause Analysis:

We can find out from the above error report that the problem is that the JSON parsing dependency package cannot be found due to an error encountered during the operation of Servet. Generally, we put the dependent packages in the lib package. Finally, I checked the directory structure in detail and found that my dependent packages were misplaced.

solution:

So you need to put the lib resource package in the specified location. If you accidentally put it in the wrong place, the resource package may not be found when the server is running, so the json dependency package will not be found. So where should it be placed? It must be placed under the web package (webapp) to be correct. At the same time, we also need to add the dependency package to the jar resource package, as shown in the following figure: Then right-click the
insert image description here
lib package, find Add as library at the bottom and click it You will encounter the following prompt box.
insert image description here
At this point, we can just click OK, and then we can redeploy and run.
This is the page display of my successful operation:
insert image description here

Finally, I am very grateful to the veterans for their support, like and follow. At the same time, I will publish a complete blog based on this project in the future. At the same time, I will explain this project in detail and share the complete source code in the blog! Finally, thank you again for your attention! Ha ha ha ha!

Guess you like

Origin blog.csdn.net/a910247/article/details/130031662