Home  >  Article  >  Java  >  The most complete summary of the four major domains of Java (pictures and texts)

The most complete summary of the four major domains of Java (pictures and texts)

PHPz
PHPzOriginal
2017-04-03 09:49:181496browse

I recently finished studying the web part, and found that some parts are always easy to understand individually, but when you put them all together into a hodgepodge, there are always a few knowledge points that are easily confused. In fact, there is enough information on the Internet, although there is no shortage of hard-working porters. But isn’t the ultimate goal just for us to understand ourselves? Only what we understand is truly ours. As an added bonus, let's first focus on the nine implicit objects of JSP.

You can take a look at the nine implicit objects of JSP in the picture below. They are always said to be the key points by teachers.

# #Scope------As the name suggests, the size range that works is also! If you are learning a knowledge point by yourself, what goals do you have to achieve to be considered as knowing, understanding, and mastering a knowledge point? From Dr. Bian's observation, hearing, and questioning, to the addition, deletion, and modification of the database, everything starts with the same question: What is it? What is the use? how to use? Why can it be used like this? Therefore, I think we should grasp the following issues, study with these issues, and discover our own shortcomings, which is the best way to learn.

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

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

#3) How to use these scopes. (How to use?)

#4) It uses the principle of implementation like this. (Why can it be used in this way?)

Let’s start analyzing one by one:

(1) servletcontext domain (application domain)

1) Actual scope size. (What is it?)

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

After the data is generated, it will not only be used later, but also used by others, please use servletcontext.

It is the largest domain among the four domains.

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

Since all servlets in a web application share the same servletcontext object, multiple servlets realize data transfer between different servlets through the servletcontext object. of sharing.

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

a) can be bound through programming, or can be used as a global variable in a web application Accessed by all Servlets and JSPs

设置Context属性:
              ServletContext application=this.getServletContext();
              application.setAttribute("person1",new Person("Jim"));
              application.setAttribute("person2",new Person("Green"));
 获取Context属性:
              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

 

  data

##  Hello world!

 

Access these initialization parameters from the Servlet:

            ServletContext application=this.getServletContext();

                                      

c) Read resource files

Use the ServletContext interface to directly access the static content document structure in the web application. Including HTML, GIF and JPEG files. Such as the following methods:
.getResource()
.getResourceAsStream()
The parameters of these two methods are strings starting with "/", indicating that the resource is relative to the context root Relative path. The document structure can exist in the server file system, or in the war package, or on the remote server, or other locations. It cannot be used to obtain dynamic resources, such as getResource("/index.jsp"). This method will return the source code of the jsp file, not the dynamic page. You can use "Dispatching Requests" to obtain dynamic content. List the web applications For resources that can be accessed, you can use the getResourcePaths(String path) method.

4) It uses the principles or advantages and disadvantages of implementation. (Why can it be used in this way?)

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

JSP can embed java code, which makes it possible for it to receive java data. At the same time, since the servletcontext domain allows the entire web application to share the data, the "thread security" problem also affects the forwarded data, so the request domain needs to be used.

(2) Httpsession domain (session domain)

1) Scope actual size. (What is it?)

The scope of Httpsession is: a session.

After the data is generated, it is displayed to the user. If it needs to be used later, use the Httpsession field.

  它是四个域中范围第二大的域。

2)  作用域的作用。(有什么用?)

  (会话范围)在第一次调用request.getSession()方法时,服务器会检查是否已经有对应的session。如果没有,就在内存中创建一个session并返回。当一短时间内(默认30分钟)session没有被使用,则服务器会销毁该session。若服务器非正常关闭,未到期的session也会跟着销毁。若调用session提供的invalidate()方法,可以立即销毁session。

3)怎么使用这些作用域。(怎么用?)

  a) jsp中操作session
    (String)request.getSession().getAttribute("username"); // 获取
    request.getSession().setAttribute("username", "xxx");  // 设置

   b) java中操作session
    //servlet中
    request.getSession();
    session.getAttribute("username");
    session.setAttribute("username", "xxx");
    session.setMaxInactiveInterval(30*60);
    session.invalidate();
 
    //struts中方法1
    ServletActionContext.getRequest().getSession().setAttribute("username", "xxx");
    ServletActionContext.getRequest().getSession().getAttribute("username");
    ServletActionContext.getRequest().getSession().setMaxInactiveInterval(30*60);
    ServletActionContext.getRequest().getSession().invalidate();

    //struts中方法2
    ActionContext.getContext().getSession().put("username", "xxx");
    ActionContext.getContext().getSession().get("username");
    ActionContext.getContext().getSession().clear();

   c) web.xml中操作session
    <session-config>          <session-timeout>30</session-timeout>      </session-config>   d) tomcat-->conf-->conf/web.xml
    <session-config>        <session-timeout>30</session-timeout>    </session-config>

