9

Java EE Web Profile (EJB) quickstart tutorial with Maven, Intellij and Glassfish

 3 years ago
source link: https://marco.dev/2012/09/30/java-ee-web-profile-ejb-quickstart-tutorial-with-maven-intellij-and-glassfish/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

java-version.com: What's new in Java 16? 15? Keep up to date!

Java EE Web Profile (EJB) quickstart tutorial with Maven, Intellij and Glassfish

This short tutorial has the goal to show how to start as fast as possible a new EJB project using IntelliJ and Maven.

  1. Create a new project from scratch

start11.png?w=300&resize=300%2C138

  1. Complete the form, you have to choose the Maven module

start2.png?w=300&resize=300%2C157

  1. Complete the Maven information

untitled.jpg?w=300&resize=300%2C88

  1. Your project is created according to Maven standards
untitled2.jpg?w=300&resize=300%2C237
  1. Add Java EE dependencies, this step is necessary to create a Web Profile project, Intellij will download automatically the new artifact

untitled4.jpg?w=300&resize=300%2C92

We have to define the type of packaging we want in our maven config. Without declaring a packaging of type war the application won’t be deployed.

  1. Create a new Class, this class is of type Stateless (a Service for who comes from Spring)

untitled-3.jpg?resize=207%2C99

  1. Edit the class, we do something very easy: a classical HelloWorld bean.

With the EE web profile we don’t need to create interfaces if we don’t access the stateless bean (service) from a different JVM.

untitled-12.jpg?w=300&resize=300%2C238
  1. We create our web configuration. Add a web.xml file under WEB-INF

untitled12.jpg?resize=187%2C134

  1. The web.xml file is quite traditional, you can use one of the many templates on the web
untitled13.jpg?w=271&resize=271%2C300
  1. We can create now the managed bean, it’s the controller who manage the communication between the View (jsp) and the Model (EJB)

111.jpg?resize=215%2C119

  1. The code of the controller is simple. The class is declared as @ManagedBean to allow the injection in the view. The EJB (Stateless class) is injected using the annotation. The getText method is exposed to the view.
untitled-12.jpg?w=300&resize=300%2C238
  1. We can now create facelets page

untitled-13.jpg?w=300&resize=300%2C119

  1. We call the view Hello.xhtml and we place it in the main webapp folder
  1. The interesting part of the code is the call to the helloMB managed bean. The @ManagedBean annotation previously used allow us to access easily to the bean.
untitled3.jpg?w=300&resize=300%2C223
  1. We connect to a local GlassFish server

glassfish.png?w=300&resize=300%2C163

  1.  We use the web explosed facet to deploy our application

untitled26.jpg?w=300&resize=300%2C95

untitled23.jpg?w=300&resize=300%2C116

  1. We have to choose to which domain deploy our application (e.g. domain1)
untitled27.jpg?w=300&resize=300%2C208
  1. We can start the server. The application is deployed.

Author

Marco Molteni

Marco Molteni Blog


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK