Summary of four domains in Java

Source: Internet
Author: User
Tags session id

Recently learned the Web Part, found that some places are always easy to understand, but put all together to a hodgepodge, there are always a few knowledge points easily confused. In fact, the online information has been enough, although there are no shortage of laborious porters. But the ultimate goal is not to have our own understanding of it? What you understand is really ours. As an additional bonus, let's take a look at the nine implicit objects of the JSP.

You can look at the nine hidden objects in the JSP, they can always be said by the teachers is the key point oh.

Scope------As the name implies, the size range of the function also! If it is you to learn a knowledge point, you want to achieve which goal is to a knowledge point is understood, understood, mastered it? From the palpation of the flat magic doctor to the database additions and deletions, all things begin to be nothing else: what is it? What's the use? How to use it? Why do you use it this way? Therefore, I think we should grasp the following questions, with these problems to learn, find their shortcomings, is the best way to learn.

1) The actual size of the scope. (What is it?) )

2) The role of the scope. (What's the use?) )

3) How these scopes are used. (How to use?) )

4) It uses the principle of implementation. (Why is it possible to use this?) )

The following begins the analysis:

(i) ServletContext domain (application domain)

1) The actual size of the scope. (What is it?) )

The scope of the ServletContext domain is: the entire Web application.

After the data has been generated, not only will it need to be used, but also for others, please use ServletContext.

It is the largest domain within four domains.

2) The role of the scope. (What's the use?) )

Because all servlets in a web app share the same ServletContext object, multiple servlets implement the sharing of data between different servlets through the ServletContext object.

3) How these scopes are used. (How to use?) )

A) can be bound programmatically, or be accessed as a global variable for Web applications by all Servlets and JSPs

Set the context property:              ServletContext application=this.getservletcontext ();              Application.setattribute ("Person1", New Person ("Jim"));              Application.setattribute ("Person2", New Person ("Green")); Get the Context property:              ServletContext application=this.getservletcontext ();              Enumberation persons=application.getattributenames ();               while (Persons.hasmoreelements ()) {                      string name= (String) persons.nextelement ();                      Person p= (person) persons.getattribute (name);                      Application.removeattribute (name);              }

b) Configure the context domain for the entire Web application:

Modify the Web. XML configuration file to include the following:

<context-param>

<param-name>data</param-name>

<param-value>hello World!</param-value>

</context-param>

Access these initialization parameters from the servlet:

ServletContext Application=this.getservletcontext ();

Out.println (Application.getinitparameter ("Data"));

c) Read the resource file

The ServletContext interface provides direct access to the structure of a static content document in a Web App. Includes html,gif and JPEG files. As in the following ways:
. GetResource ()
. getResourceAsStream ()
The arguments for both methods are strings that begin with "/", indicating the relative path of the resource relative to the context root. The document structure can exist in the server file system, in a war package, on a remote server, or in another location. Can not be used to obtain dynamic resources, such as GetResource ("/index.jsp"), this method will return the JSP file source, not dynamic page. You can use "dispatching requests" Get dynamic content. Lists the resources that can be accessed in the Web app, using the Getresourcepaths (String path) method.

4) It uses the principle of implementation or advantages and disadvantages. (Why is it possible to use this?) )

The servlet is not suitable for data output, so it needs to be forwarded to the JSP file for beautification and output to the client.

The JSP can embed Java code, which makes it possible to receive Java data. Also, because the ServletContext domain enables the entire Web application to share this data, the "thread-safe" issue also affects the forwarded data, and therefore requires the request domain to be used.

(ii) HttpSession Domain (session field)

1) The actual size of the scope. (What is it?) )

The scope of the HttpSession is: one session.

The data is generated and displayed to the user, and the httpsession domain is used in cases where it is also used.

It is the second largest domain in four domains.

