js and java Chinese garbled

Because the encoding method of UTF-8 is used when encoding Chinese in jsp, and when the request.getParameter(); method is called in the servlet, the encoding format specified by the server is used to automatically decode once, so the foreground encodes the background once Decoding once and the decoding and encoding methods are not used, resulting in the appearance of garbled characters;

 

 

Solution one:

JavaScript

window.self.location="searchbytext.action?searchtext="+encodeURIComponent(encodeURIComponent(seartext));

Java

searchtext=java.net.URLDecoder.decode(searchtext,"UTF-8");

The reason why it needs to be encoded twice: When the background java code assigns value to searchtext, it has already used one decoding, but the decoding result is still wrong. So we can perform two encoding operations on the page, so that the automatic one in the background can be offset once, and then use searchtext=java.net.URLDecoder.decode(searchtext,"UTF-8"); to decode once All right.

 (ps: if it doesn’t work, turn it a few times and try it a few times, this is a pit

  fileNameTemp=  java.net.URLDecoder.decode(fileNameTemp,"UTF-8");

   //System.out.println(fileNameTemp);

  fileNameTemp=  java.net.URLDecoder.decode(fileNameTemp,"UTF-8");

)

Solution two:

Another way is to do one coding in JavaScript, and just change the idea when doing background java processing:

java code:

String s = new String(request.getParameter("name").getBytes("ISO8859-1"), "UTF-8");

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326590402&siteId=291194637