Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7006931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:32:27+00:00 2026-05-27T21:32:27+00:00

I am very new to Java EE/EJB, with very little knowledge about it. I

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T21:32:28+00:00Added an answer on May 27, 2026 at 9:32 pm

    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 javax.annotation.PostConstruct;
    import javax.ejb.Singleton;
    import javax.ejb.LocalBean;
    import javax.ejb.Startup;
    
    @Singleton
    @Startup
    public class NewSessionBean {
    
        // This method will be invoked upon server startup (@PostConstruct & @Startup)
        @PostConstruct
        private void init() {
            int result = add(4, 5);
            System.out.println(result);
        }
    
        // This is your business method.
        public int add(int x, int y) {
            return x + y;
        }
    }
    

    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.println instructions, 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.


    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 Stateless instead of Singleton in the appropriate window.

    3. Put business logic into your EJB

    Find the example below:

    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;
        }
    }
    

    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 Calculator EJB by using annotation.

    @EJB
    Calculator calculator;
    

    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 {
        // ...
    }
    

    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:

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    
        try {
            // Fetch the user-sent parameters (operands)
            int operand1 = Integer.parseInt(request.getParameter("operand1"));
            int operand2 = Integer.parseInt(request.getParameter("operand2"));
    
            // Calculate the result of this operation
            int result = calculator.add(operand1, operand2);
    
            // Put the result as an attribute (JSP will use it)
            request.setAttribute("result", result);
        } catch (NumberFormatException ex) {            
            // We're translating Strings into Integers - we need to be careful...
            request.setAttribute("result", "ERROR. Not a number.");
        } finally {            
    
            // No matter what - dispatch the request back to the JSP to show the result.
            request.getRequestDispatcher("calculator.jsp").forward(request, response);
        }
    }
    

    7. Create JSP file

    Ctrl + N on your project -> Web -> JSP -> Next -> type the file name (in my case its calculator, as the Servlet code uses this name (take a look at getRequestDispatcher part). 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: operand1 and operand2 and pushes the request to your servlet URL mapping. In my case its something like the following:

    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>JSP Page</title>
        </head>
        <body>
            <form action="MyServlet">
                <input type="text" name="operand1" size="3" value="${param['operand1']}" /> +
                <input type="text" name="operand2" size="3" value="${param['operand2']}" /> = 
                <input type="submit" value="Calculate" />
            </form>
    
            <div style="color: #00c; text-align: center; font-size: 20pt;">${result}</div>
        </body>
    </html>
    

    Just watch the form action attribute value (it should be your Servlet URL mapping.).

    1. Enter your application

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm very new to Java but I've been developing a habit to use final
Very new to FluentNHibernate, but I'm also excited about the area. I've recently started
I am very new to Java generics and have spent an exorbitant amount of
I'm very new to Java programming language so this is probably dumb question but
Being completely new to Java EE (but not to Java itself) I'm trying to
I am very new to spring and java. I have been using mostly springsource.org
I am very new to JAVA. I have written simple program (in Linux -VIM
Firstly I am very new to JAVA, so I apoligise if I am not
I'm very new to Java and the Android SDK but I'm almost done with
I'm very new to Java and am not sure if I'm even allowed to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.