This tutorial will explain the principles of JavaBean, then explain the specific syntax of JavaBean under JavaServer Page, then demonstrate a simple counter using Jsp+JavaBean, and finally explain in detail a user registration program of JavaBean+jsp with database function.
Java Bean Principles and Applications
1. What is JavaBean?
JavaBean is a software component model that describes Java, somewhat similar to Microsoft's COM component concept. In the Java model, the functions of Java programs can be infinitely expanded through JavaBeans, and new applications can be quickly generated through the combination of JavaBeans. For programmers, the best thing is that JavaBeans can realize code reuse. In addition, it also has great significance for the ease of program maintenance and so on.
JavaBeans can be executed correctly through the Java Virtual Machine. The minimum requirement for running JavaBeans is JDK1.1 or above.
The traditional application of JavaBean lies in the field of visualization, such as applications under AWT. Since the birth of Jsp, JavaBean has been used more in non-visual fields and has shown increasing vitality in server-side applications. Here we mainly discuss non-visual JavaBeans. Visual JavaBeans are explained in detail in many Java books on the market, so they will not be the focus here.
2. Non-visual JavaBean
Non-visual JavaBean, as the name implies, is a JavaBean without a GUI interface. It is often used in Jsp programs to encapsulate transaction logic, database operations, etc., which can effectively separate business logic and front-end programs (such as jsp files), making the system more robust and flexible.
A simple example, such as a shopping cart program, to implement the function of adding an item to the shopping cart, you can write a JavaBean for shopping cart operation, create a public AddItem member method, and call this directly in the front-end Jsp file method to achieve. If we later consider adding goods, we need to determine whether there are goods in inventory. If there are no goods, we cannot purchase them. At this time, we can directly modify the AddItem method of JavaBean and add processing statements to achieve it. This way, there is no need to modify the front-end jsp program.
Of course, you can also write all these processing operations in a jsp program, but such a jsp page may have hundreds or thousands of lines. Just reading the code is a headache, let alone modifying it. If you have used asp to develop programs, I believe you have a deep understanding of this? The SP+COM component can completely implement the same architecture as jsp+javabean, but for some reason, it is common on the Internet to write all In the ASP page, it makes maintenance and modification extremely inconvenient. Of course, this is off topic). It can be seen that through JavaBean, logic encapsulation, easy program maintenance, etc. can be well realized.
If you use Jsp to develop programs, a good habit is to use more JavaBeans.
3. Simple example of JavaBean
Creating a JavaBean is not a difficult task. If you have written a Java program, it will be very easy. One thing to note is that in non-visual JavaBeans, member methods such as >get or >set are commonly used. to handle properties>(properties>). >
Let's take a look at a simple JavaBean
FirstJavaBean.java import java.io.*; public class FirstJavaBean { private String FirstProperty = new String(""); public FirstJavaBean() { } public String getFirstProperty() { return FirstProperty; } public void setFirstProperty(String value) { FirstProperty = value; } public static void main(String[] args) { System.out.println("My First JavaBean!"); } } |
If you run this program, the following results will appear:
First JavaBean!
This is a very typical representative of JavaBean. Briefly explain, FirstProperty is one of the properties (Property), and this property can be set externally through the get/set method. Carry out the operation. If you have written VB classes, you will no longer be familiar with this. The Main method is used to test the program. When writing a JavaBean, you can directly use the main method to debug without adding it to the Jsp program. After debugging, you can call it in the Jsp program. (Unfinished)