Servlet --- request built-in objects

Request object:

Question: browser initiates a request to the server, will follow the HTTP protocol to request data sent to the server. So how to change the data storage server receives a request it? To not only save, but also to ensure the completion of the.

Solution: Use object store, each server accepts a request, the requested data to create a special request to store an object.

Realization: request Object Explanation: The server receives the request after the browser, it will create a Request object, the object is stored in the data request related requests. Request object when calling the Servlet server will be created as a means of arguments passed to the Servlet, such as: service method.

Use: acquiring request header row data acquisition request data acquired user data

Response object:

Problem: After using the Request object to obtain the requested data and processing, processing results to the browser how to display it?

Solution: Use the Response object to explain: When the server request processing specified in the call Servlet, Servlet delivery methods will give two arguments request and response. Wherein the storage request request data associated with the request, the response is used for responding to an object.

Use: setting response header encoding format in response to the responding entity is provided

Request distortion problem solving: String using re-encoded: uname = new String (uname.getBytes ( "iso8859-1"), "utf-8"); Get request mode: using the service methods: req.setCharacterEncoding ( "utf -8 "); server.xml file found in conf files in the directory tomcat server, open as follows:

Post request mode: using the service methods: req.setCharacterEncoding ( "utf-8");

 Process Summary:

Servlet use process:

Encoding format setting request provided information acquisition request in response to the encoding format in response to processing result of the information processing request

Data transfer process:

Browser ------> ------- server> Database

Browser <server ------ <------- database

Forwards the request:

Problem: The server after receiving a request from the browser, just use a Servlet request process, will result in different code redundancy logic Servlet, Servlet responsibilities is not clear.

Solution: Use forward the request.

Features: a request for information does not change the address bar.

Request Object Scope

Problem: After using forwards requests, how data sharing it between different Servlet? Or how data flow from one to another servlet Servlet it?

Solution: use the request object's scope

使用: request.setAttribute(object name,Object value); request.getAttribute(Object obj)

Role: to solve the data of a different Servlet requests within (request data + other data) share issue.

Scope: Based on the request forwarded a request Servlet all share.

NOTE: use the Request object data streams, the data is only valid in one request.

Features: The server creates each request creates a request lifecycle

Redirect

Question: If the current request, Servlet how to do can not be processed if a request is forwarded, resulting in duplication of data submission form how to do??

Solution: Use redirection

Use:. Response.sendRedirect ( "Path") is a local path: uri network path: directional information resource URL

Features: two requests the browser address bar information changes to avoid duplicate submission form

Guess you like

Origin www.cnblogs.com/zang1998/p/10968771.html