Build your first simple Enterprise Java Application

Simple enterprise java application with each and every detail. I’ve used Netbeans 8.2, GlassFish 4.1 Server and SQLyog Database for building simple enterprise java application.

First download and install SQLyog database and configure it. Then create database named “data“. You’ll do something like this

After that download Glassfish Server and add it to the Netbeans. You can add server in Netbeans as:

Once you’ve successfully added glassfish server, then you can start it as :

Once your server starts, then view domain admin console as

and you will see something like this

then on the left side, you can see JDBC -> JDBC Connection Pools -> New. Add new connection pool as :

Then add your resource in JDBC -> JDBC Resources. Your resource name is “jdbc/testDS” and it will include the “SqlPool“.

Now finally you’re ready to start building your application.

Simple Enterprise Java Application

Open Netbeans and create new Enterprise Application as

Then finally you have to make project structure as:

Now create package Entity and create Entity Class Student.java, then only persistence.xml is appeared as

Note: Click on create persistence unit to automatically generate persistence.xml

Make sure that you make your configuration as you seen in the screenshot.

Now you’re ready to code.

package Entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

/**
 *
 * @author Sujan Maka
 */
@Entity
@Table(name = "Student")
public class Student {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @Column(name = "firstName")
    private String firstName;

    @Column(name = "lastName")
    private String lastName;

  //getter and setters

}

Now create package Impl and include the StudentSessionBean.java and StudentSessionBeanLocal.java

StudentSessionBeanLocal

package Impl;

import Entity.Student;
import javax.ejb.Local;

@Local
public interface StudentSessionBeanLocal {

    public Student createAccount(String firstName, String lastName);
}

StudentSessionBean.java

package Impl;

import Entity.Student;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@Stateless
public class StudentSessionBean implements StudentSessionBeanLocal {

    @PersistenceContext(unitName = "persistence")
    private EntityManager em;

    @Override
    public Student createAccount(String firstName, String lastName) {
        Student student = new Student();
        student.setFirstName(firstName);
        student.setLastName(lastName);
        em.persist(student);
        return student;
    }
}

After that create index.html inside EJB-JPA-web -> Web Pages and include the following code

<!DOCTYPE html>
<html>
    <head>
        <title>Create Customer</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <form action="AccountController" method="post" >
            <input type="text" name="firstName" placeholder="First Name" />
            <input type="text" name="lastName" placeholder="Last Name"/>
            <input type="submit" value="Create"/>
        </form>
    </body>
</html>

And also create AccountController.java as


import Entity.Student;
import java.io.IOException;
import java.io.PrintWriter;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * ** @author SujanMaka
 */
@WebServlet(name = "AccountController", urlPatterns = {"/AccountController"})
public class AccountController extends HttpServlet {

    @EJBprivate
    Impl.StudentSessionBeanLocal studentSessionBean;

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            String firstName = request.getParameter("firstName");
            String lastName = request.getParameter("lastName");
            Student student = studentSessionBean.createAccount(firstName, lastName);
            out.println("Customer[" + student.getFirstName() + "] : " + student.getId());
        }
    }
}

Then finally you’re ready to run your first enterprise application. Clean and build the EJB-JPA, then run the EJB-JPA-ear and you will get output like this

simple enterprise java application
simple enterprise java application

Now check your database and you will find the data as:

Congratulation you’ve build your simple enterprise java application.