Professional Documents
Culture Documents
Stack Overflow
Questions
Tags
Users
Badges
Unanswered
Ask Question
Take the 2-minute tour
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's
100% free, no registration required.
up
vote4do
wn vote
favorite
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.
java
netbeans
ejb-3.0
netbeans-7
alessandro
52731334
add a comment
1 Answer
activeoldest
up
vote5do
wn vote
accepted
votes
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 hit Next. 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 Projects tab on the left hand side. Choose New -> Session Bean. Choose whatever
name suits you, define your package and choose Singleton (if you're using EJB 3.0
you cannot create singleton EJBs - just choose another type). Make sure Local and
Remote interfaces are unchecked. Hit Finish.
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.:
a servlet,
a standalone client,
a @PostConstruct method.
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 @PostConstruct annotated 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@Singleton EJB, you can force the container to initialise
your bean during the server startup. In this way, you'll know that
your @PostConstruct method will be invoked during server startup.
At this point, you should have a code similar to the following one:
package your.package;
import
import
import
import
javax.annotation.PostConstruct;
javax.ejb.Singleton;
javax.ejb.LocalBean;
javax.ejb.Startup;
@Singleton
@Startup
public class NewSessionBean {
After running this exemplary code (big green arrow on the toolbar), you should see
GlassFish logs similar to this one:
INFO: Portable JNDI names for EJB NewSessionBean :
[java:global/EJBModule1/NewSessionBean!sss.NewSessionBean,
java:global/EJBModule1/NewSessionBean]
INFO: 9
INFO: EJBModule1 was successfully deployed in 78 milliseconds.
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 used this to
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.
package com.yourpckg;
import javax.ejb.Stateless;
// Stateless EJB with LocalBean view (default when no interface is
implementated)
@Stateless
public class Calculator {
// Business method (public) that may be invoked by EJB clients
public int add(int x, int y) {
return x + y;
}
}
You put this as a field in your servlet class, so it should be something like this:
@WebServlet(name = "MyServlet", urlPatterns = {"/MyServlet"})
public class MyServlet extends HttpServlet {
@EJB
Calculator calculator;
protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
// ...
}
} finally {
// No matter what - dispatch the request back to the JSP to show the result.
request.getRequestDispatcher("calculator.jsp").forward(request,
response);
}
}
1.
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:
http://localhost:37663/MyWebApp/calculator.jsp
In two input texts you type the operands and after clicking 'Calculate' you should see
the result.
shareimprove this answer
answered
Piotr Nowicki
7,94032253
2 Please give me a sign if you have a problem with EJB <-> Servlets connection and you already have working
EJBs. Piotr Nowicki Dec 28 '11 at 17:32
1 @alessandro are you sure that you need to go to EJB 2.x world? Are you sure and willing to enter this god-forgotte
land? ;-) Before answering, you should know that "home interface" is not required for connecting EJB with Servlets
It's just EJB 2.x way of accessing your beans. I will shortly update my answer with Servlets and JSP using this
EJB. Piotr Nowicki Dec 28 '11 at 19:49
You can check the updated answer. You should now know how to use these tree concepts. This uses no-interface vie
(LocalBeans) which are EJB 3.1 feature. For remote and home interfaces I'll guess you'll need to google a
bit :-) Piotr Nowicki Dec 28 '11 at 20:58
Thanks, I solved it. alessandro Dec 30 '11 at 8:35
add a comment
Your Answer
Sign up or log in
Sign up using Google
Post as a guest
Name
Email
Post Your Answ er
By posting your answer, you agree to the privacy policy and terms of service.
java
netbeans
ejb-3.0
asked
netbeans-7
3 years ago
viewed
4825 times
active
2 years ago
Related
0
question feed
tour help blog chat data legal privacy policy work here advertising info mobile contact us feedback
TECHNOLOGY
LIFE / ARTS
Stack
Overflow
Programmer
s
Server Fault
Super User
Linux
Webmasters
Different (Apple)
WordPress
Development
Game
Development
Geographic
Information Systems
TeX - LaTeX
Drupal
Science
Electrical
Engineering
Android
& Usage
Skeptics
Graphic
SharePoint
User
Experience
Mathematica
Salesforce
more (14)
Design
Seasoned
Comp
Travel
Advice (cooking)
Christianity
Home
Arqade (gaming)
Improvement
Bicycles
Personal
Academia
Role-playing
Games
more (21)
more (10)
Enthusiasts
Information
Security
site design / logo 2015 stack exchange inc; user contributions licensed under cc by-sa 3.0 with attribution required
rev 2015.2.11.2283
Valida
Mi Yodeya
(Judaism)
SCIE
English Language
Answers
Ask
Ask Ubuntu
Photography
Administrators
Unix &
Web
Applications
Database
CULTURE /
RECREATION