When the web server receives the client's http request, it will create a request object representing the request and a response object representing the response for each request. Since the request and response objects represent requests and responses, if we want to obtain the data submitted by the client, we only need to find the request object. To output data to the client, just look for the response object.
Copy the code code as follows:
package com.yyz.response;
import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//Problem with outputting Chinese
public class ResponseDemo extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String data = "China";
OutputStream out = response.getOutputStream();
out.write(data.getBytes());
/**
* out.write(data.getBytes()); This code involves checking the code table twice.
* When "China" changes from character data to byte data, the gb2312 code table will be consulted.
* When the data is sent to the browser for display, the code table needs to be consulted again. The code table consulted at this time is related to the browser settings.
*/
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
Test results when the browser encoding is set to GB2312:
Test results when the browser encoding is set to UTF-8:
In order to make our website accessible to foreign users, when we convert character data into byte data, we must specify the conversion code table as UTF-8. But at this time, if the browser is opened with GB2312, garbled characters will appear again. Although this garbled code problem can be solved by changing the browser settings, it is not conducive to enhancing the user experience. Therefore, we need to use a program to tell the browser which code table to consult to display the data.
Copy the code code as follows:
package com.yyz.response;
import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//Problem with outputting Chinese
public class ResponseDemo extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//On the server side, which code table is used to output the data, then it is necessary to control which code table the browser is opened with.
String data = "China";
response.setHeader("content-type", "text/html;charset=UTF-8");
OutputStream out = response.getOutputStream();
out.write(data.getBytes("UTF-8"));
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
Learn one more trick:
Use the <meta> tag in HTML language to control browser behavior.
<meta http-equiv="Content-type'' content=''text/html;charset=UTF-8">
http-equiv simulates the HTTP response header and tells the browser to open it in UTF-8 code table. Real response headers take precedence over response headers simulated with http-equiv.
In actual development, the server should use character streams to write text data to the browser. However, the default code table of the character stream obtained through the getWriter method of response is ISO8859-1. There is no corresponding Chinese encoding in this code table, so it will be changed? The corresponding encoding is sent to the browser, and when the browser is opened, it is full of question marks. The code table consulted by the server when sending data can be modified through setCharacterEncoding of response.
Copy the code code as follows:
package com.yyz.response;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//Problem with outputting Chinese
public class ResponseDemo extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//On the server side, which code table the data is output from, then it is necessary to control which code table the browser is opened with.
String data = "China";
response.setHeader("content-type", "text/html;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
out.write(data);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
There are a few small details to note here:
1. response.setCharacterEncoding("UTF-8"); needs to be written in front of PrintWriter out = response.getWriter();. It is useless to set the encoding after getting the character stream.
2. response.setHeader("content-type", "text/html;charset=UTF-8"); There is a simpler way of writing response.setContentType("text/html;charset=UTF-8");.
3.response.setContentType("text/html;charset=UTF-8"); This code actually has two functions: notifying the response to output in UTF-8 and the browser to open in UTF-8. That is equivalent to response.setHeader("content-type", "text/html;charset=UTF-8"); and response.setCharacterEncoding("UTF-8");.
4. Through the above reading, readers should be able to understand why the output of response.getOutputStream.write(1); in the browser is not 1. Because the browser is a text editor, after receiving the data, it will take 1 to look up the code table, and then display the corresponding characters. If you want to output numbers in the browser, you should turn the numbers into strings, response.getOutputStream.write((1+"").getBytes());.
Use OutputStream (byte stream) to send data:
1. response.getOutputStream().write("China".getBytes());//Send data in default encoding
2. response.getOutputStream().write("China".getBytes("UTF-8"));//Send data in UTF-8 encoding, the browser (default GB2312) will appear garbled
Solution:
2.1 By changing the encoding method of the browser: IE/"View"/"Encoding"/"UTF-8" (not advisable)
2.2 Inform the client of the encoding method by setting the response header: response.setHeader("Content-type", "text/html;charset=UTF-8");//Inform the browser of the data type and encoding
2.3 Simulate request headers through meta tags: out.write("<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />".getBytes());
2.4 Through the following method: response.setContentType("text/html;charset=UTF-8");