2) The role of the scope. (What's the use?) )

(Session scope) when the Request.getsession () method is called for the first time, the server checks to see if a corresponding session already exists. If not, a session is created in memory and returned. When the session is not used in a short time (default 30 minutes), the server destroys the session. If the server is not properly closed, the unexpired session will be destroyed. If you call the Invalidate () method provided by the session, you can immediately destroy the session.

3) How these scopes are used. (How to use?) )

  A) The operation session (String) Request.getsession (). getattribute ("username") in the JSP;  Get Request.getsession (). SetAttribute ("username", "xxx");    Set b) Operation in Java session//servlet in Request.getsession ();    Session.getattribute ("username");    Session.setattribute ("username", "xxx");     Session.setmaxinactiveinterval (30*60); Session.invalidate ();    Struts in Method 1 Servletactioncontext.getrequest (). GetSession (). SetAttribute ("username", "xxx");    Servletactioncontext.getrequest (). GetSession (). getattribute ("username");    Servletactioncontext.getrequest (). GetSession (). Setmaxinactiveinterval (30*60);    Servletactioncontext.getrequest (). GetSession (). invalidate ();    Struts in Method 2 Actioncontext.getcontext (). GetSession (). Put ("username", "xxx");    Actioncontext.getcontext (). GetSession (). Get ("username");   Actioncontext.getcontext (). GetSession (). Clear (); c) Operation session <session-config> <session-timeout>30</session-timeout> </sessio in Web. xml N-confIg> d) tomcat-->conf-->conf/web.xml <session-config> <session-timeout>30</session-time Out> </session-config>

4) It uses the principle of implementation. (Why is it possible to use this?) )

HttpSession creates a unique memory space for the browser in the server, where session-related information is saved. Server creation session, will be the session ID number, as a cookie back to the client, so as long as the client's browser is not closed, and then to access the server, will take the session ID number to go, the server found the client browser with session ID comes in, it will use the corresponding session in memory to serve. If you want to ask me why, I do not know Ah!

(iii) ServletRequest domain (request domain)

1) The actual size of the scope. (What is it?) )

The Servletrequset domain is: The entire request chain (Request forwarding also exists).

Once the data has been generated, use only one time, in which case use the Servletrequset domain.

It is the third domain in a range of four domains.

2) The role of the scope. (What's the use?) )

Share data across the chain of requests.

Most commonly used: the data processed in the servlet is given to the JSP display, where parameters can be placed in the Servletrequset domain with the past.

3) How these scopes are used. (How to use?) )

A) How to obtain client information
The Getrequesturl method returns the full URL of the client when the request is made.
The Getrequesturi method returns the resource name portion of the request row.
The GetQueryString method returns the parameters section in the request line.
The Getremoteaddr method returns the IP address of the client that made the request
The Getremotehost method returns the full host name of the client that made the request
The Getremoteport method returns the network port number used by the client
The Getlocaladdr method returns the IP address of the Web server.
Getlocalname method returns the host name of the Web server
GetMethod How to get the client request
b) Get the client request header
GetHeader (string name) method
Getheaders (String name) method
Getheadernames method
c) Obtain the client request parameters (data submitted by the client)
GetParameter (name) method
Getparametervalues (String name) method
Getparameternames method
Getparametermap method

4) It uses the principle of implementation. (Why is it possible to use this?) )

Created by the server before the service method call, the service method is passed in. The entire request is over, requesting the end of life.

(iv) PageContext domain (page field)

1) The actual size of the scope. (What is it?) )

The scope of the PageContext domain is: the entire JSP page.

It is the smallest domain in a range of four domains.

2) The role of the scope. (What's the use?) )
A) It can acquire the other eight implicit objects, which can be considered as a portal object.

b) Get data from all other domains.

c) Jump to other resources. The ForWord and Sendredirect methods are provided to simplify the forwarding and redirection operations.

3) How these scopes are used. (How to use?) )

Here is an example of a simple JSP page program:

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >

Show Results:

Page Set Value: Earth Request Set Value: The value set by the Solar system session: The value set by the Galaxy application: The value of the Universe range 1: The value of the Earth Range 2: The value of the Solar System range 3: The value within the Milky Way range of 4: the Universe PageContext modified session set Value: Nullpa Gecontext Modified Application value: Search for Cosmic Values: Earth Property name Range: 1

4) It uses the principle of implementation. (Why is it possible to use this?) )

The PageContext object that represents the page context, which is used primarily to access shared data between JSPs. When a request to a JSP starts, it is destroyed when the response ends.

Summary of four domains 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.