First, initialization of JavaBean
1, in the JSP
The following procedure is executed in the code in the previous paragraph:
? Look for the javabean of the same ID according to the parameters of scope, if found, jump out of it;
? If not found, an instance is created at the position of the scope parameter and the initialization statement is executed and in;
In other words, if you are sure you want to set the Beanname in the current state of the scope parameter position, avoid using the code like the above, and you should put the statement outside.
2, in the Serlvlet
Package.classname beanname=new package.classname ();
Then place the beanname in a different position;
Ii. location and operation of JavaBean
It is said here that the location of the JavaBean is mainly for different scopeparameter, so it will be discussed here separately.
The JSP servlet reads and writes (the servlet lists only the acquisition or set reference, the setxxx and getxxx that omit the specific attributes)
JSP Servlet
Scope= "Page" Request.pagecontext
Or
Direct reference
Scope= "Request" Request Package.classname beanref= (package.classname) request. GetAttribute ("Beanname");
Or
Request.setattribute ("Beanname", beanref);
Scope= "Session" HttpSession HttpSession session= request.getsession (true);
Package.classname beanref = (package.classname) session.getattribute ("Beanname");
Or
Session.setattribute ("Beanname", beanref);
Scope= "Applicaton" ServletContext package.classname beanref= getservletcontext (). getattribute ("Beanname");
Or
Getservletcontext (). setattribute ("Beanname", beanref);
Third, the specific application
Type Application
Scope= The default setting of "page" in a JSP, similar to a variable instance within a page range.
Scope= "Request" A variable instance within the valid range of a request.
Scope= "Session" within a client access scope variable instances, each access is different.
Scope= "Applicaton" is a variable instance within the scope of the server program, similar to static, valid for each client access.