Java and java

Source: Internet
Author: User

Java and java

After learning the web part recently, I found that some parts are always easy to understand. However, when we put all the pieces together, there are always a few knowledge points that are easy to confuse. In fact, there are already enough information on the Internet, although there are also many hardworking porters. But the ultimate goal is not to understand ourselves? What we understand is really ours. As an extra reward, let's take a look at the nine implicit objects in JSP.

Let's take a look at the nine implicit objects about JSP. They can always be emphasized by teachers.

 

 

Scope ------ as the name suggests, the scope of the action is also! If you want to learn a knowledge point by yourself, what goals do you want to achieve? From the perspective of the medical doctor to the addition, deletion, modification, and query of the database, everything starts to look nothing more: What is it? What is the purpose? How to use it? Why? Therefore, I think we should grasp the following questions and take these questions to learn and discover our own shortcomings, which is the best way to learn.

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

2) The role of the scope. (What is the purpose ?)

3) how to use these scopes. (How to use it ?)

4) it uses the implementation principle in this way. (Why ?)

 

Next we will start the analysis one by one:

(1) servletcontext domain (application domain)

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

The scope of the servletcontext domain is the entire web application.

After data is generated, use servletcontext.

It is the largest domain in the four domains.

2) The role of the scope. (What is the purpose ?)

Because all servlets in a web application share the same servletcontext object, multiple servlets share data between different servlets through the servletcontext object.

3) how to use these scopes. (How to use it ?)

A) It can be bound by programming or accessed by all servlets and JSPs as global variables of web applications.

 

Set the Context attribute: ServletContext application = this. getServletContext (); application. setAttribute ("person1", new Person ("Jim"); application. setAttribute ("person2", new Person ("Green"); obtain the Context attribute: 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 and add the following content:

<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 resource files

You can use the ServletContext interface to directly access the static content document structure in the web application, including HTML, GIF, and JPEG files. Follow these steps:
. GetResource ()
. GetResourceAsStream ()
The parameters of both methods start with "/", indicating the relative path of the resource to the context root. the document structure can be stored in a Server File System, a war package, a remote server, or other locations. Cannot be used to obtain dynamic resources, for example, getResource ("/index. jsp "), this method will return the source code of the jsp file, rather than dynamic pages. you can use "DispatchingRequests" to obtain dynamic content. lists resources that can be accessed in a web application. You can use the getResourcePaths (String path) method.

4) it uses the principle or advantages and disadvantages of implementation in this way. (Why ?)

Servlet is not suitable for data output, so you need to forward the data to the JSP file for beautification and then output to the client.

Java code can be embedded in JSP, which makes it possible to receive java data. At the same time, because the servletcontext domain allows the entire web application to share the data, the "thread security" problem also affects data forwarding. Therefore, the request domain must be used.

 

(2) Httpsession domain)

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

The scope of Httpsession is: one session.

After the data is generated, it is displayed to the user, and the Httpsession domain will be used in other cases.

It is the second largest domain in the four domains.

2) The role of the scope. (What is the purpose ?)

(Session range) when the request. getSession () method is called for the first time, the server checks whether a corresponding session exists. If not, create a session in the memory and return it. If a session is not used for a short period of time (30 minutes by default), the server destroys the session. If the server is shut down abnormally, the session that has not expired will be destroyed. If the invalidate () method provided by the session is called, the session can be destroyed immediately.

3) how to use these scopes. (How to use it ?)

  

A) operate session (String) request in jsp. getSession (). getAttribute ("username"); // GET request. getSession (). setAttribute ("username", "xxx"); // Set B) operate the request in session // servlet in java. getSession (); session. getAttribute ("username"); session. setAttribute ("username", "xxx"); session. setMaxInactiveInterval (30*60); session. invalidate (); // method 1 ServletActionContext in struts. getRequest (). getSession (). setAttribute ("username", "xxx"); ServletActionContext. getRequest (). getSession (). getAttribute ("username"); ServletActionContext. getRequest (). getSession (). setMaxInactiveInterval (30*60); ServletActionContext. getRequest (). getSession (). invalidate (); // method 2 ActionContext in struts. getContext (). getSession (). put ("username", "xxx"); ActionContext. getContext (). getSession (). get ("username"); ActionContext. getContext (). getSession (). clear (); c) web. operate session <session-config> <session-timeout> 30 </session-timeout> </session-config> d) tomcat --> conf/web. xml <session-config> <session-timeout> 30 </session-timeout> </session-config>

 

 

 

4) it uses the implementation principle in this way. (Why ?)

HttpSession creates a unique memory space for the browser on the server and stores session-related information. After a session is created on the server, the session ID is sent back to the client in the form of a cookie. In this way, when the client's browser is disabled, the server finds that the client browser has a session id, and uses the corresponding session in the memory to serve the session. I don't know why!

 

(3) ServletRequest domain (request domain)

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

The ServletRequset domain is the entire request chain (request forwarding also exists ).

After data is generated, you only need to use it once. In this case, use the ServletRequset domain.

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

2) The role of the scope. (What is the purpose ?)

Share data in the entire request chain.

Most commonly used: the processed data in the servlet is handed to the JSP for display. In this case, the parameters can be placed in the ServletRequset domain with the past.

3) how to use these scopes. (How to use it ?)

A) method for obtaining client information
The getRequestURL method returns the complete URL when the client sends a request.
The getRequestURI method returns the resource name in the request line.
The getQueryString method returns the parameters in the request line.
The getRemoteAddr method returns the IP address of the client sending the request.
The getRemoteHost method returns the complete host name of the client sending 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.
The getLocalName method returns the Host Name of the WEB server.
GetMethod
B) obtain the client request header
GetHeader (string name) Method
GetHeaders (String name) Method
GetHeaderNames Method
C) obtain client request parameters (data submitted by the client)
GetParameter (name) Method
GetParameterValues (String name) Method
GetParameterNames Method
GetParameterMap Method

4) it uses the implementation principle in this way. (Why ?)

The server creates a service method before calling the service method and passes in the service method. The entire request ends, and the request lifecycle ends.

 

(4) PageContext domain (page domain)

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

The PageContext field applies to the entire JSP page.

It is the smallest domain in four domains.

2) The role of the scope. (What is the purpose ?)
A) It can obtain other eight types of implicit objects and can be considered as an entry object.

B) obtain 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 to use these scopes. (How to use it ?)

The following uses a simple JSP page program as an example:

<% @ 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"> 

 

 

 

Display result:

 

Page value: the value set by the Earth request: the value set by the session in the Solar System: the value set by the application in the galaxy: the value in the cosmic range 1: the value in the earth range 2: value in range 3 of the Solar System: Value in range 4 of the galaxy: value set by the session after the modified pageContext of the universe: value set by the application after the modified nullpageContext: Search for the cosmic value: earth attribute name range: 1

 

 

 

 

4) it uses the implementation principle in this way. (Why ?)

PageContext object, which represents the page context. This object is mainly used to access Shared data between JSP. When a JSP request starts and the response ends, it is destroyed.

 

 

 



 

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.