Jsp+javabean step-by-Step tutorial (i)

Source: Internet
Author: User
JSP as a very good dynamic web development language has been more and more widely used in all kinds of JSP applications, JSP + Java Bean portfolio has become a fact the most common JSP program standards, let us see how the specific JSP is a combination of Java bean.

This tutorial will explain the principles of JavaBean, then explain the specific syntax of JavaBean under JavaServer Page, and then demonstrate a simple counter using Jsp+javabean, which will explain in detail a database-enabled javabean+ JSP's user registration program.

Java Bean Principles and applications

1, what is JavaBean?

JavaBean is a software component model that describes Java, somewhat similar to the Microsoft COM component concept. In the Java model, you can extend the functionality of the Java program infinitely by JavaBean, and you can quickly generate new applications through the combination of JavaBean. For programmers, the best point is that JavaBean can achieve the reuse of code, in addition to the ease of maintenance of the program, and so also have great significance.

JavaBean can be executed correctly through the Java Virtual machine (Java Machine), and the minimum JavaBean requirement is JDK1.1 or above.

The traditional application of JavaBean is in the field of visualization, such as the application of AWT. Since the birth of JSP, JavaBean more applications in the field of Non-visual, server-side applications in the performance of more and more strong vitality. Here we mainly discuss the JavaBean, visualization of the JavaBean in the market there are many Java books are elaborated in detail, here is not the focus.

2, non-visualization of the JavaBean

Non-visual JavaBean, as the name implies is no GUI interface JavaBean. In the JSP program commonly used to encapsulate transaction logic, database operation and so on, can well realize the business logic and foreground program (such as JSP file) separation, make the system has better robustness and flexibility.

A simple example, such as a shopping cart program, to achieve the shopping cart to add a product such a function, you can write a shopping cart operation JavaBean, set up a public AddItem member method, the front desk JSP file directly call this method to achieve. If you later consider adding goods when you need to determine whether there are goods inventory, no goods can not buy, at this time we will be able to directly modify the JavaBean AddItem method, add processing statements to achieve, so there is no need to modify the foreground JSP program.

Of course, these processing operations can also be completely written in the JSP program, but such a JSP page may have hundreds of rows, just look at the code is a headache, let alone modified. If you have used ASP to develop a program, I believe that this is a deep understanding? 涫 Stool 肁 sp+com components can achieve the same structure as Jsp+javabean, but do not know for some reason on the Internet are all commonly written in the ASP page, so that maintenance changes such as the extremely inconvenient, of course, this is a digression. This shows that through JavaBean can be a good way to achieve the logic of encapsulation, easy maintenance of the program and so on.

If you use the JSP development program, a good habit is to use JavaBean more.

3, simple example of JavaBean

Creating JavaBean is not a difficult task, and if you've written Java programs that's easy, it's important to note that in Non-visual JavaBean, member methods such as >get or >set are used to handle attributes > Properties >). >

Now let's 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 A-javabean!");

}

}

If you run this program, the following results will appear:

The javabean!

This is a very typical JavaBean representative, simply explained, Firstproperty is one of the properties, the external through the Get/set method can operate on this property, if you have written VB class, It's a familiar one. The main method is to test the application, write JavaBean can not add to the JSP program to call, and directly with the main method to debug, after debugging can be called in the JSP program.
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.