Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

How to read a XHTML form with JSF and persist using EJB

This article will help us to learn about the procedure of reading the XHTML form data and also will explain the process of using EJB (Enterprise JavaBeans) for inserting the data to MySQL database. The whole procedure is explained with the help of an example.

An entity is an object, defining a table from the database, and each attribute in the entity class defines a row in the table. Let us consider a Vendor Management System as an example. In a Vendor Management System, Vendor and Order are two entities, where the Vendor entity captures vendor related details like vendor name, vendor address etc. and Order entity captures information related to purchase orders like items, quantity, rate, date of purchase, quotation id etc.

Here Vendor and Order are two tables in database and name, vendor, address, items, quantity, rate, quotation id are some of the fields from the tables.

An Entity Bean can contain one or more annotations, which condition the program how the value should be stored in the database.

Other than annotations entity bean has getters and setters for each and every attribute.

In addition to Entity Bean we have Managed Bean, which is used to manage our business logic and Session Bean which is used to manage CRUD operations.

EJB is based on Model View Controller Architecture, where Entity Bean act as controller, XHTML acts as view.

Listing 1: Sample Entity bean.

@Entity
@Table(name = "vendor")
public class Vendor implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @NotNull
    @Column(name = "vendor_id")
    private Integer vendorId;
    @Size(max = 45)
    @Column(name = "name")
    private String name;
    @Size(max = 60)
    @Column(name = "address")
    private String address;
    @Column(name = "phone")
    private Integer phone;
    @Size(max = 45)    
   public Vendor() {
    }
    public Vendor(Integer vendorId) {
        this.vendorId = vendorId;
    }
    public Integer getVendorId() {
        return vendorId;
    }
    public void setVendorId(Integer vendorId) {
        this.vendorId = vendorId;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this. name = name;
    }
……

These are the annotations that are used in the program and their meaning:

  • @Entity annotation indicates the class in an entity class.
  • @Table(name = "vendor") tells the program which table is to be used.
  • @Id indicates primary key of the table.
  • @Basic(optional = false) tells the program, this particular field should never be blank.
  • @NotNull tells the program, this field can never be null.
  • @Column(name = "vendor_id") tells the program, which field it is.
  • @Size(max = 45) , tells the program about the maximum size the field can accommodate.

In a Vendor Management System, when the client wants to have a look at the orders given to a particular vendor, the information regarding the specific Vendor is loaded in to an entity bean.

Thus an entity bean can be considered as a bridge between client and the database.

In order to create our first EJB application we need to download Netbeans. Netbeans is a tool to develop java and java web application. We can download Netbeans along with glass fish server in the following link https://netbeans.org/community/releases/73/.

We will create a small application which gets vendor related details from a xhtml form and store it in the Mysql Database.

Step 1

In this step, we will create an EJB application in Netbeans.

Shows the screenshot of how to create a new EJB project in netbeans (Step -1)

Figure 1: Shows the screenshot of how to create a new EJB project in netbeans (Step -1)

Shows the screenshot of how to create a new EJB project in netbeans (Step -2)

Figure 2: Shows the screenshot of how to create a new EJB project in netbeans (Step -2)

Shows the screenshot of how to create a new EJB project in netbeans (Step -3)

Figure 3: Shows the screenshot of how to create a new EJB project in netbeans (Step -3)

Step 2

In this step, we will create a XHTML page to get Vendor details.

Shows the screenshot of how to create a xhtml page in netbeans (Step - 1)

Figure 4: Shows the screenshot of how to create a xhtml page in netbeans (Step - 1)

Shows the screenshot of how to create a xhtml page in netbeans (Step - 2)

Figure 5: Shows the screenshot of how to create a xhtml page in netbeans (Step - 2)

Listing 1: Shows the xhtml code to perform step 2

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:rich="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">
    <h:head>
        <title>Create Vendor</title>
    </h:head>
    <h:body>
        <f:view>
            <h:form>
 <h:panelGrid columns="2">
 <h:outputText value="Vedor Name"/>
<h:inputText value="#{ vendorMB. name }"/>
 <h:outputText value="Address"/>
<h:inputText value="#{ vendorMB. address }"/>
<h:outputText value="Description"/>
<h:inputText value="#{ vendorMB. description }"/>
<h:outputText value="Phone No."/>
<h:inputText value="#{ vendorMB. phone }"/>
<h:outputText value="Fax"/>
<h:inputText value="#{ vendorMB. fax }"/>
<h:outputText value="EmailId"/>
<h:inputText value="#{ vendorMB. emailid }"/>
<h:outputText value="Contact Person"/>
<h:inputText value=="#{ vendorMB. contactperson }"/>
<h:outputText value="Remarks"/>
<h:inputText value=="#{ vendorMB. remarks }"/>
<h:button value="Submit" onclick=""/>
 </h:panelGrid>
            </h:form>
         </f:view>
    </h:body>
</html>

At this step the page will show some errors, as we don’t have the supporting entity class. This error will be solved automatically once we reach step 5.

Step 3

In this step, we will create a table in the database for our program.

Listing 2: Shows the code to perform step 3

