Develop Java Enterprise Application with Netbeans 6.1

In this post I’ll show you how easy to develop full Java Enterprise application with Netbeans 6.1beta. For example I’ll develop a simple “Contact Person” application. I’ll used JPA for the model layer, SessionBean for facade object and Jsp/Servlet for the web client. All of this technology are supported by Netbeans6.1beta. Thanks for NetBeans teams your are rock!!

This application will have 2 entity model: Person and Address. A Person can have more than one Address.

contact.jpg

From the design above lets start to develop the application 😀

  • Start you Netbeans6.1beta IDE. If you don’t have it please download here. You have to download the Web & Java EE bundled.
  • Create a new project. Click on File>New Project
new-project.jpg
  • Chose Enterprise for the Categories and Enterprise Application for the Project.
  • On the New Enterprise Application Window, set the Project Name with person-jee, Server with GlassFish and check the Create EJB Module and Create Web Application Module checkbox then click Next and Finish.
project-name.jpg
  • Netbeans will create a new Enterprise Application project with EJB Module and Web Module project.
project-tree.jpg
  • Next we start to create the EJB Modul. We have to create the model: Person and Address using JPA and Facade Object using SessionBean.
  • SessionBean as facade object will encapsulates and centralizes the interactions between calling clients (web client) and the model entities and operations of the Java Persistence APIs. It provides a single interface for operations on a set of entities.
  • The client using this facade does not have to be aware of all the details of the APIs of each entity and does not have to be aware of any persistence mechanisms being used when accessing the entity managers or other transaction managers.
arsitekture.jpg
  • Clients and the entities in the model tier makes the code more loosely-coupled and easier to maintain.
  • Ok lets create the entity model. From the EJB Module project, right click on the Source Package>New>Entity Class
new-entity.jpg
  • From the New Entity Class Window fill in the Class Name with Person and Package with entity and then click on Create Persistence Unit button. We have to create new Persistence unit.
person1.jpg
  • From the Create Persistence Unit window, set the Persistence Unit Name to personPU and select TopLink as the Persistence Provider. Next we have to create New Data Source.
persistenceunit.jpg
  • From the Create Data Source window, set the JNDI Name to jdbc/dbperson and then we have to create New Database Connection. In this example we using MySQL as the Database Server.
datasource.jpg
  • We will create a Connection to a database in MySQL Server. For example we will create connection to dbperson. Dbperson is an empty database that don’t have any tables. The tables structure will be create by JPA automatically 😀
dbconnection1.jpg
  • Fill in the database connection property according to your database server. Klik Ok until you back to the Create Persistence Unit Window.
pu.jpg
  • Now click Create to create to Persistence Unit and you will back to the New Entity Class window. From this window click Finish to create the Person class.
  • Netbeans will create the entity Person source. Do the same step to create the entity Address but you don’t have to create Persistence Unit.
  • Now we have to modify the Person and Address source code. First please modify Person class.
@Entity
public class Person implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
private String occupation;
@OneToMany(cascade = CascadeType.ALL)
private List<Address>address = new ArrayList<Address>();
//other source here
}
  • Don’t forget to create setter and getter method for name,occupation and address field. Netbeans6.1beta have nice feature for this 😀
  • Press Alt+Ins on your keyboard and netbeans will show a popup for create setter and getter method 😀
setergeter.jpg
  • Select all field and click Generate. Netbeans will create the setter and getter method
setergeter2.jpg
  • Next modify the Address class. Add field street, city, province and phone. And don’t forget to add the setter and getter method too
  • After that we will create the Facade object that is SessionBean. Right click on Source Package>New>Session Bean
facade1.jpg
  • Fill in PersonFacadeSession for the EJB Name, facade for the package, Stateles for the Session type and Remote for the Interface and then click Finish. Netbeans will create the PersonFacadeSession source code.
  • We will add 5 business method in this facade object.
    • insert(Person p) : to insert New Person
    • delete(long id): to delete Person object by Id
    • update(long oldId,Person p): update a Person object by Id
    • list(): to get all person object
    • load(long id): to get a Person object by Id
  • To add business method in SessionBean (the facade object) is very simple. Netbeans has this feature too 😀
  • Right click on the source code area, select EJB Methods>Add Business Method…
addbusiness.jpg
  • On Add Business Method window set the method Name to insert and add a parameter person that has Object type.
businessmethodadd.jpg
  • And for the other business methods do the same way.
load.jpg
laod Person
delete.jpg
delete Person
update.jpg
update Person
list.jpg
list Person
  • Our facade object then will use EntityManager from JPA API to manipulated the model. So we must add it too. Right click on the source code area and select Persistence>Use Entity Manager
em.jpg
  • Modify your PersonFacadeSessionBean class
@Stateless
public class PersonFacadeSessionBean implements PersonFacadeSessionRemote {
@PersistenceContext
private EntityManager em;
public void insert(Object person) {
em.persist(person);
}
public void delete(long id) {
em.remove(em.find(Person.class, id));
}
public void update(long id, Object person) {
Person p = em.find(Person.class, id);
p = (Person) person;
em.refresh(p);
}
public java.util.List list() {
List list = em.createQuery("SELECT p FROM Person p").getResultList();
return list;
}
public Person load(long id) {
Person p = em.find(Person.class, id);
return p;
}
}
  • Because we using MySQL Server for the database, so we need to add MySQL Connector to the EJB module project.
  • Right click on the Library>Add Library
