A simple example of using JavaBean in JSP

Source: Internet
Author: User

After using JavaBean in JSP, we can realize the separation of HTML code and Java code, it is easier for JSP to develop and maintain. So JavaBean became a necessary tool for JSP programmers.
Although JavaBean is a Java class, it also has some of its own features
(1) JavaBean is a public class
(2) JavaBean has a construction method with no parameters
(3) The properties in JavaBean are manipulated by the get and set methods

For example: Define a circlearea of the JavaBean used to encapsulate the circle area of the calculation logic, after all the use of the circle area of the calculation method can be used to complete the javabean, the implementation of code reuse.

Circlearea.java


Package javabeantest;

public class Circlearea {
Define two properties (r: RADIUS; unit: Units)
Private double r=0;
Private String unit= "";
Non-parametric construction method
Public Circlearea () {
Super ();
}
Public double Getr () {
return R;
}
public void Setr (double r) {
THIS.R = R;
}
Public String Getunit () {
return unit;
}
public void Setunit (String unit) {
This.unit = unit;
}
Calculates the area of a circle and returns the calculated result
Public String Getarea () {
return 3.14*r*r+ "Square" +unit;
}
}


Use JavaBean in the JSP.


<%@ page language= "java" import= "java.util.*" pageencoding= "Utf-8"%>
<!--use JavaBean in JSP generally follow the following four steps
(1) class for importing JavaBean
(2) Instantiate a JavaBean object within the specified range, and if the object already exists, direct reference
(3) operation of the JavaBean instance object
(4) Removal of JavaBean from the specified range
-
<%@ page language= "java" import= "java.util.*" pageencoding= "Utf-8"%>
<% @pageimport= "Javabeantest.circlearea"%>
<jsp:usebean id= "Circle1" class= "Javabeantest.circlearea" scope= "page"/>
<jsp:usebean id= "Circle2" class= "Javabeantest.circlearea" scope= "Request"/>
<jsp:usebean id= "Circle3" class= "Javabeantest.circlearea" scope= "Session"/>
<jsp:usebean id= "circle4" class= "Javabeantest.circlearea" scope= "Application"/>

The application of <title>javabean in JSP </title>

<body>
<%
Invoke the JavaBean in the page range
CIRCLE1.SETR (4);
Circle1.setunit ("M");
Out.println (Circle1.getarea ());
Out.println ("<br/><br/>");
Call the JavaBean within the request scope
CIRCLE1.SETR (6);
Circle1.setunit ("M");
Out.println (Circle1.getarea ());
Out.println ("<br/><br/>");
Call the JavaBean within the session range
CIRCLE1.SETR (8);
Circle1.setunit ("M");
Out.println (Circle1.getarea ());
Out.println ("<br/><br/>");
Call the JavaBean in the application range
CIRCLE1.SETR (9);
Circle1.setunit ("M");
Out.println (Circle1.getarea ());
Out.println ("<br/><br/>");
%>
<%
Remove the Circle1 from the page range
Pagecontext.removeattribute ("Circle1");
Remove the circle2 from the request scope
Request.removeattribute ("Circle2");
Remove the Circle3 from the session range
Session.removeattribute ("Circle1");
Remove Circle4 from the application range
Application.removeattribute ("Circle1");
%>
</body>





A simple example of using JavaBean in JSP

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.