Summary of four major scopes in Java

Source: Internet
Author: User

First, ServletContext

1. Life cycle: When a Web application is loaded into a container, a ServletContext object representing the entire web app is created, and the ServletContext object is destroyed when the server shuts down or the Web app is removed.

2, the scope of action: the entire Web application.

3. Function:

A) forwarding between different servlets

This.getservletcontext (). Getrequestdispatcher ("/servlet/demo10servlet"). Forward (request, response);

At the end of the method execution, the service returns to the server, and then the server calls the target servlet, where the request is recreated and the data of the previous request is copied in.

b) Read the resource file.

1, because the relative path by default relative to the Java Virtual machine startup directory, so we directly write the relative path will be relative to the Tomcat/bin directory, so there is no resources. If you write an absolute path, the absolute path is wrong when the project is published to another environment.

2. In order to solve this problem, ServletContext provides:

This.getservletcontext (). Getrealpath ("/1.properties"), given a virtual path to a resource, will return the actual path of the resource in the current environment.

This.getservletcontext (). getResourceAsStream ("/1.properties"), a stream that returns the virtual path of a resource to the true path of the resource.

3, when the non-servlet to obtain the resource file, there is no ServletContext object used, at this time can only use the class loader

Classloader.getresourceasstream (".. /.. /1.properties "), this method uses the ClassLoader to load resources directly into memory, has an issue with update latency, and if the file is too large, takes up too much memory.

Classloader.getresource (".. /1.properties "). GetPath (), directly returns the true path of the resource without the problem of update latency.

Second, Request domain

1. Life cycle: Created by the server before the service method call, incoming service method. The entire request is over, requesting the end of life.

2, the scope of action: The entire request chain (Request forwarding also exists).

3. Function: Share data throughout the request chain. Most commonly used: data processed in the servlet is given to the JSP display, where parameters can be placed in the request domain with the past.

Third, Session field

HttpSession creates a unique memory space for the browser in the server, where session-related information is saved.
1, life cycle: When the first call to the Request.getsession () method, the server will check if there is already a corresponding session, if not in memory to create a session and return.

When the session is not used for a period of time (the default is 30 minutes), the server destroys the session.

If the server shuts down gracefully (forcibly closed), the session with no expiration will be destroyed.

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

Note: The server shuts down properly and then starts, and the session object is deactivated and activated. In the meantime, if the server passivation time is within the session default destruction time, then the activation session still exists.  Otherwise the session does not exist. If the JavaBean data is not implemented when the session is deactivated, serializable will disappear when the session is activated.

2. Scope of action: one session.

Four, PageContext domain

1. Life cycle: When a request to a JSP starts, it is destroyed when the response ends.

2, the scope of action: The entire JSP page, is the smallest of the four major scopes.

Role:

(1) Get the other eight implicit objects, which can be considered as a portal object.

(2) Get data from all of its domains

PageContext methods for manipulating properties in all domains

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)

Constants representing fields in the PageContext

Pagecontext.application_scope

Pagecontext.session_scope

Pagecontext.request_scope

Pagecontext.page_scope

Findattribute method, search for attributes in the four fields, the search order is the page field, request domain, session domain, application domain, from small domain to large domain start search, if the search to get the value directly, if not found in all domains, Returns a null (unlike the EL expression, where null is returned and is unfriendly to the Web page)

(3) Jump to other resources it provides forward and include methods to simplify redirection and forwarding operations

Quote Original: http://www.cnblogs.com/stanljj/p/4119672.html

Blog is to remember that they are easy to forget things, but also a summary of their work, the article can be reproduced, without copyright. Hope to do their own efforts to do better, we work together to improve!

If there is any problem, welcome to discuss together, code if there is a problem, you are welcome to the great God!

Summary of four major scopes in Java

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.