A simple example of the 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;
}
}
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:
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 to be placed in the <server root>\web-inf\classes\ count\ counter.class.
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.