addlibrary.jpg
  • Find MySQL JDBC Driver and then click Add Library button
mysqljdbc.jpg
  • Right here we just finish develop the EJB module 😀 Next step we will create the web base client using struts framework.
  • First we prepare the Servlet class. Right click on Source Packages>New>Servlet
servlet1.jpg
  • The first Servlet is a servlet that get all person from database through the Facade object Session Bean.
  • Set the Class Name to GetListPersonServlet and the Package to servlet and click Next.
servlet2.jpg
  • On the Next Window, set the URL Pattern(s) to /list and Finish. It is the URL to call the Servlet.
servlet3.jpg
  • Then Netbeans will create the source code. Because this Servlet will call the Facade Object SessionBean, so we add some code for it. Netbeans is very helpful for this 😀
  • Right click on the source area, select Enterprise Resources>Call Enterprise Bean
callsession.jpg
  • On Call Enterprise Bean Window select PersonFacadeSessionBean and then click OK button
sessioncal2.jpg
  • You will see this code at top of the servlet source.
@EJB
private PersonFacadeSessionRemote personFacadeSessionBean;
  • Update the processRequest method
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
List persons = personFacadeSessionBean.list();
request.setAttribute("persons", persons);
RequestDispatcher dis = request.getRequestDispatcher("list.jsp");
dis.forward(request, response);
} catch(Exception ex) {
ex.printStackTrace();
}
}
  • Next Servlet is DeletePersonServlet. Create this servlet just like the other one before, but the URL Pattern(s) set to /delete. Don’t forget to add the SessionBean too
  • Modify the processRequest method for DeletePersonServlet
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
long id = Long.parseLong(request.getParameter("id"));
personFacadeSessionBean.delete(id);
RequestDispatcher dis = request.getRequestDispatcher("list");
dis.forward(request, response);
} catch (Exception ex) {
ex.printStackTrace();
}
}
  • Next Servlet is InsertPersonServlet. Create this servlet just like the other one before, but the URL Pattern(s) set to /insert. Don’t forget to add the SessionBean too
  • Modify the processRequest method for InsertPersonServlet
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
String name = request.getParameter("name");
String occupation = request.getParameter("occupation");
String street1 = request.getParameter("street1");
String city1 = request.getParameter("city1");
String province1 = request.getParameter("province1");
String phone1 = request.getParameter("phone1");
String street2 = request.getParameter("street2");
String city2 = request.getParameter("city2");
String province2 = request.getParameter("province2");
String phone2 = request.getParameter("phone2");Address addr1 = new Address();
addr1.setStreet(street1);
addr1.setCity(city1);
addr1.setProvince(province1);
addr1.setPhone(phone1);
Address addr2 = new Address();
addr2.setStreet(street2);
addr2.setCity(city2);
addr2.setProvince(province2);
addr2.setPhone(phone2);Person person = new Person();
person.setName(name);
person.setOccupation(occupation);
person.getAddress().add(addr1);
person.getAddress().add(addr2);personFacadeSessionBean.insert(person);
RequestDispatcher dis = request.getRequestDispatcher("list");
dis.forward(request, response);
} catch (Exception ex) {
ex.printStackTrace();
}
}
  • Next Servlet is DetailPersonServlet. Create this servlet just like the other one before, but the URL Pattern(s) set to /detail. Don’t forget to add the SessionBean too
  • Modify the processRequest method for DetailPersonServlet
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
long id = Long.parseLong(request.getParameter("id"));
Person p = personFacadeSessionBean.load(id);
request.setAttribute("person", p);
RequestDispatcher dis = request.getRequestDispatcher("detail.jsp");
dis.forward(request, response);
} catch (Exception ex) {
ex.printStackTrace();
}
}
  • Ok.. now we have to create the user interface with jsp page.
  • Create new JSP file: right click on Web Pages>New>JSP…
jsp1.jpg
  • Set JSP file name to list and then click Finish.
jsp2.jpg
  • Before we modify the source, we need to add JSTL library to our web module because we will use it in the JSP page. Right click on the Libraries>Add Library
addjstl.jpg
  • Find the JSTL 1.1 library and click Add Library
jstllib.jpg
😦 wiuh… i will continue this post next week

JUG Joglosemar website

Our Java community Jug joglosemar are plan to create a website, so I create the core module and host it on google code http://jugjoglosemar.googlecode.com/. We will start to develop it at NgoJek event next month..

The website use struts and hibernate as the framework. For awhile the preview looks like this

Logo jug-joglosemar

Hallo jugers joglosemar..

nih ada teman yang udah mulai desain logo jug-joglosemar. Rencanannya nanti kalo logo ini sudah disepakati kita bisa buat kaos dan pake logo yang disepakati bersama.

Bagi yang masih ingin kontribusi desain, silahkan kirim desainnya diemail aku hendro.steven@gmail.com

Logo 1

Logo 2

Logo 3

Logo 4