当前位置:网站首页>Record -- about the problem of garbled code when JSP foreground passes parameters to the background

Record -- about the problem of garbled code when JSP foreground passes parameters to the background

2022-06-24 06:39:00 Cannon cannon~~

Record – About JSP There is a garbled code problem when the foreground transmits parameters to the background

Record your study JSP The problems and solutions in the process of construction

Method 1

In case of garbled code, first check the code settings on the code page , The coding of all pages in the project needs to be unified
 Insert picture description here
Can be unified into UTF-8, perhaps GB2312 etc.

Method 2

Add... Before the code that needs to pass characters :

request.setCharacterEncoding("UTF-8");

The inside of this is UTF-8 For example , You need to modify other codes by yourself
Called here request(request yes jsp Built in objects for , It is mainly used to process various parameters and options in the request submitted by the client browser ) Of setCharacterEncoding Method

Method 3

Write a java Tool class , Write a method to deal with garbled code , Call... When needed

public static String toChinese(String str){
    		// The method of transcoding 
		if(str==null)
			str="";
		try {
    
			str=new String(str.getBytes("ISO-8859-1"),"gb2312");
		} catch (UnsupportedEncodingException e) {
    
			str="";
			e.printStackTrace();
		}
		return str;
	}

In fact, the tool class can not only write transcoding methods , You can also write data type conversion methods

Method four

If the appeal method is tried and the problem is not solved , Please try the last method
Here I use my own example to illustrate
 Insert picture description here
I have used some of the previous methods in the figure , Can not solve my garbled code problem , There is still a question mark when running on the server (??)
How to solve it ?

Let's talk about how to do it :
find tomcat Installation path , Such as my :D:\ruanjian\apache-tomcat-9.0.8, Find... Under the path conf This folder , open
 Insert picture description here
find server.xml Insert picture description here
open , Find the inside connector Label line :

 Insert picture description here
Add... To the tag attribute :URIEncoding=“UTF-8”
 Insert picture description here
preservation , close , Let's check if it works
 Insert picture description here
To test the transmitted characters , I use the console to output , It's easy to see
 Insert picture description here
The correct Chinese characters have been output on the console , Get it done !

Then there are ways to add , Welcome to correct !!!( Bow )

above !

原网站

版权声明
本文为[Cannon cannon~~]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206240019092962.html