What is JavaBean and how to summarize the two ways to use JavaBean

Source: Internet
Author: User

After reading this after no longer tangled javabean is what thing, thank Bo Master, because is Javablog can't collect in this forwarding.

The following is transferred from: http://www.blogjava.net/flysky19/articles/88180.html

What is JavaBean?

Javabean is a class that defines a series of get<name> and Set<name> methods. So simple!

Javabean is to communicate with the JSP page data simplification interaction process generated.

Own understanding of:

The difference between using JavaBean and without JavaBean:

One JavaBean is to create a space for element one by one on the JSP page, and then assign and read values as needed. Instead of JavaBean, use the HttpServlet request object directly:

Get data is only passed between JSP pages:

String name=req.getparameter ("name");

Display data: out.println (name);

Save data into DB: Embed Java code directly in JSP and deposit with INSERT statement.

It felt like the ASP code that I had built myself. asp page A series of Req.getparameter code and embedded a lot of SQL statements, code is very messy, difficult to maintain.

With JavaBean, the advantage is Java's Advantage: component technology, code reuse, and ease of maintenance. (The display of the data in the JSP page is clear.) )

What is the specific JavaBean? Information to be found

Solve:

1) http://www.itlearner.com/article/2004/551.shtml

Software components are parts of software that can be isolated and easily reused. JavaBean is a software component idea based on the Java platform.

2) http://www.chinaitpower.com/A200507/2005-07-27/170233.html

JavaBean, what the hell is a thing?

1. JavaBean and EJB
(1) The JavaBean and EJB specifications share a common goal: To promote Java program code through a standard design pattern, and to enhance the portability of reuse between the development process and development tools. But the original problem with these two specifications is to solve different problems.
The standard specification, defined in the JavaBean component model, is designed to produce reusable components that are often used in IDE development tools and are usually visual components, not necessarily visual components.
(2) The EJB specification defines the component modeler, which is just the Java program used to develop the server side, because EJBS can be executed on different servers, including large, non-graphical hosts, so EJB cannot use a graphical library like AWT or swing.

2. differences between JavaBean and class libraries:
(1) Not all useful modules must be converted into beans. Beans can be used to visualize operations to accomplish some of the effects.
(2) A class library is a valid operation that provides a function operation to the programmer, but does not benefit from visual manipulation.
(3) For example: The API that provides JDBC database access as a class cubby is better as a bean, because the core of JDBC is to use a programmable API and not be able to directly visualize the operation. However, it is still meaningful to write a library access bean at the top level of JDBC. For example, you can write a custom "select" Bean to help the user make a query statement, and the program runs with JDBC to run the query and display the result set.

3. in visual programming, it can be seen that a component is a piece of program code that, by being saved as a class, is key in how the program development tool discovers the properties and events of the component. Both VB and Delphi implement this through some mechanism, while Java uses JavaBeans to bring visual component development to a higher level, because a bean is a class. For the above content, JavaBean has the following rules:

(1) for a property named XXX, you typically write two functions, getXxx () and setxxx ().
(2) You can use the Get/set method to handle the Boolean, you can use is to replace get.
(3) The generic function of a bean does not need to follow the naming conventions described above, but it needs to be public.
(4) For events, you can use swing's listener practices to handle them.

4. JavaBean Architecture :
The JavaBean architecture is one of the first comprehensive component-based standard models. Enables JavaBean to be manipulated at design time in an integrated IDE. The component model is shown as:

Combined with the information to get their own experience and summary:

(after all the problems are Google and Baidu, alas, this problem last year when the Java EE class, asked a lot of students, including Su, they did not have a very good answer. )

1. JavaBean is a software component, so-called components are a Java class. Like a mechanical part, it can be used repeatedly to assemble and form a product. But not every Java class is a JavaBean component. Software components have a certain concept and architecture. It is your understanding that the JavaBean component is a component that defines rules that require get and set methods, and by defining these rules, you can implement mechanisms for discovering component properties and events.

