Nine built-in objects and four scopes of javaee jsp pages

Nine built-in objects and four domains 1.
Four domains
The role of domain objects: save data, obtain data, and share data.
The scope ranges from small to large: PageContext (jsp page), ServletRequest (one request), HttpSession (one session), ServletContext (the entire web application).

The first scope is page, which is only valid on the current page, that is, the page requested by the user is valid. When the current page is closed or transferred to other pages, the page object will be released after the response is returned to the client.
    The second scope is request, which is valid in the current request. Request can realize the information transfer in the page through the setAttribute() method, and can also jump between pages through the forward() method. It should be noted that the request is forwarded rather than redirected. Forwarding is transparent to the browser, that is, no matter how the page jumps, the address bar still displays the original address.
    The third scope is session, which is valid in the current session. When the same browser on a computer makes multiple visits to the server, the information passed between these multiple visits is the scope of the session scope. It can be considered that the session starts from the first HTTP request sent by the browser, but the end time of the session is uncertain, because the server will not be notified when the browser is closed. Generally, the default time set by Tomcat is 120 minutes. It can also be set through the setMaxInactiveInterval(int) method, or the current session is forcibly terminated through the invalidate() method.
    The fourth scope is application, which is valid in all applications, that is, from the start of the server to the end of the server, the data stored in the application scope is valid, and can also be assigned by setAttribute and getAttribute.

Built-in object one (out):
    The out object is used to output information in the Web browser and manage the output buffer on the application server. When using the out object to output data, you can operate the data buffer, clear the residual data in the buffer in time, and make buffer space for other output. After the data output is completed, the output stream should be closed in time. The most commonly used method is print, which can display string information on the page. The scope of out is page:
 
built-in object 2 (request):
    The request object is an object of type javax.servlet.httpServletRequest. This object represents the request information of the client, and is mainly used to accept the data transmitted to the server through the HTTP protocol. (Including header information, system information, request method and request parameters, etc.). The scope of the request object is a request (that is, the request scope). The commonly used methods of request are getParameter(String name) to obtain the page submission data according to the form component name, getParameterValues(String name) to obtain the data submitted by a group of form components named with the same name, setCharacterEncoding(String charset) before calling the getParameter() method to solve Chinese garbled characters, getRequestDispatcher(String path) returns a javax.servlet.RequestDispatcher object, the forward() method of the object is used Forwarding request: built-in object three (response)
 
:
Response represents the response to the client, mainly to transfer the object processed by the JSP container back to the client. The response object also has scope, it is only valid within the JSP page. Common methods of response include addCookie (Cookie cookie) to add a cookie to the client, serContentType (String type) to set the contentType type of the HTTP response, setCharacterEncoding (String charset) to set the character encoding type used in the response, and sendRedirect (String location) to relocate the request to a new address. This is where the address on the address bar will change:

Built-in object four (session):
The session object is an object related to user requests automatically created by the server. The server generates a session object for each user, which is used to save the user's information and track the user's operation status. The session object internally uses the Map class to save data, so the format of saving data is "Key/value". The value of the session object can use the object type. Common methods of session include setAttribute(String key, Object value) to save the object in the session in the form of key/value, getAttribute(String key) to obtain the value saved in the session through the key value, invalidate() to force the session object to be invalid, getId() to obtain the sessionid, setMaxInactiveInterval(int interval) to set the session inactive time, getMaxInactiveInterval() to obtain the effective inactive time of the session , removeAttribute(String key) deletes the value of the corresponding key in the session:

Built-in object five (application):
The application object can save information in the server until the server is closed, otherwise the information saved in the application object will be valid throughout the application. Compared with the session object, the application object has a longer life cycle, similar to the "global variables" of the system. The common methods of application are setAttribute(String key, Object value) to store the object in the application in the form of key/value, getAttribute(String key) to obtain the object stored in the application through the key, and getRealPath(String path) to return the real path of the relative path:

Built-in object six (pageContext):
The function of the pageContext object is to obtain any range of parameters, through which you can obtain objects such as out, request, response, session, and application of the JSP page. The creation and initialization of the pageContext object is done by the container, and the pageContext object can be used directly in the JSP page. Common methods of pageContext include getRequest() to get the request object, getResponse() to get the response object, getSession() to get the session object, getOut() to get the out object, setAttribute(String key, Object value) to save the attribute, getAttribute(String key) to get the attribute, include("url") to request the specified resource, and include the response result of the target resource in the response of the calling page:

Built-in object seven (page):
    The page object represents the JSP itself, and is only legal within the JSP page. The page implicit object essentially contains the variables referenced by the current Servlet interface, similar to the this pointer in Java programming.
Built-in object eight (config): 
    The main function of the config object is to obtain the configuration information of the server. A config object can be obtained through the getServletConfig() method of the pageConext object. When a Servlet is initialized, the container passes certain information to the Servlet through the config object. Developers can provide initialization parameters for Servlet programs and JSP pages in the application environment in the web.xml file.
Built-in object nine (exception):
    The function of the exception object is to display exception information, which can only be used in pages containing isErrorPage="true", and JSP files cannot be compiled when using this object in ordinary JSP pages. The exception object, like all objects in Java, has an inheritance structure provided by the system. The exception object defines almost all exceptional conditions. In the Java program, you can use the try/catch keyword to handle exceptions; if there is an exception that is not caught in the JSP page, an exception object will be generated, and the exception object will be sent to the error page set in the page command, and then the corresponding exception object will be processed in the error page.
    Because the last three objects are rarely used in JSP pages, so I won't introduce them here.

Guess you like

Origin blog.csdn.net/Rockandrollman/article/details/131742510