ServletAPI Detailed Explanation (4)-HttpServletResponse

Let's look at the third method, HttpServletResponse

As mentioned in the principle of servlet operation, the purpose of the doXXX method in the servlet code is to calculate the response according to the request, and then set the response data to the HttpServletResponse object, and then Tomcat will convert the HttpServletResponse object into a character according to the format of the HTTP protocol String, and write back to the browser through Socket

The following methods can set response headers, which are available through the HttpServletResponse object

serial number Method & Description
1 String encodeRedirectURL(String url)
Encodes the specified URL used in the sendRedirect method, or returns the URL unchanged if encoding is not required.
2 String encodeURL(String url)
encodes the specified URL containing the session ID, or returns the URL unchanged if encoding is not required.
3 boolean containsHeader(String name)
Returns a boolean indicating whether the named response header has been set.
4 boolean isCommitted()
returns a boolean indicating whether the response has been committed.
5 void addCookie(Cookie cookie)
adds the specified cookie to the response.
6 void addDateHeader(String name, long date)
Adds a response header with the given name and date value.
7 void addHeader(String name, String value)
adds a response header with the given name and value.
8 void addIntHeader(String name, int value)
Adds a response header with the given name and integer value.
9 void flushBuffer()
Forces whatever is in the buffer to be written to the client.
10 void reset()
clears any data present in the buffer, including status codes and headers.
11 void resetBuffer()
clears the content of the basic buffer in the response, but does not clear the status code and header.
12 void sendError(int sc)
sends an error response to the client with the specified status code and clears the buffer.
13 void sendError(int sc, String msg)
sends an error response to the client with the specified status.
14 void sendRedirect(String location)
Sends a temporary redirection response to the client using the specified redirection location URL.
15 void setBufferSize(int size)
Sets the preferred buffer size for the response body.
16 void setCharacterEncoding(String charset)
Sets the character encoding (MIME charset) of the response sent to the client, for example, UTF-8.
17 void setContentLength(int len)
sets the length of the content body in the HTTP Servlet response, this method sets the HTTP Content-Length header.
18 void setContentType(String type)
Sets the content type of the response sent to the client if the response has not been submitted.
19 void setDateHeader(String name, long date)
Sets a response header with the given name and date value.
20 void setHeader(String name, String value)
sets a response header with the given name and value.
21 void setIntHeader(String name, int value)
Sets a response header with the given name and integer value.
22 void setLocale(Locale loc)
If the response has not been submitted, set the locale of the response.
23

void setStatus(int sc)
sets the status code for this response.

24

void setCharacterEncoding(String charset)

Sets the character encoding (MIME charset) of the response sent to the client e.g. UTF-8

25

void sendRedirect(String location)

Sends a temporary redirect response to the client using the specified redirect location URL.

3XX status code, the browser will jump to the new address

26

PrintWriter getWriter()

Used to write text format data into the body

27

OutputStream getOutputStream()

Used to write binary format data to the body

void setCharacterEncoding(String charset)

 重启服务器后输入URL,发现不能正确识别中文 

如果URL中有中文字符,没有设置UTF8,返回的响应就会出现乱码,浏览器默认不知道字符的编码方式,要通过方法显示的告诉浏览器,就可以正确识别了

或者直接和contenttype一起设置

 重启服务后再次刷新页面,就正常了 

注意:设置字符集和contenttype必须要在返回对象的代码上面,必须先被执行,写到下面不会生效

void sendRedirect(String location)我们使用重定向方法

@WebServlet("/redirct")
public class RedirctServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.sendRedirect("http://www.baidu.com");
    }
}

输入URL(http://127.0.0.1:8080/ServletHelloWorld/redirct)后会直接跳转到百度页面

通过抓包我们可以看到响应中有个Location,这个就代表重定向的地址

keep-Alive是建议浏览器和服务器之间保持长连接,短链接是每个连接只进行一次请求和响应,长连接可以进行多次请求和响应 

我们也可以不用sendRedirect方法,分步进行,可以先设置状态码,再设置Location

 重新启动服务器,输入后发现效果是相同的

Guess you like

Origin blog.csdn.net/chenchenchencl/article/details/129889231