I am very new to Java EE/EJB, with very little knowledge about it.
I have downloaded NetBeans (7.01) and GlassFish (3.01). But, as I have no idea about EJB, and am not getting how to run the code in NetBeans which includes a simple Stateless Session Bean, a JSP and some Servlets.
I found a nice example code Calculator Example. Can any body help me by giving a step by step procedure how to run the NetBeans example? Thanks in advance.
I’d advise you not to use the linked tutorial. It seems to be from 2011, but it still talks about a lot of deployment descriptors and home interfaces (which are old, bad, ugly and unnecessary nowadays).
You can refer to this JBoss tutorial about EJB 3.0.
NetBeans have great support for Java EE development. Just a very fast tutorial (in Java EE 6):
1. Create your EJB project (EJB Module)
Create new project:
Shift + Ctrl + N->Java EE->EJB Module->Next. Choose whatever name suits you and hitNext. Choose the target application server (NetBeans should suggest you Glassfish Server 3.x) and Java EE version (choose Java EE 6) ->Finish.2. Add EJB to your project
Now you have your EJB project created. Right click on the project name in
Projectstab on the left hand side. ChooseNew->Session Bean. Choose whatever name suits you, define your package and chooseSingleton(if you’re using EJB 3.0 you cannot create singleton EJBs – just choose another type). Make sure Local and Remote interfaces are unchecked. HitFinish.You’ve just created your first EJB 😉
3. Invoking EJB business method
You can now try to use it. You need to execute your EJB class method – to do it, you’d need somebody to invoke your method. It can be i.e.:
@PostConstructmethod.I’ll show you how to use the last option which seems to be the easiest if you can use Singleton EJBs. All you need to know about
@PostConstructannotated method is that it will be invoked when the application container creates your bean. It’s like a special type of the constructor.The point is that you normally don’t have any control over EJBs initialisation. However, if you used the
@SingletonEJB, you can force the container to initialise your bean during the server startup. In this way, you’ll know that your@PostConstructmethod will be invoked during server startup.At this point, you should have a code similar to the following one:
After running this exemplary code (big green arrow on the toolbar), you should see GlassFish logs similar to this one:
This example also shows another feature of the EJB 3.1 – right now, not only you don’t need to use home interfaces but you don’t even have to use any interfaces. You can just use your class directly.
Note that there are several things wrong with this example. You should not use
System.out.printlninstructions, I’ve not used business interface but usedthisto invoke business method, I haven’t used Servlets to invoke my EJB’s business method and so on.This is just a very simple example to let you to start EJB developing.
As requested, below you can find mini-tutorial for EJB<->Servlet<->JSP workflow:
1. Create Web Project
(Note: you could achieve the above example – with Singleton EJB – with Web Project as well. In this case we need Web Project as we’ll create a servlet and JSP in one package.)
Ctrl + Shift + N->Java Web->Web Application->Next. Set the name of your application ->Next-> the defaults are fine (remember the context path – you’ll need it to access your application) ->Finish.2. Create your EJB
In this time, it’ll be stateless EJB as it betters reflects what the calculator bean should be.
You do it exactly as described above – just select
Statelessinstead ofSingletonin the appropriate window.3. Put business logic into your EJB
Find the example below:
4. Create Servlet which will call your business logic
RMB on your project or
Ctrl + N->Web->Servlet->Next-> define servlet name and its package ->Next-> define its URL pattern (remember it – you’ll need it to access your servlet) ->Finish.5. Define dependency between your Servlet and EJB.
Your controller (Servlet) need to use your EJB. You don’t want to do any lookups or nasty boilerplate code. You just defines that your Servlet will use your
CalculatorEJB by using annotation.You put this as a field in your servlet class, so it should be something like this:
6. Put controller logic into your Servlet
NetBeans by defaults delegates all HTTP Method requests into one method –
processRequest(-), so it’s the only method you should modify.Find the example below:
7. Create JSP file
Ctrl + Non your project ->Web->JSP->Next-> type the file name (in my case itscalculator, as the Servlet code uses this name (take a look atgetRequestDispatcherpart). Leave the folder input value empty. ->Finish.8. Fill JSP file with user interface code
It should be a simple form which defines two parameters:
operand1andoperand2and pushes the request to your servlet URL mapping. In my case its something like the following:Just watch the form
actionattribute value (it should be your Servlet URL mapping.).You should know what port your GlassFish uses. I guess that in NetBeans by default its
37663. Next thing is the Web Application URL and JSP file name. Combine it all together and you should have something like:In two input texts you type the operands and after clicking ‘Calculate’ you should see the result.