JavaWeb - response encoding and request encoding

I. Introduction


I used to be very worried about coding problems in the web, but now I have no choice but to do it. I have to bite the bullet and figure out what is in it for everyone to share==Be sure to understand the principle first, and then dive into the code==


The basics of coding


Here we mainly understand the problem of character set and character encoding.

The character set is like a font library, similar to English, Chinese, Japanese, etc., just a library;

Character encoding is like a mapping relationship, which maps the character set to a number and stores it in a computer; such as utf, note that utf-8, utf-6, etc. are all a character set, and different character encodings==


3. Response coding


Generally, the tomcat server decodes ios-8859-1 by default. After tomcat8, the default utf-8 does not need to be modified. The browser's default decoding is gbk, so we generally set it by ourselves, which is better to control.

Here response.setHeader("content-type", "text/html;charset=utf-8"); has two functions: 1. The returned data is encoded according to utf-8; 2. Set the contenttype to inform the browser of the encoding format, and then the browser It will be decoded in the corresponding format, so that there will be no garbled characters==

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setHeader("content-type", "text/html;charset=utf-8");
		//request.setCharacterEncoding("utf-8");
		response.getWriter().println("Hahaha");
	}



Fourth, the request code


It is divided into get request and post request:


1. get request

You can use getParameter() directly without conversion. If tomcat7 and before, you may need to set the comment code, because the default gbk of tomcat between them needs to be converted.

Then set the return encoding format to setHeader. Only gbk and utf-8 can be used here. I started to set iso-8859-1 except for garbled characters. Later, I found that my brain was watery, iso-8859-1 does not support Chinese at all==


		String name=request.getParameter("name");
/*		byte[]bytes=name.getBytes("iso-8859-1");
		name=new String(bytes,"utf-8");
		response.setHeader("content-type", "text/html;charset=utf-8");*/
		
		response.setHeader("content-type", "text/html;charset=utf-8");
		response.getWriter().print(name);


2. post request

The post request needs to set the corresponding decoding method, which is generally determined by the interface that sends the post request. For example, the jsp interface with a form encodes utf-8 by default, and the sent request is also utf-8.

		request.setCharacterEncoding("utf-8");
		String name=request.getParameter("name");
		response.setHeader("content-type", "text/html;charset=utf-8");
		//response.setHeader("content-type", "text/html;charset=utf-8");
		response.getWriter().print(name);





5. URL encoding


URL encoding is to convert Chinese into hexadecimal representation, in order to prevent data loss during network transmission.

Get generally does not encode Chinese, post encodes Chinese, and then automatically encodes it in the browser and automatically decodes it in the server.



4. Summary


  • set response encoding;

  • Set request encoding, get and post;

  • URL encoding;


Guess you like

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