What is the preferred way to use EJB and servlets for web applications?

I am trying to familiarize myself with JavaEE. I'm a little confused that the purpose of each “component” (due to the lack of a better word) is: Session Beans and Servlets, as well as how they interact correctly with a web application (client-side JavaScript).

In an attempt to understand this, I am creating a simple web application. What is the preferred way to use each component to create something similar to the following:

  • User visits login page
  • The user enters data and clicks. Then I send a request with AJAX for user login.
  • Then the server side checks the user input and "registers" the user (returns the user profile, etc.).

When sending a request, send it to a servlet (which uses EJB) or to a Bean session via WSDL? How do I maintain a “state” for this user using any method? I assume that with Session Beans it is as simple as annotating it with @Stateful.

In addition, I assume that requests sent from the client should be in SOAP format. How easy is it to use something lighter (like JSON)? Although I would prefer to use something lightweight, it is not necessary if SOAP makes development faster / easier.

+4
source share
3 answers

The Java Enterprise Edition Tutorial covers almost all the topics you raise; what is the purpose with different types of bean, how can I implement web services, how to implement authentication, etc.

I highly recommend you take the time to create a sample application, especially if you are completely new to Java Enterprise Edition (Java EE). It’s important that you understand the basic concepts well, because at first it’s hard to understand what to focus on because of the breadth and depth of the technologies and standards that include Java EE.

It should be borne in mind that although Java EE certainly tries to support best practices and allows you to design and develop secure enterprise applications that work well and scale, it does not prescribe or restrict enterprise applications to follow one specific protocol, data format and design pattern enterprise applications. Some protocols and formats are better supported explicitly with the main framework implementations, and some options are vendor-specific, but very few specific technology options are blocked in the specification.

To answer some of your specific questions, Java EE has excellent SOAP support, but does not prefer or limit web services to SOAP. With JAXB and JAX-RS, it is just as easy to create RESTful web services that accept and return XML or JSON, or both. You decide if you need to use SOAP, REST, or another protocol.

It is also your choice whether you want to use frameworks such as JAX-RS or explicitly create servlets to handle HTTP requests and responses. In many cases, JAX-RS will have everything you need, which means that you can implement your web services as simple old Java methods with a few annotations without worrying about sorting and unpacking the contents and parameters.

Similarly, with JAXB, it depends on whether you want to use WSDL or not. This is great if you have WSDL definitions, but no problem if you don't.

In many cases, you typically maintain state using the Java Persistence Architecture (JPA) infrastructure, and access and manage this data through a stateless beans session. Developers new to Java EE are often tempted to use a beans state session to maintain a state that is better managed in persistent storage. The tutorial presents the various types of bean and their purpose.

+4
source

Web services (WSDL, SOAP, etc.) are commonly used for communication between applications.

Inside a single web application, you usually make simple GET / POST requests using AJAX or not, and you get either a full HTML page, or an HTML fragment (AJAX), or XML or JSON (AJAX) data. The browser usually talks to the servlet, but it is rarely possible to use servlets directly.

The usual way is to use a framework on top of a servlet. Structures can be divided into two broad categories: action-based frameworks (Stripes, Spring MVC, Struts, etc.) or component-based frameworks (JSF, Wicket, Tapestry, etc.).

In an n-tier application, all of the above technologies should contain only a presentation layer. This layer represents the business layer where the real business logic occurs when transactions are used to access databases, messaging systems, etc. This business layer is used to use EJB.

+1
source

You can create a basic architecture as follows:

Create an EAR to develop two different projects, such as the EJB Jar and the WAR web application

You can create servlets that invoke some delegate class that has logic to cancel the EJB, Either invoking it as a remote invocation, or by using the @EJB annotation in the delegation class.

ServletClass { do/post(){ DelegateClass d = new DelegateClass(); d.callMethod(withParam); } } DelegateClass { @EJB EJBlocalinterface ejbintance; void callMethod(DefinPrarm){ ejbinstance.callEJBMethod(); } } @Statelss EJBbeanClass implements EJBlocalinterface{ void callEJBmethod(someParam){ } } 
0
source

All Articles