Test. javapackage test; import Java. util. date; import Java. text. simpledateformat; public class test {date d = new date (); Public test () {// super (); system. out. println ("in constructor"); // todo auto-generated constructor stub} Public String getdatetime () {system. out. println ("in the instance method"); simpledateformat format = new simpledateformat ("yyyy-mm-dd-hh-mm-SS"); string now = format. format (d); return now ;}}
Index. JSP <% @ page Language = "Java" contenttype = "text/html; charset = gb2312" %> <JSP: usebean id = "Tober" class = "test. test "Scope =" page "/> <% = Tober. getdatetime () %>
OK. Next we will start testing 1. when the scope is page or request, every time I refresh the page or open a new page, the page always displays the latest time, in the "Constructor method" and "in the instance method", this means that when the scope is page or request, every time a page is loaded, a constructor is called to generate an instance of the bean, which leads to changes in the time on the page. (Because when calling the constructor method, it will execute date d = new date () to generate a new time) 2. sessionok. Now I want to change scope to session, refresh the page, or open a new page to see what will happen. At this time, you will see that the time for refreshing the page remains unchanged, but if you open a new page, the time for entering the new page is different from the previous page, but the refresh will not change. In addition, when you enter the page, "constructor" and "in instance method" will appear at the same time, and then refresh the page, only "in instance method" will appear. This indicates that when scope is a session, the bean constructor in the same session will be called only once, this is why the time for refreshing a page does not change and the time for opening a new page changes (because only when the constructor is called will the date d = new date () be executed (), to generate a new time ). 3. Change scope to application in applicationlast. At this time, I found that no matter how I refresh the page or open a new page, the time does not change after entering the page ,:(. The time will only change when I restart the server and then enter it again, but the time will not change until the server is restarted, and the background only outputs "in the instance method ", this indicates that when scope is application, the bean constructor is called only once during one operation on the server, so the time remains unchanged. (Because Date d = new date () is executed only when the constructor is called to generate a new time ). Finally, let's perform another test to move date d = new date () to the getdatetime () instance method. At this time, we find that no matter what the scope is, each time you refresh the page or re-enter a new page, the time changes.
OK. Let's start our test.
1. Page/Request
When scope is page or request, every time I refresh the page or open a new page, the page always displays the latest time, in the "Constructor method" and "in the instance method", this means that when the scope is page or request, every time a page is loaded, a constructor is called to generate an instance of the bean, which leads to changes in the time on the page. (Because when calling the constructor, it will execute date d = new date () to generate a new time)
2. Session
OK. Now I want to change scope to session, refresh the page, or open a new page to see what will happen. At this time, you will see that the time for refreshing the page remains unchanged, but if you open a new page, the time for entering the new page is different from the previous page, but the refresh will not change. In addition, when you enter the page, "constructor" and "in instance method" will appear at the same time, and then refresh the page, only "in instance method" will appear. This indicates that when scope is a session, the bean constructor in the same session will be called only once, this is why the time for refreshing a page does not change and the time for opening a new page changes (because only when the constructor is called will the date d = new date () be executed (), to generate a new time ).
3. Application
Last, change scope to application. At this time, I found that no matter how I refresh the page or open a new page after entering the page, the time does not change ,:(. The time will only change when I restart the server and then enter it again, but the time will not change until the server is restarted, and the background only outputs "in the instance method ", this indicates that when scope is application, the bean constructor is called only once during one operation on the server, so the time remains unchanged. (Because Date d = new date () is executed only when the constructor is called to generate a new time ).
Finally, let's perform another test to move date d = new date () to the getdatetime () instance method. At this time, we find that no matter what the scope is, each time you refresh the page or re-enter a new page, the time changes.
Summarize the function of bean scope: Each execution
<JSP: usebean id = "Tober" class = "test. test "Scope =" ×× "/>, the server will look for an instance of this type in the corresponding scope. If yes, it will be reused, and if no, it will be re-created. The session is used as an example. When I first enter the page, the server creates a test named tolobby. test class instance, and put it into the session. Therefore, in the same session, the constructor of this class will no longer be called to create a tolobby instance of this class, because it already exists, unless we use session. removeattribute ("Toby") killed it. Other scopes can be interpreted in analogy.