Develop Java Enterprise Application with Netbeans 6.1
April 23, 2008 47 Comments
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.
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
- 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.
- Netbeans will create a new Enterprise Application project with EJB Module and Web Module project.
- 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.
- 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
- 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.
- 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.
- 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.
- 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
- Fill in the database connection property according to your database server. Klik Ok until you back to the Create Persistence Unit Window.
- 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
- Select all field and click Generate. Netbeans will create the setter and getter method
- 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
- 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...
- On Add Business Method window set the method Name to insert and add a parameter person that has Object type.
- 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
- 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
- Find MySQL JDBC Driver and then click Add Library button
- 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
- 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.
- On the Next Window, set the URL Pattern(s) to /list and Finish. It is the URL to call the Servlet.
- 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
- On Call Enterprise Bean Window select PersonFacadeSessionBean and then click OK button
- 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...
































Hahaha..,kalau ak mending kasih source langsung, coz ribet nulis2nya itu pak… hahaha!
kalo sourcenya banyak di internet..
tapi bisa ngerti gak cara buatnya???
tapi kalo step by step mungkin lebih mudah dimengerti dari pada hanya source codenya yang di tampilin..
btw ada gak yang mau capek-capek tulis step by stepnya?? hehehehe.. semoga aku gak bosan
Huhuhu, aku sih capek kalo nulis segitu banyak di blog pak…
ya biar jadi teladan deh ini…
step by step aja pak, saya lebih gampang ngikutinnya
(kayak yang di video tutorialnya)
how about creating a web service from the session beans and calling webservice methods from the jsp pages?
This is good. Are you planning on completing it?
yes of course but wait for my free time
hallo radu..
creating web service from session bean is very simple.. I plan to post about it
pak saya mau tanya, ada ga perbedaan antara membuat aplikasi enterprise sperti jsf or ejb di netbean dengan di eclipse?
thx b4
secara konsep sama aja yang berbeda hanya di wizard-wizard yang disediakan oleh masing-masing IDE. Yang lebih mudah penggunaanya Netbeans
saya mw tny lagi pak,
klw di netbean misalkan kita membuat taglib seperti maka netbeans ga akn bawel alias ga eror.
tapi begitu saya coba di eclipse, maka keluar eror yang mengatakan tidak menemukan lib ny.
apakah di eclipse harus membuat pulgin dl? karena saya baca dibeberapa web mengatakan bahwa harus mendownload WTP. Bagaimana menurut bpk?
tag lib nya seperti ini pak :
%@ taglib prefix=”f” uri=”http://java.sun.com/core” % (tanda kurungny saya hilangkan karena pada comment diatas tidak keluar syntax ny
kalo ingin membuat aplikasi web dengan eclipse sebaiknya instal wtp juga. Atau biar gak repot download aja eclipse yang udah ada dengan wtpnya
wah repot juga y pak, ga semudah dinetbean
>.<
pak klw bs ks tw website yang bs bwt download wtp or eclipse yang ud include wtpny.
Karena ini sya coba download di eclipse.org ga bs trus. thx b4
Hallo Eva sory baru balas

wah aku gak punya alama lain selain di eclipse.org
Coba deh di http://www.eclipse.org/downloads/
trus pilih yang Eclipse IDE for Java JEE Developer nah nanti tinggal pilih mirornya.. ada server untuk Indonesia tuh..
semoga membatu
ok2 pak.
thx a lot ^^
ni masalah nya sudah terselesaikan dengan sempurna ^^
I want to know more about NetBean IDE for developing large Enterprise Application which involvs N-Tier architecture.
ditunggu, sambungannya
I have problem creating Enterprise application with Glassfish and MySQl….please continue.
Do you have complete source code for reference?
where is the second part of developing enterprise application in nebeabs 6.1
Greetings sir:
I’ve fully appreciated this tutorial and feel very grateful for it,since it assisted me to build a student database application.Yet,now I’ve got a new challenge: build a file database application.that’s why I’m asking for your help on creating the code for the user
to upload the files from elsewhere to the database.If you have,please assist me.
Thank you so much!
please continue the Enterprise application i need some more.
Hello Hendro! Thank you for this fine tutorial.
I’ve got a problem in creating a code to upload files and save them in a database created with this in view.Have you got the code? please let me know
bi boka yaramıyo
hey i wonder if you can help me, Iam trying to create a smart card application (Id Card) for members of a certain hospital staff. I have decided to use netbeans 6.1 and also downloaded java card development kit 2-2-2, please i need a guide of how i can make this happen, i am new with all these.
You can get intouch with me using my email address
much appreciation
Thanks
halo Rah,
cause i don’t know about smart card well..
I’m so sorry.. i can’t help you in that case
but thanks for your comment
I need the reminder of this project steps
Hi,
Just wanted to say thankyou for this tutorial, has been the most helpful I have found in regards to ejb’s.
Also wondering if the second half has been uploaded anywhere?
Thankyou again
Kate
Greetings Mr Evandro:
Hope you are doing well.I’ve got a problem and thought you to be up to it.
When trying to start a new connection to mysql server in my IDE(the server is registered) it issues the following: unable to add connection.for input string:”".
Why is that?
Thank you so much
Mas bisa bikin tulisan khusus ga? Yang memetakan tentang arsitektur java ee, beserta jenis2 teknologi java ee dan tempat imlementasinya pada konsep mvc, seperti Struts 2.0, velocity, jsf, iBatis, hibernate, Tapestry, dll. Dari yang opensource dan yang ikut standar JCP.
Kalo bisa saya juga minta saran, mulai dari mana tahap belajar JavaEE, dan apa saja toolsnya.
halo bruno,
sory for my late response
are your mysql server is running?
or your username and password is correct?
hehehe mas agak ribet tapi akan aku coba mas hehehehehe
please continue with jsp.
Please continue – or update ASAP.
Regards,
Kat.
olease continue for jsp
mana kelanjutannya nih….
trus methode update pada servlet kok g ada..?
tlg dilengkapi ya masss….
biar selesai saya mempelajarinya
I wonder have you completed this application. If so please send me the link I really need to understand how this application works. I have a similar but much bigger project to build and I need all the information I can get
Hi Hendro
please continue this application
mas hendro tutorial java card ada nggak? mohon pencerahannya
Please continue the jsp page
up to this was very useful to me
thanks and regards
Ayo pak lanjutin lagi dong mpe selesai. byk yg minta nih!
Hi Hendro
I wonder have you completed this application. If so please send me the link I really need to understand how this application works. I have a similar but much bigger project to build and I need all the information I can get.
I express my sincere thanks for your efforts.
Hi Hendro
I wonder have you completed this application. If so please send me the link I really need to understand how this application works. I have a similar but much bigger project to build and I need all the information I can get.
I express my sincere thanks for your efforts.
Helmut Orth
ada bukunya g
pak, sangat membantu saya dalam belajar euy tutorialnya tq ya pak….
senang bisa membantu
tutorial ini sangat membantu saia, bisa tolong dilanjutkan hingga selesai