2. The role of software components: Just write one time, can be executed everywhere.

For programmers, the best thing is that JavaBean can implement code reuse, but also for the ease of maintenance of the program, and so has a great significance.

3. Features of JavaBean:

1. Executing the Java.io.Serializable interface
2. Parameter-free constructors available
3. Provides the Get and set methods to access its properties.

Two, the use of JavaBean: (Embed Java code and use JSP notation) "That is, as a normal class to use or in the JSP page processing when there is a special token processing method"

The following is a comparison of the different ways to use each of the features of JavaBean: (using JavaBean is just a convenience and can be used for component reuse.) )

A) Two methods of using the JavaBean class in the JSP: (the two lines of code are the same)

Method One: Use the <jsp:useBean> tag in JSP to access JavaBean:

<jsp:usebean id= "test" class= "test. Testbean "/>

Method Two: JSP embed Java code way access JavaBean:

First line import JavaBean:

<%@ page import= "Com.javaBean.TestBean"%>

The following can be used as in the Java language:

<% Testbean testbean=new Testbean (); %>

b) Two ways to pass and get data between JSP pages:

L Get Data:

Method One: Use attribute markers:

<jsp:getproperty name= "test" property= "message"/>

Method Two: Embed Java code directly: (simpler)

<%=testbean.getname ()%>

L Save data:

Method One: Use attribute markers:

To set a single element value:

<jsp:setproperty name= "test" property= "name" value= "JCM"/>

Set values for all elements in a JSP page:

<jsp:setproperty name= "Test" property= "*"/>

Note: This method is particularly convenient, only need to receive data and responsible for the display of the JSP page using <jsp:useBean> after adding this line of code, all the data is automatically assigned. You can then use the Get method to display the data in the next JSP code.

Law two: Embed Java code directly:

<%

String name=req.getparameter ("name");

Testbean.setname (name)

String pwd=req.getparameter ("password");

Testbean.setname (PWD)

%>

Note:

Usually in addition to the get and set methods in the JavaBean function, there is a method for storing data in db (that is, connecting to DB and implementing an INSERT statement).

Problem:

1. A javabean is used as a common class, (the import class, constructs an object through the new method, then invokes the method through the object), and uses the JSP marker <jsp:useBean> to use the difference? What is the difference between it and a normal class? JavaBean can also be accessed through the use of ordinary classes. This is what the above summarizes.

2. Is it only through the marker <jsp:useBean> to use in order to really check the benefits of JavaBean components: automatic assignment? Can I automatically assign a value if I don't use a marker <jsp:useBean>?

Need to be solved experimentally.

Solution: (Finally, come to understand!) )

Resources:

http://www.faq-it.org/archives/jsp/52ae59e6bc6d13af9ee621213347274a.php

Two questions about JavaBean's original rationality

Author: BDSC


First contact with JavaBean, encountered two of the original rational problem, did not find a definite information to get the answer, hope here can get everyone's help.
1. What is the difference between a JavaBean class and a normal Java class? JavaBean Class Code has no clear rules, such as must inherit what class? If not, how do you say a Java class is JavaBean, or not?
2. is JavaBean executed at the client or on the server?
2.1 If the client executes, that is not the client must have the JVM, and to download the corresponding
JavaBean's class file? But I did not find the corresponding JavaBean in my system disk.
What about the class file? The applet is also running on the client, why should there be javaBean?
2.2 If the server executes, the JavaBean is generated on the client side of the user interface, the service
How does the code executed on the server side generate the user interface on the client?
---------------------------------------------------------------

1.for JavaBeanin normal Javathe classthe distinction between classes is not very clear, mainlyJavaBeantypically in JSPThere are special reference methods on the page.
2.JavaBeanis performed on the server side. JavaBeandoes not directly generate the user interface (which is the appletdo), and usually only do data organization, processing work. Then through the JSPpage to generate the user interface.