CREATE TABLE VENDOR ( VENDOR_ID int(10) NOT NULL auto_increment,
  NAME varchar(45) NOT NULL, ADDRESS varchar(60) NOT NULL,
  DESCRIPTION varchar(60) NOT NULL, PHONE int(10) NOT NULL,
 FAX int(10) NOT NULL, EMAIL_ID varchar(10) NOT NULL, CONTACT_PERSON varchar(45) NOT NULL, REMARKS varchar(100) NOT NULL, PRIMARY KEY  (ID))

Step 4

In this step, we will create an entity bean.

Shows the screenshot of how to create an entity bean in netbeans (Step - 1)

Figure 6: Shows the screenshot of how to create an entity bean in netbeans (Step – 1)

Shows the screenshot of how to create an entity bean in netbeans (Step - 2)

Figure 7: Shows the screenshot of how to create an entity bean in netbeans (Step – 2)

On clicking Create Persistense Unit, Net beans allows us to create a connection to the database, by allowing us to enter the connection parameters.

Listing 3: Shows the code to perform step 4

import javax.persistence.Entity;
import java.io.Serializable;
import javax.persistence.Id;
import javax.persistence.*;
import javax.persistence.Table;
import java.util.Collection;
import javax.persistence.GeneratedValue; 
import javax.persistence.GenerationType;
@Entity
@Table(name = "vendor")
public class Vendor implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @NotNull
    @Column(name = "vendor_id")
    private Integer vendorId;
    @Size(max = 10)
    @Column(name = "name")
    private String name;
    @Size(max = 45)
    @Column(name = "address")
    private String address;
   @Size(max = 60)
   @Column(name = "description")
    private String description;
    @Size(max = 60)
    @Column(name = "phone")
    private Integer phone;
    @Size(max = 10)    
    @Column(name = "fax")
    private Integer fax;
    @Size(max = 10)    
   @Column(name = "email_id")
    private String  emailid;
    @Size(max = 10)    
   @Column(name = "contact_person")
    private String contactperson;
    @Size(max = 45)    
@Column(name = "remarks")
    private String remarks;
    @Size(max = 100)    
  public Vendor() {
    }
    public Vendor(Integer vendorId) {
        this.vendorId = vendorId;
    }
    public Integer getVendorId() {
        return vendorId;
    }
    public void setVendorId(Integer vendorId) {
        this.vendorId = vendorId;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this. name = name;
    }
public String getAddress () {
        return address;
    }
    public void setAddress (String address) {
        this.address= address;
    }
public String getDescription() {
        return description;
    }
    public void setDescription (String description) {
        this. description = description;
    }
public int getPhone () {
        return phone;
    }
    public void setPhone (int phone) {
        this. phone = phone;
    }
public int getFax() {
        return fax;
    }
    public void setFax (int fax) {
        this. fax= fax;
    }
public String getEmailId() {
        return emailid;
    }
    public void setEmailId(String emailid) {
        this. emailid = emailid;
    }
public String getContactPerson() {
        return contactperson;
    }
    public void setContactPerson (String contactperson) {
        this. contactperson = contactperson;
    }
public String getRemarks () {
        return remarks;
    }
    public void setRemarks (String remarks) {
        this. remarks = remarks;
    }


Step 5

In this step, we will create a Managed bean to handle our business logic.

Shows the screenshot of how to create a JSF Managed bean in netbeans (Step -1)

Figure 8: Shows the screenshot of how to create a JSF Managed bean in netbeans (Step -1)

Shows the screenshot of how to create a JSF Managed bean in netbeans (Step -2)

Figure 9: Shows the screenshot of how to create a JSF Managed bean in netbeans (Step -2)

Listing 4: Shows the code to perform step 5

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import com.myproj.Vendor;
@ManagedBean(name = "vendorMB")
@RequestScoped
public class VendorMB {
VendorSB vendorsb;
Vendor vendor;

public createVendor(){
this.vendor=vendor;
this.vendorsb=vendorsb;
}
  public void setVendor(Vendor vendor) {
        this.vendor = vendor;
    }
    public Vendor getVendor() {
        return vendor;
    }
public void saveVendor(){
vendorsb.saveVendor(vendor);
}
}

Step 6

In this step, we will write a Session Bean to perform the Create operation.

Shows the screenshot of how to create a session bean in netbeans (Step -1)

Figure 10: Shows the screenshot of how to create a session bean in netbeans (Step -1)

Shows the screenshot of how to create a session bean in netbeans (Step -2)

Figure 11: Shows the screenshot of how to create a session bean in netbeans (Step -2)

Listing 5: Shows the code to perform step 6

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.io.Serializable;
@Stateless
@LocalBean

public class VendorSB implements Serializable {
  @PersistenceContext(unitName="EntityBean")
 EntityManager em;
public void saveVendor(Vendor vendor){
em.persist(vendor);
}
}

Conclusion

Finally, in this article, we have learnt how to get data from a XHTML form, to our database by means of EJB.

I hope you liked, see you next time.



I am well versed with Computer Programming languages and possess good working knowledge on software languages such as C, Java, PHP, HTML and CSS

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login