4)它这样使用实现的原理。(为什么可以这么用?)

  HttpSession在服务器中,为浏览器创建独一无二的内存空间,在其中保存了会话相关的信息。服务器创建session出来后,会把session的id号,以cookie的形式回写给客户机,这样,只要客户机的浏览器不关,再去访问服务器时,都 会带着session的id号去,服务器发现客户机浏览器带session id过来了,就会使用内存中与之对应的session为之服务。如果要问我为什么,我也不知道啊!

(三)ServletRequest域(request域)

1)作用域的实际大小。(是什么?)

  ServletRequset域是:整个请求链(请求转发也存在)。

  数据产生之后,只需要使用一次,这种情况下请使用ServletRequset域。

 It is the third largest domain among the four domains.

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

Sharing data throughout the request chain.

Most commonly used: the data processed in the servlet is handed over to JSP for display. At this time, the parameters can be placed in the ServletRequset field and brought over.

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

 a) Method of obtaining client information
  The getRequestURL method returns the complete URL when the client makes a request.
The getRequestURI method returns the resource name part of the request line.
  The getQueryString method returns the parameter part in the request line.
The getRemoteAddr method returns the IP address of the client that made the request
The getRemoteHost method returns the complete 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 WEB The IP address of the server.
GetLocalName methodReturns the host name of the WEB server
GetMethod gets the client request method
b) Gets the client request header
getHeader( string name) method
  getHeaders(String name) method
  getHeaderNames method
c) Get clientRequest parameters(data submitted by the client)
 getParameter(name) method
GetParameterValues(String name) method
getParameterNames method
getParameterMapmethod

4) It uses the principle of implementation like this. (Why can it be used in this way?)

Created by the server before the service method is called, and passed into the service method. The entire request ends and the request life ends.

 

(四)PageContext域(page域)

1)作用域的实际大小。(是什么?)

  PageContext域的作用范围是:整个JSP页面。

  它是四个域中范围最小的一个域。

2)  作用域的作用。(有什么用?)  
  a) 它可以获取其它八大隐式对象,可以认为它是一个入口对象。

  b) 获取其它所有域中的数据。

  c) 跳转到其它资源。其身上提供了forword和sendRedirect方法,简化了转发和重定向的操作。 

3)怎么使用这些作用域。(怎么用?)

  以下以一个简单的JSP页面程序为例:

<%@ 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"><html><head><title>pageContext对象_例1</title></head><body><br><%//使用pageContext设置属性,该属性默认在page范围内
pageContext.setAttribute("name","地球");
request.setAttribute("name","太阳系");
session.setAttribute("name","银河系");//session.putValue("name","麦哲伦系");
application.setAttribute("name","宇宙");%>page设定的值:<%=pageContext.getAttribute("name")%><br>request设定的值:<%=pageContext.getRequest().getAttribute("name")%><br>session设定的值:<%=pageContext.getSession().getAttribute("name")%><br>application设定的值:<%=pageContext.getServletContext().getAttribute("name")%><br>范围1内的值:<%=pageContext.getAttribute("name",1)%><br>范围2内的值:<%=pageContext.getAttribute("name",2)%><br>范围3内的值:<%=pageContext.getAttribute("name",3)%><br>范围4内的值:<%=pageContext.getAttribute("name",4)%><br><!--从最小的范围page开始,然后是reques、session以及application--><%pageContext.removeAttribute("name",3);%>pageContext修改后的session设定的值:<%=session.getValue("name")%><br><%pageContext.setAttribute("name","宇宙",4);%>pageContext修改后的application设定的值:<%=pageContext.getServletContext().getAttribute("name")%><br>值的查找:<%=pageContext.findAttribute("name")%><br>属性name的范围:<%=pageContext.getAttributesScope("name")%><br></body></html>

显示结果:

page设定的值:地球
request设定的值:太阳系
session设定的值:银河系
application设定的值:宇宙
范围1内的值:地球
范围2内的值:太阳系
范围3内的值:银河系
范围4内的值:宇宙
pageContext修改后的session设定的值:nullpageContext修改后的application设定的值:宇宙
值的查找:地球
属性name的范围:1

4)它这样使用实现的原理。(为什么可以这么用?)

 pageContext object, this object represents the page context. This object is mainly used to access shared data between JSPs. Started when a request is made to the JSP and destroyed when the response is completed.

The above is the detailed content of The most complete summary of the four major domains of Java (pictures and texts). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn