Summary of four Java domain objects

Source: Internet
Author: User

Summary of four Java domain objects
I. ServletContext

1. lifecycle: When a Web application is loaded into a container, a ServletContext object representing the entire web application is created. When the server is closed or the Web application is removed, the ServletContext object is destroyed.

2. Scope of application: the entire Web application.

3. functions:

A) forwarding between different servlets

this.getServletContext().getRequestDispatcher(/servlet/Demo10Servlet).forward(request, response);

After the method is executed, the service will return to the server, and then the server will call the target servlet, where the request will be re-created and the data of the previous request will be copied.

B) read the resource file.

1. Because the relative path is relative to the directory started by the Java Virtual Machine by default, we can directly write the relative path relative to the tomcat/bin directory, so we cannot get the resource.

If it is written as an absolute path, when the project is released to other environments, the absolute path is wrong.

2. To solve this problem, ServletContext provides:

This. getServletContext (). getRealPath ("/1. properties"). A Virtual Path of the resource is provided, and the actual path of the resource in the current environment is returned.

This. getServletContext (). getResourceAsStream ("/1. properties") returns the stream to the real path of a resource in the virtual path.

3. When obtaining resource files in a non-servlet environment, no ServletContext object is used. In this case, only the class loader can be used.

ClassLoader. getResourceAsStream ("../1. properties"). This method uses the class loader to directly load resources into the memory, which may cause update delays,

And if the file is too large, the memory usage is too large.

ClassLoader. getResource ("../1. properties"). getPath () directly returns the actual path of the resource without updating latency.

Ii. Request domain

1. Life Cycle: a service method is created by the server before the service method is called. The entire request ends, and the request lifecycle ends.

2. Scope of action: the entire request chain (request forwarding also exists ).

3. Role: share data in the entire request chain. Most commonly used: the processed data in the Servlet is displayed to the Jsp, and the parameters can be placed in the Request domain with the past.

Iii. Session domain

HttpSession creates a unique memory space for the browser on the server and stores session-related information.

1. Life Cycle: When the request. getSession () method is called for the first time, the server checks whether a corresponding session exists. If not, it creates a session in the memory and returns it.

If the session is not used for a period of time (30 minutes by default), the server will destroy the session.

If the server is shut down abnormally (forcibly closed), sessions that have not expired will also be destroyed.

If the invalidate () provided by the session is called, the session can be destroyed immediately.

Note: If the server is shut down normally and then started, the Session object will be deactivated and activated. At the same time, if the server passivation time is within the default session destruction time,

The activated session still exists. Otherwise, the Session does not exist. If the Serializable attribute is not implemented when the an data is deactivated in the session, it disappears when the Session is activated.

2. Scope: one session.

4. PageContext domain

1. lifecycle: the lifecycle begins when a JSP request is sent and the request is destroyed when the response ends.

2. Scope: The entire JSP page is the smallest of the four scopes.

Purpose:

(1) obtain the other eight implicit objects, which can be considered as an entry object.

(2) obtain data from all its domains

PageContext

public java.lang.Object getAttribute(java.lang.String name,int scope)public void setAttribute(java.lang.String name, java.lang.Object value,int scope)public void removeAttribute(java.lang.String name,int scope)

Constant representing the domain in pageContext

PageContext.APPLICATION_SCOPEPageContext.SESSION_SCOPEPageContext.REQUEST_SCOPEPageContext.PAGE_SCOPE

The findAttribute method searches for attributes in four domains. The search sequence is the page domain, request domain, session domain, and application domain,

Search from a small domain to a large domain. If this value is obtained, a null value is returned if it is not found in all fields. (unlike the el expression, null is returned here, is unfriendly to webpages)

(3) The forward and include methods are provided to redirect to other resources to simplify the redirection and forwarding operations.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.