This is the 18th day of my participation in Gwen Challenge

We looked at HttpServletResponse a few days ago, so let’s look at HttpServletRequest today. As with HttpServletResponse, the request type in the service method is ServletRequest and the request type in the doGet/doPost method is HttpServletRequest. HttpServletRequest is a subinterface to ServletRequest with more power and methods.

Request running process

Http requests have been covered before in JavaWeb – Http protocol details and will not be covered here. This object can be used to obtain the Http request line, request header, and request body, respectively.

Get the request row from request

GetMethod () : String getMethod();

Get the requested resource:

String getRequestURI() ;

StringBuffer getRequestURL() ;

String getContextPath(); – Name of the Web application

String getQueryString(); —- get Parameter string after url address submission, for example, username= zhangsan&Password =123

Request.getremoteaddr () – Gets the IP address of the client to access

Note: Request gets some information about the client (client)

Get the request header from request

long getDateHeader(String name)

String getHeader(String name)

Enumeration getHeaderNames()

Enumeration getHeaders(String name)

int getIntHeader(String name)

The referer header is used to implement the source of this access and to prevent theft

Get the request body from request

The contents of the request body are the request parameters submitted via POST in the following format:

username=zhangsan&password=123&hobby=football&hobby=basketball

//key ---------------------- value

username				[zhangsan]
password				[123] Hobby [football, basketball]Copy the code

Using the above parameters as an example, the request parameters can be obtained by using this method:

String getParameter(String name)

String[] getParameterValues(String name)

Enumeration getParameterNames()

Map<String,String[]> getParameterMap()

Note: Get request parameters can be obtained as described above

Way the garbled message: post submission request. SetCharacterEncoding (” utf-8 “);

Parameter = new String(parameter.getBytes (” iso8859-1 “), “UTF-8”);

Other features of Request

(1) Request is a domain object

The Request object is also an area object that stores data, so it also has the following methods:

setAttribute(String name, Object o)

getAttribute(String name)

removeAttribute(String name)

Note: The scope of the request field is a single request

(2) Request completes request forwarding

Obtain the request forwarder —-path is the forwarding address

RequestDispatcher getRequestDispatcher(String path)

Forwarder object forwarding

requestDispathcer.forward(ServletRequest request, ServletResponse response)

Note: How does the life cycle of the ServletContext domain compare to that of the Request domain?

ServletContext:

Create: The server is started

Destroy: The server is down

Scope of domain: the entire Web application

Request:

Create: Request is created during access

Destroy: Response ends request destruction

Scope of a domain: in a request

Note: What is the difference between forwarding and redirecting?

1) Redirects two requests and forwards one request

2) The address in the redirection address bar changes, but the forwarding address remains unchanged

3) Redirection can access external website forwarding can only access internal resources

4) Forwarding has better performance than redirection

That’s all for today’s content. If you have different opinions or better ideas, please contact Ah Q: Qingqing-4132. Ah Q can invite you to join the technical group to discuss technology together. Ah Q is looking forward to your arrival!