The scope of usebean actions in JSP can be used to determine the differences between four scopes (page, request, session, application ).

Source: Internet
Author: User


The example is simple. The bean function is to get the current time, and its code is:
Common. Java:
--------------------
Package xx;

Import java. util. date;
Import java. Text .*;

Public class common
{
Date d = new date ();
Public String now (string S)
{
Simpledateformat formatter = new simpledateformat (s );
Return formatter. Format (d );
}
}

The Code on the test page is:
Date. jsp
-----------------------------------------------------------
<JSP: usebean id = "cm" Scope = "XX" class = "XX. Common"/>
<% = Cm. Now ("yyyy-mm-dd hh: mm: SS") %>

Test results:

1. When scope = application, we browse date. jsp, and the system time is displayed. However, no matter how we refresh, re-open the browser, or even change the server, the display time remains the same, it is the original time (that is, the system time obtained when bean was just created ), because scope = application, the bean instance has only one copy in the memory. At this time, as long as the Web service is not restarted, the output will not change.

2. When scope = session, browsing date. jsp will not change during refresh. However, when we re-open a browser, that is, a new session, the system will create a bean instance again to get the current system time, and then get the correct time. Similarly, refresh the new page (new session) and the display will not change.

3. When scope = page/Request (there is no difference between them only when static files are included), the page will be refreshed continuously to get the current system time.

The above shows that different scopes and bean have different scopes. Make sure that you do not set the scope of bean that is frequently changed to application or session. This will produce incorrect results.

Page, that is, this bean is only valid on this page. For example, if you change a page or refresh it, the bean will be re-instantiated.

Request, bean is valid only during the request

Session, bean is valid in a session

Application, as long as your web server is not stopped, it will always be valid.

To put it bluntly, the page is only valid for the current page. For the request, you can use JSP: forward to let the request be transmitted in several servlets or JSPs, so the page range

The valid range of bean and ID variable name. The default value is page, which is described as follows:

A) page: You can use the bean in the JSP file containing the <JSP: usebean> element and all static inclusion files in the file, wait until the page execution is complete and send a response to the client or go to another file.

B) Request: You use bean in any JSP file that executes the same request until the page execution is complete and sends a response to the client or transfers it to another file. You can use the request object to access Bean

C) Session: from bean creation, bean can be used in any JSP file using the same session. This bean exists throughout the session lifecycle. Any JSP file that shares this session can use the same bean. Note that session = true must be specified in the <% @ Page %> command of the JSP file of the bean you created.

D) Application: from bean creation, you can use bean in any JSP file using the same application. This bean exists throughout the application lifecycle, and any JSP file sharing this application can use the same bean.

Example of a JavaBean scope = "request"

Request1.jsp File
<% @ Page contenttype = "text/html; charset = gb2312" Language = "Java" Import = "Java. util. *, test. testbean" %>
<HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Title> request1 </title>
</Head>
<Body>
<JSP: usebean id = "test" class = "test. testbean" Scope = "request"/>
<%
Out. println (test. getcount ());
Request. setattribute ("test", test );
%>
<JSP: Forward page = "request2.jsp"/>
</Form>
</Body>
</Html>

Request2.jsp File
<% @ Page contenttype = "text/html; charset = gb2312" Language = "Java" Import = "Java. util. *, test. testbean" %>
<HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Title> request2 </title>
</Head>
<Body>
<%
Out. println (testbean) request. getattribute ("test"). getcount ());
%>

</Body>
</Html>

Bean File
Package test;
Public class testbean
{
Private int count;

Public int getcount (){
Return count ++;
}
}

Related Article

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.