Servlet request and response objects

Principles of request and response objects
(1) The tomcat server creates the corresponding ServletDemo object according to the resource path in the requested url.
(2) The tomcat server creates request and response objects, and the request object encapsulates the requested message data.
(3) Tomcat passes the request and response objects to the service method, and calls the service method.
(4) Programmers can obtain the request message data through the request object, and set the response message data through the response object.
(5) The server will take the response message data set by the programmer from the response object before responding to the browser.

Principles of request and response objects
(1) The request and response objects are created by the server and we use them.
(2) The request object is used to obtain the request message, and the response object is used to set the response message

request function
1. Get request message data
Get request line data
For example, GET /day14/demo1?name=zhangsan HTTP/1.1
method:
(1) Get request method: GET
String getMethod()

(2) Get the virtual directory: /day14 (emphasis)
String getContextPath()

(3) Obtain the Servlet path: /demo1
String getServletPath()

(4) Get the request parameter of get method: name=zhangsan
String getQueryString()

(5) Obtain the request URI: /day14/demo1 (Key point)
String getRequestURI Obtain: /day14/demo1
StringBuffer getRequestURLObtain: http://localhost/day14/demo1

(6) Obtain the protocol and version: HTTP/1.1
String getProtocol()

(7) Obtain the IP address of the client:
Sting getRemoteAddr()

2. Get the request header data
Method:
(1) Get the value of the request header by the name of the request header
String getHeader(String name)
(2) Get the name of all request headers
Enumeration<String> getHeaderNames()

3. Obtain request body data
Request body: Only POST request method has a request body. The request parameters of the POST request are encapsulated in the request body.
Steps:
(1) Obtain the stream object
BufferedReader getReader()Obtain the character input stream, and only operate the character data to
ServletInputStream getInputStream()obtain the character Throttling the input stream, you can manipulate all types of data
(2) Get data from the stream object

4. Other functions
(1) General method of obtaining request parameters (both get request and post request can be used)
String getParameter(String name) Obtain parameter value
String[] getParameterValues(String name)according to parameter name Obtain parameter value array according to parameter name Obtain
Enumeration<String> getParameterNames()the parameter name of all requests
Map<String, String[]> getParameterMap()Obtain the map set of all parameters

Chinese garbled problem:
get method: In Tomcat8, the problem of get garbled method has been solved.
Post method: garbled. Just need to set the encoding of the request before getting the parameters:request.setCharacterEncoding("utf-8");

(2) Request forwarding
Step:
Obtain the request forwarder object through the request object: RequestDispatcher getRequestDispatcher(String path)
Use the RequestDispatcher object to forward:forward(ServletRequest request, ServletResponse response)

Features: The
path of the browser address bar does not change, and it
can only be forwarded to the internal resources of the current server.
Forwarding is a request

(3) Data sharing
Domain object: an object with a scope that can share data within the scope
request domain: represents the scope of a request, generally used to share data among multiple resources that request forwarding

Method:
void setAttribute(String name, Object obj)Store data
Object getAttribute(String name)Get values
void removeAttribute(String name)through keys Remove key-value pairs through keys

(4) Obtain ServletContext
ServletContext getServletContext()

Guess you like

Origin blog.csdn.net/qq_42524288/article/details/104409702