Java web request and response Servlet Technology

The main function of servlet is to process client requests and respond. Therefore, for each request, the web container will create two objects before calling service (), namely HttpServletRequest and httpservletresponse. HttpServletRequest encapsulates the HTTP request message, and httpservletresponse encapsulates the HTTP response message. It should be noted that during the operation of the web server, each servlet will create only one instance object, but each request will call the service (ServletRequest req, servletresponse RES) method of the servlet instance. Here, HttpServletRequest is a subclass of ServletRequest and httpservletresponse is a subclass of servletresponse.

The inheritance relationship between HttpServletRequest and httpservletresponse interfaces is as follows:

1、HttpServletResponse

The httpservletresponse interface inherits from the servletresponse interface. Since the HTTP response message is divided into three parts: status line, response message body and message body, the method of sending response status code, response message header and response message body to the client is defined in the httpservletresponse interface. Although there are many methods in the httpservletresponse interface, we often use those methods. If other methods are used, you can read the source code or relevant materials of the response.

Send status code correlation function

Send response header correlation function

Send response message body correlation function

Chinese output garbled

The data in the computer is stored in binary form. Therefore, when transmitting text, the conversion between bytes of characters will occur. The conversion between characters and bytes is completed by looking up the code table. The process of converting characters into bytes is called encoding, and the process of converting bytes into characters is called decoding. If the code table used for encoding and decoding is different, there will be a problem of garbled code.

Note: when encoding the character output stream of httpservletresponse object, ISO 8859-1 encoding is adopted by default. This encoding method is not compatible with Chinese. For example, "China" will be encoded as "63" (63 will be displayed for characters not found in the code table of ISO 8959-1). When the browser decodes the received data, GB2312 will be used by default to decode "63" into "?", The browser decodes the two characters "China" into "?".

Httpservletresponse program example

Sometimes we encounter the problem of regularly jumping to the page. The refresh header field in HTTP can notify the browser to automatically refresh and jump to other pages within the specified time, and the web page will refresh and jump to the specified page regularly.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>