js| Tutorials A simple example of a combination of JSP and JavaBean
The above said so much, until now have not seen the concrete application, that good, now we look at the specific JavaServer Pages+javabean example, let's first look at a simple counter program.
This routine contains a total of 3 files
Javabean--counter.java files, JavaServer page--counter.jsp files, counter1.jsp files where Counter.java is used primarily for counter counting operations, counter.jsp and counter1.jsp files are used primarily to display the count of pages.
Counter.java file
Package count;
/**
* Title:test
* Description:counter Bean
* @author Liuyufeng
* @version 1.0
*/
public class Counter {
Initialize the JavaBean member variable
int count = 0;
Class Builder
Public counter () {
}
Get method of Property count
public int GetCount () {
Count operation, each time the request is carried out counter plus a
count++;
return this.count;
}
Set method for attribute count
public void SetCount (int count) {
This.count = count;
}
}
counter.jsp file
<HTML>
<HEAD>
<TITLE>
Counter
</TITLE>
</HEAD>
<BODY>
<H1>
JBuilder generated JSP
</H1>
<!-Initialize counter This bean, instance is bean0-->
<jsp:usebean id= "Bean0" scope= "Application" class= "Count.counter"/>
<%
Displays the value of the current property count, which is the value of the counter, where we use the Out.println method, and the following counter1.jsp will use another method
Out.println ("The Counter is:" + bean0.getcount () + "<BR>");
%>
</BODY>
</HTML>
counter1.jsp file
<HTML>
<HEAD>
<TITLE>
Counter
</TITLE>
</HEAD>
<BODY>
<H1>
JBuilder generated JSP
</H1>
<!-Initialize counter This bean, instance is bean0-->
<jsp:usebean id= "Bean0" scope= "Application" class= "Count.counter"/>
<!-uses the jsp:getproperty tag to get the value of the Count property, which is the value of the counter-->
The Counter is:
<jsp:getproperty name= "Bean0" property= "Count"/><br>
</BODY>
</HTML>
From this example we are not difficult to see JSP and JavaBean application of the general operation method, first in the JSP page to declare and initialize JavaBean, this javabean has a unique ID flag, there is a scope of survival ( Set to application is to enable multiple users to share a counter function, if you want to achieve a single user count, you can modify scope for session, and finally to develop JavaBean class source Count.counter:
<jsp:usebean id= "Bean0" scope= "Application" class= "Count.counter"/>
We can then use the public method provided by JavaBean or use the <jsp:getProperty> tag directly to get the value of the property in the JavaBean:
Out.println ("The Counter is:" + bean0.getcount () + "<BR>");
Or
<jsp:getproperty name= "Bean0" property= "Count"/>
OK, now run the program to see, and then refresh several times, pay attention to the changes in the counter. The above program is debugged and passed under Jbuilder4.0.
If you want to debug directly in some JSP environments (such as Tomcat, IAS, WebLogic, etc.), be aware of your respective documentation and place the JavaBean file correctly. As in the Tomcat environment, this example javabean the compiled file with <server root>web-infclasses count Counter.class.