Coding Problems--JavaWeb

 

coding:

Encoding: Strings become bytes; Decoding: bytes become strings

Server: generally ISO8859-1 encoding [response.getWriter().print("Hello everyone"); The server encodes the string "Hello everyone" into bytecode that can be read by the computer, using ISO8859-1 encoding ]

Browser: generally gbk decoding [after the browser receives the response from the server, it decodes the bytecode encoded by the server into a string and displays it on the browser. ]

1. Request encoding

Data encoding sent by the client: determined by the browser:

  1). If the url is given directly in the address bar, it is usually GBK by default, but this may not be too big.

  2). If the request is made through a form or hyperlink on the page, the encoding of the sent parameters is determined by the encoding of the current page.

 Regardless of the encoded data sent by the browser, Tomcat uses ISO-8859-1 encoding by default

  1). POST: You can use the request.setCharacterEncoding() method to set the encoding of the request body data. Because the POST request parameters are in the request body, the encoding can be set. Before using the request.getParameter() method to get the parameters, use the request.setCharacterEncoding() method to set the encoding.

  2). GET: There is no way to set it because the parameters are in the url. So the data obtained using request.getParameter() must be wrongly decoded using iso-8859-1. You can use iso-8859-1 to convert the string back to byte[], and then use the correct encoding to decode it.

  String s = request.getParameter("s");//Wrong decoding using iso-8859-1

  byte[] bytes = s.getBytes("iso-8859-1");//Return the wrong encoding, let the string return to the byte data through iso-8859-1, that is, restore the byte data

  s = new String(bytes, "utf-8");//Reuse the correct utf-8 to decode.

 

Response garbled solution:

response.setCharacterEnconding("utf-8"): Set the data that the server responds to the browser to be encoded in utf-8. Instead of telling the browser what encoding to use to parse the response data.

response.setHeader("Content-Type","text/html;charset=utf-8"): content-Type Its role is to inform the browser of the mimeType type of the response data, and to inform the browser of the encoding of the response data and in what format such encoding to parse the data;

response.setContentType("text/html;charset=utf-8");[These two sentences have the same effect]

 

Request:

Request garbled solution:

When the browser enters data, the browser encodes the entered data (utf-8).

The Request object is created by the server. When the server encapsulates the parameters passed by the browser into the request object, it decodes the data encoded by the browser (iso8859-1) and then encapsulates it into the request object.

Request parameter garbled solution:

1), first re-encode the data encoded by the browser with the server code table, and then use the code table corresponding to the server and the browser to decode [because tomcat8 and below are all decoded by the default iso8859-1 code].

String name = reuqest.getParameter(name);

Name = new String(name.getBytes(“iso8859-1”,”utf-8”));

2), concise writing: only valid for post requests

      request.setCharacterEncoding(“utf-8”);

About get/post request parameter format:

The parameters of the Get request are after the resource path in the request line:

/webContext/xxx?time=new Date()&password=11;

Post: The request parameters are in the request body

userName=tom&password=111;

 

Chinese is not allowed in the cookie. If you want to save Chinese, you need to use URL encoding:

Save cookies:

String name = URLEncoder.encode("姓名", "UTF-8");

String value = URLEncoder.encode("张三", "UTF-8");

Get cookies:

String name = URLDecoder.decode(c.getName(), "UTF-8");

String value = URLDecoder.decode(c.getValue(), "UTF-8");

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326394211&siteId=291194637