Original page: http://www.douban.com/note/102320977/
JavaBean is a reusable, cross-platform component. There are two kinds of javabean: a non-user interface, which is generally used for processing data operations, operating databases and so on. The other is a JavaBean with user interface (Interface). General access to JavaBean with no user interface in JSP.
Access JavaBean syntax in JSP:
1. Use the page directive to import the JavaBean class:
<% @page import= "package name. Class name "%>
2. Use the <jsp:useBean> tag to declare JavaBean objects:
<jsp:usebean id= "Instance name" class= "package name. Class name "scope=" Scope "/>
Note: To give the full (package name). Class name), otherwise reported classnotfoundexception
Example: <jsp:usebean id= "bean" class= "com. Countbean "scope=" session "/>
The above code is equivalent to:
Countbean bean = new Countbean ();
session.setattribute ("Bean", bean);
Note: If the JavaBean already exists within scope's specified range, <jsp:useBean> does not generate a new JavaBean object, but instead directly obtains a reference to the JavaBean object that already exists.
3. Access JavaBean properties using <jsp:getProperty> and <jsp:setProerty> tags:
<jsp:getproperty name= "Instance name" property= "attribute name in Bean" >
Call the Get method in the bean to get the value of a property
<jsp:setproperty name= "Instance name" property= "attribute name in Bean" >
Call a set method in a bean to assign a value to a property
1.JavaBean in the page scope:
In this case, each time a customer accesses a JSP page, a JavaBean object is created, valid only in the current JSP page.
The result of the example:
Count Values is:0
Scope = page
At this point JavaBean will end the life cycle in the following two scenarios
(1) When a customer accesses the current JSP page and forwards the request to another file via ForWord
(2) When the client accesses the current JSP page execution completes and echoes to the client
2.JavaBean in the request scope:
As with page, each time a customer accesses a JSP page, a JavaBean object is created.
The result of the example:
Count Values is:0
Scope = Request
At this point, when the client accesses the current JSP page execution finishes and sends a response back to the client, JavaBean ends the lifecycle
3 JavaBean in Session scope:
After the JavaBean object is created, it persists in the current session, sharing the same JavaBean
Example results:
Count Values is:0
(up to 1 per refresh, when another browser window is opened to re-issue the request, it is not in the same session, a new session is created, starting from 0 and growing again.) )
Scope = Session
4.JavaBean in the application scope
After the JavaBean object is created, it persists in the life cycle of the current Web application, in which the same JavaBean is shared
Example results:
Count Values is:0
(up to 1 per refresh, when another browser window is opened to re-issue the request, it is also in the same Web application, from the original basis)
Note: By default, the properties of the session in the JSP are true
For example: JavaBean action Range is the case of the session!
a.jsp
.................
<jsp:usebean id= "dbmsg" class= "Com.bean.SQLBean" scope= "session"/>
<%
Initialization
Dbmsg.getconncetion ("Org.gjt.mm.mysql.Driver", "Jdbc:mysql://localhost/ss", "Lala", "haha");
Dbmsg.getstatement (resultset.type_scroll_insensitive, resultset.concur_read_only);
%>
<jsp:include page= "b.jsp"/>
.............
b.jsp
................
<jsp:usebean id= "dbmsg" class= "Com.bean.SQLBean" scope="session"/>
Dbmsg.doing ();
................
JavaBean and JSP applications