Java Four domains

Source: Internet
Author: User

根据有作用范围由小到大:

page(jsp有效)------》page域指的是pageContext.request(一次请求)---》request域request  HttpServletRequestsession(一次会话)---》session域session HttpSessionapplication(当前web应用)---》application域指的是application  ServletContext;之所以他们是域对象,原因是他们都内置了map集合,都有setAttribute和getAttribute方法。一、PageContext域1.生命周期:当对JSP的请求开始,当相应结束时销毁。           jsp页面被执行,声明周期开始;           jsp页面执行完毕,声明周期结束;2.作用范围:整个JSP页面,是四大作用域中最小的一个。3.作用:   (1)获取其它八大隐式对象,可以认为是一个入口对象。    (2)获取其所有域中的数据    pageContext  操作所有域中属性的方法    publicjava.lang.Object getAttribute(java.lang.String name,intscope)                   publicvoid setAttribute(java.lang.String name, java.lang.Object value,intscope)    publicvoidremoveAttribute(java.lang.String name,intscope)          pageContext 中代表域的常量       PageContext.APPLICATION_SCOPE       PageContext.SESSION_SCOPE       PageContext.REQUEST_SCOPE       PageContext.PAGE_SCOPE          findAttribute方法,在四大域中搜寻属性,搜寻的顺序是page域、request域、session域、application域,从小域到大域开始搜索,如果搜索到就直接获取并且不会再往下寻找,如果所有域中都找不到,返回一个null(与el表达式不同,此处返回null,对网页是不友好的)          (3)跳转到其他资源      其身上提供了forward和include方法,简化重定向和转发的操作二、Request域1.生命周期:    在Service方法调用前由服务器创建,传入service方法。整个请求结束,request生命结束。    用户发送一个请求,开始,服务器返回响应,请求结束,生命周期结束;2.作用范围:整个请求链(请求转发也存在)3.作用:在整个请求链中共享数据,经常用到:在servlet中处理好的数据交给JSP显示,此时参数就可以放在Request域中。三、HttpSession 域1.生命周期:    在第一次调用request.getSession()方法时,服务器会检查是否已经有对应的session,如果没有就在内存中创建一个session并返回。(1)当一段时间内session没有被使用(默认为30分钟),则服务器会销毁该session。(2)如果服务器非正常关闭,没有到期的session也会跟着销毁。(3)如果调用session提供的invalidate(),可以立即销毁session。用户打开浏览器访问,创建session(开始),session超时或者被声明失效,该对象生命周期结束;2.作用范围:一次会话。HttpSession 在服务器中,为浏览器创建独一无二的内存空间,在其中保存会话相关的信息注意:服务器正常关闭,再启动,Session对象会进行钝化和活化操作。同时如果服务器钝化的时间在session 默认销毁时间之内, 则活化后session还是存在的。否则Session不存在。  如果JavaBean 数据在session钝化时,没有实现Serializable 则当Session活化时,会消失。四、ServletContext1.生命周期:    当WEB应用被加载进容器创建代表整个WEB应用的ServletContext对象;    当服务器关闭或WEB应用被移除时,ServletContext对象跟着被销毁。2.作用范围:整个WEB应用。3、作用: a)在不同Servlet 之间转发  this.getServletContext().getRequestDispatcher("/servlet/Demo10Servlet").forward(request,response); 方法执行结束,service就会返回到服务器,再有服务器去调用目标servlet,其中request会重新创建,并将之前的request的数据拷贝进去。    b)读取资源文件。1、由于相对路径默认相对的是java虚拟机启动的目录,所以我们直接写相对路径将会是相对于tomcat/bin目录,所以是拿不到资源的。如果写成绝对路径,当项目发布到其他环境时,绝对路径就错了。  为了解决这个问题ServletContext提供了:this.getServletContext().getRealPath("/1.properties"),给进一个资源的虚拟路径,将会返回该资源在当前环境下的真实路径。        this.getServletContext().getResourceAsStream("/1.properties"),给一个资源的虚拟路径返回到该资源真实路径的流。  2、当在非servlet下获取资源文件时,就没有ServletContext对象用了,此时只能用类加载器   classLoader.getResourceAsStream("../../1.properties"),此方法利用类加载器直接将资源加载到内存中,有更新延迟的问题,以及如果文件太大,占用内存过大。   classLoader.getResource("../1.properties").getPath(),直接返回资源的真实路径,没有更新延迟的问题。

Similarities and differences of ServletContext and application

Same: In fact ServletContext and application are the same, it is equivalent to a class created two different names of variables. In

The ServletContext in the servlet is the Application object. All you have to do is open the JSP in the generated servlet after compilation

The _jspservice () method can see the following declaration:
Servletcontextapplication = null;

Application= Pagecontext.getservletcontext ();

Differences: The difference between the two is that application is used in JSPs and ServletContext is used in Servlets. Application and Page

Requestsession are built-in objects in JSPs, and property data stored in the background with ServletContext can be used

The Application object is obtained.

And the scope of the application is the entire Tomcat boot process.
For example: Servletcontext.setattribute ("username", username);
You can use Application.getattribute ("username") in the JSP Web page;
Come to this user name.

Java Four domains

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.