These are just my personal understanding. I would also like to discuss it with you.
---------------------------------------------------------------

JavaBeanis a (compliant) Javaclass

*.JSP is running on server-side compilation, sending a stream of ordinary characters (by HTTP protocol) to the client (WebBrowser __ie/navigator) via the encapsulated socket (you see an object of the outputstream/name maybe incorrect), the character stream Accepted by the browser, interpreted and displayed in a certain format
---------------------------------------------------------------

1. What is the difference between a JavaBean class and a normal Java class? JavaBean Class Code has no clear rules, such as must inherit what class? If not, how do you say a Java class is JavaBean, or not?
JavaBean is actually a JAVA program. Can be called inside the JSP.
Enterprise JavaBean is an EJB used to build business applications. Must be deployed above the application server.

2. is JavaBean executed at the client or on the server?
JavaBean is the only HTML code that is executed on the server and returned to the client.
If you want to execute on the client, you can use the APPLET
---------------------------------------------------------------

Sunvery early JavaBean.The concept is to solve the visual applicationthe modularity of the proposed, but because Javain client development is really not so good, so sunno further in this regard, as the interface of modular development of the JavaBeanwas actually dead. (Above the purely personal point of view, welcome criticism)

but with the webapplication of the rise of JSPthe advent of JSPneed such a concept, function/The performance is separated, the function is encapsulated in a module, in order to achieve maximum code reuse, and then pick up this JavaBeanto use it in web/jsp .development.

The article you mentioned is aboutVisualization ofJavaBeanused in appletsin the situation, you can be clear in your mind (because perhaps no one is looking at it), what others are saying is that kind of javabean.

As to what exactly meets the requirements, honestly I don't know (because I personally disapprove of using JavaBean)

Self-Summary:

1. JavaBean is a (compliant) Java class. The distinction between JavaBean and ordinary Java class classes is not very clear, mainly because JavaBean usually have special reference methods on JSP pages.

2. So javabean can be used as a common class, or there are special reference methods on JSP pages.

3. For the third question: Is it only through the marker <jsp:useBean> to use to really check the benefits of the JavaBean component: Automatic assignment? Can I automatically assign a value if I don't use a marker <jsp:useBean>?

Answer: No. JSP automatic assignment refers to: through the <jsp:setproperty name= "Mybean" property= "*"/> This property access to the tag can be easily implemented on the JSP page all the parameters of the assignment, convenient, and if the ordinary class access , you need one by one to get the values and call the Set method. Like what:

<%

Stuinfobean stuinfobean=new Stuinfobean ();

String stuno=request.getparameter ("Stuno");

String stuname=request.getparameter ("Stuname");

Stuinfobean.setstuno (Stuno);

Stuinfobean.setstuname (Stuname);

%>

4. So JavaBean is just a Java class that conforms to the set and get methods and can have a special reference on the JSP.

Second, an example, using the JSP Special reference method and the use of ordinary classes to implement the call to JavaBean:

The main difference is in two places:

1) Use of the class:

L JSP notation: using <jsp:usebean/>

L General Class Mode: <%@ page import= "Com.javaBean.StuInfoBean"%>

2) Assign a value to a property:

L JSP notation: <jsp:setproperty name= "Stuinfobean" property= "*"/>

L General Class mode:

<%

Stuinfobean stuinfobean=new Stuinfobean ();

String stuno=request.getparameter ("Stuno");

String stuname=request.getparameter ("Stuname");

Stuinfobean.setstuno (Stuno);

Stuinfobean.setstuname (Stuname);

%>

--------------------------------------------------------------------------the following original content----------------------------------------- -------------------------------------------------------- Personal Summary:JavaBean is actually a very ordinary class, its own definition set, get property, and so on, said is a component because it is reusable, is a component idea, not like jmail and other components are encapsulated code.

What is JavaBean and how to summarize the two ways to use JavaBean

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.