Understanding the JavaBeans Java Class

A JavaBean is a Java class that uses setter and getter methods, as a means of introspection of the class attributes. Introspection, like reflection, is the ability to query class attributes by using getXXX(), where XXX is one of the class attributes. A JavaBean can save its state through persistence. Therefore, all of its persistent properties must be serializable. You use a JavaBean to encapsulate data mapping between a form's parameters and a database. It can be used as a data container (DataBean) to transfer information between the Web tier and the EJB tier. A JavaBean can encapsulate data for display purposes (ViewBean) and manage data flow between applets and applications. JavaBeans use events to interact with other beans. The most powerful use of a JavaBean is to automatically map and populate its properties from a form.

One example of a JavaBean is the StudentDataBean to transfer data between the UserManager EJB and the Web tier:

import java.io.Serializable;
public class StudentDataBean implements java.io.Serializable {
  private String userId;
  private String password;
  private String firstName;
  private String lastName;
  public StudentDataBean (String userId, String password,
               String firstName, String lastName) {
    this.userId = userId;
    this.password = password;
    this.firstName = firstName;
    this.lastName = lastName;
  }
  public void setFirstName(String n){ firstName = n;}
  public String getFirstName(){ return firstName;}
  public void setLastName(String n){ lastName = n;}
  public String getLastName(){ return lastName;}
  public void setUserId(String n){ userId = n;}
  public String getUserId(){ return userId;}
  public void setPassword(String n){ password = n;}
  public String getPassword (){ return password;}
}

JSP can use a JavaBean to automatically capture the form data of the client tier. Here, we use the standard tag <jsp:useBean> to process the form:

<%@ page import="day07.* %>
<jsp:useBean id="myBean" scope="session"
           class="day07.StudentDataBean" property="*" />
<html>
  <head>
    <title>Using Student Bean</title> </head>
  <body>
    <h1> Using Automatic Mapping of Student Bean</h1>
    <%
    String userName = request.getParameter( "Login Name" );
    out.print(userName + " : " + myBean.getUserId());
    %>
  </body>
</html>

In the preceding example, the <jsp:useBean> tag creates a JavaBean component by instantiating the StudentDataBean class, and automatically populates all its properties, as indicated by "*".

Servlets can use JavaBeans as parameters passed to their method invocations when calling an EJB. The EJB populates the JavaBean properties with result data after processing the servlet's request from a database or other entity EJBs. For example, the following doPost() method displays all courses for which a student has enrolled:

public void doPost (HttpServletRequest req, HttpServletResponse res)
        throws ServletException, IOException {
    String studentId = req.getParameter("Login name");
    PrintWriter out = res.getWriter();
    try{
       Collection coll = userManager.getCourses(studentId);
       while (coll.hasNext){
         EnrollmentDataBean enroll = (EnrollmentDataBean) coll.getNext();
         out.write("Course id: " + enroll.getCourseId() + "<br>");
    }catch{
     e.printStackTrace();
    }
}

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.133.54