Java EE 8 Cookbook
eBook - ePub

Java EE 8 Cookbook

Elder Moraes

Partager le livre
  1. 382 pages
  2. English
  3. ePUB (adapté aux mobiles)
  4. Disponible sur iOS et Android
eBook - ePub

Java EE 8 Cookbook

Elder Moraes

DĂ©tails du livre
Aperçu du livre
Table des matiĂšres
Citations

À propos de ce livre

A practical guide for building effective enterprise solutions with Java EE 8

Key Features

  • Recipes to get you up-and-running with Java EE 8 application development
  • Learn how to apply the major Java EE 8 APIs and specifications
  • Implement microservices and Reactive programming with Java EE 8

Book Description

Java EE is a collection of technologies and APIs to support Enterprise Application development. The choice of what to use and when can be dauntingly complex for any developer. This book will help you master this. Packed with easy to follow recipes, this is your guide to becoming productive with Java EE 8.

You will begin by seeing the latest features of Java EE 8, including major Java EE 8 APIs and specifications such as JSF 2.3, and CDI 2.0, and what they mean for you.

You will use the new features of Java EE 8 to implement web-based services for your client applications. You will then learn to process the Model and Streaming APIs using JSON-P and JSON-B and will learn to use the Java Lambdas support offered in JSON-P. There are more recipes to fine-tune your RESTful development, and you will learn about the Reactive enhancements offered by the JAX-RS 2.1 specification.

Later on, you will learn about the role of multithreading in your enterprise applications and how to integrate them for transaction handling. This is followed by implementing microservices with Java EE and the advancements made by Java EE for cloud computing.

The final set of recipes shows you how take advantage of the latest security features and authenticate your enterprise application.

At the end of the book, the Appendix shows you how knowledge sharing can change your career and your life.

What you will learn

  • Actionable information on the new features of Java EE 8
  • Using the most important APIs with real and working code
  • Building server side applications, web services, and web applications
  • Deploying and managing your application using the most important Java EE servers
  • Building and deploying microservices using Java EE 8
  • Building Reactive application by joining Java EE APIs and core Java features
  • Moving your application to the cloud using containers
  • Practical ways to improve your projects and career through community involvement

Who this book is for

This book is for developers who want to become proficient with Java EE 8 for their enterprise application development. Basic knowledge of Java is assumed

Foire aux questions

Comment puis-je résilier mon abonnement ?
Il vous suffit de vous rendre dans la section compte dans paramĂštres et de cliquer sur « RĂ©silier l’abonnement ». C’est aussi simple que cela ! Une fois que vous aurez rĂ©siliĂ© votre abonnement, il restera actif pour le reste de la pĂ©riode pour laquelle vous avez payĂ©. DĂ©couvrez-en plus ici.
Puis-je / comment puis-je télécharger des livres ?
Pour le moment, tous nos livres en format ePub adaptĂ©s aux mobiles peuvent ĂȘtre tĂ©lĂ©chargĂ©s via l’application. La plupart de nos PDF sont Ă©galement disponibles en tĂ©lĂ©chargement et les autres seront tĂ©lĂ©chargeables trĂšs prochainement. DĂ©couvrez-en plus ici.
Quelle est la différence entre les formules tarifaires ?
Les deux abonnements vous donnent un accĂšs complet Ă  la bibliothĂšque et Ă  toutes les fonctionnalitĂ©s de Perlego. Les seules diffĂ©rences sont les tarifs ainsi que la pĂ©riode d’abonnement : avec l’abonnement annuel, vous Ă©conomiserez environ 30 % par rapport Ă  12 mois d’abonnement mensuel.
Qu’est-ce que Perlego ?
Nous sommes un service d’abonnement Ă  des ouvrages universitaires en ligne, oĂč vous pouvez accĂ©der Ă  toute une bibliothĂšque pour un prix infĂ©rieur Ă  celui d’un seul livre par mois. Avec plus d’un million de livres sur plus de 1 000 sujets, nous avons ce qu’il vous faut ! DĂ©couvrez-en plus ici.
Prenez-vous en charge la synthÚse vocale ?
Recherchez le symbole Écouter sur votre prochain livre pour voir si vous pouvez l’écouter. L’outil Écouter lit le texte Ă  haute voix pour vous, en surlignant le passage qui est en cours de lecture. Vous pouvez le mettre sur pause, l’accĂ©lĂ©rer ou le ralentir. DĂ©couvrez-en plus ici.
Est-ce que Java EE 8 Cookbook est un PDF/ePUB en ligne ?
Oui, vous pouvez accĂ©der Ă  Java EE 8 Cookbook par Elder Moraes en format PDF et/ou ePUB ainsi qu’à d’autres livres populaires dans Computer Science et Programming in Java. Nous disposons de plus d’un million d’ouvrages Ă  dĂ©couvrir dans notre catalogue.

Informations

Année
2018
ISBN
9781788290258
Édition
1

Server-Side Development

Java EE can be seen as being made for server-side development. Most of the APIs are powerful for server-side processing and managing.
This chapter will provide you with some common and useful scenarios that you may face as a Java EE developer and will show you how you should deal with them.
In this chapter, we will cover the following recipes:
  • Using CDI to inject context and dependency
  • Using Bean Validation for data validation
  • Using servlet for request and response management
  • Using Server Push to make objects available beforehand
  • Using EJB and JTA for transaction management
  • Using EJB to deal with concurrency
  • Using JPA for smart data persistence
  • Using EJB and JPA for data caching
  • Using batch processing

Using CDI to inject context and dependency

Context and Dependency Injection for Java EE (CDI) is one of the most important APIs under the Java EE umbrella. Introduced in Java EE 6, it now has a big influence over many other APIs.
In the recipe, you will learn how to use CDI in a couple of different ways and situations.

Getting ready

First, let's add the required dependency needed:
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>

How to do it...

  1. We are going to build a JAX-RS based application, so we will start by preparing the application to perform:
@ApplicationPath("webresources")
public class Application extends javax.ws.rs.core.Application {
}
  1. Then, we create a User application as our main object:
public class User implements Serializable {

private String name;
private String email;

//DO NOT FORGET TO ADD THE GETTERS AND SETTERS
}
Our User class doesn't have a default constructor, so CDI doesn't know how to construct the class when it tries to inject it. So, we create a factory class and use the @Produces annotation over its methods:
public class UserFactory implements Serializable{

@Produces
public User getUser() {
return new User("Elder Moraes", "[email protected]");
}

}
  1. Let's create an enumeration to list our profile types:
public enum ProfileType {
ADMIN, OPERATOR;
}
  1. Here, we create a custom annotation:
@Qualifier
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER})
public @interface Profile {
ProfileType value();
}
  1. Add them to an interface to prototype the user profile behavior:
public interface UserProfile {
ProfileType type();
}
Now that we have defined the profile list and its behavior with respect to the user, we can give it a proper implementation for an admin profile:
@Profile(ProfileType.ADMIN)
public class ImplAdmin implements UserProfile{

@Override
public ProfileType type() {
System.out.println("User is admin");
return ProfileType.ADMIN;
}
}
And the same can be done for an operator profile:
@Profile(ProfileType.OPERATOR)
@Default
public class ImplOperator implements UserProfile{

@Override
public ProfileType type() {
System.out.println("User is operator");
return ProfileType.OPERATOR;
}
}
  1. Then, we create a REST endpoint by injecting all the objects that we are going to use into it:
@Path("userservice/")
@RequestScoped
public class UserService {

@Inject
private User user;

@Inject
@Profile(ProfileType.ADMIN)
private UserProfile userProfileAdmin;

@Inject
@Profile(ProfileType.OPERATOR)
private UserProfile userProfileOperator;

@Inject
private UserProfile userProfileDefault;

@Inject
private Event<User> userEvent;

...
  1. This method gets the user injected by CDI and sends it to the result page:
 @GET
@Path("getUser")
public Response getUser(@Context HttpServletRequest request,
@Context HttpServletResponse response)
throws ServletException, IOException{

request.setAttribute("result", user);
request.getRequestDispatcher("/result.jsp")
.forward(request, response);
return Response.ok().build();
}
  1. This one does the same with an admin profile:
 @GET
@Path("getProfileAdmin")
public Response getProfileAdmin(@Context HttpServletRequest request,
@Context HttpServletResponse response)
throws ServletException, IOException{

request.setAttribute("result",
fireUserEvents(userProfileAdmin.type()));
request.getRequestDispatcher("/result.jsp")
.forward(request, response);
return Response.ok().build();
}
  1. And this one does the same with an operator profile:
 @GET
@Path("getProfileOperator")
public Response getProfileOperator(@Context HttpServletRequest request,
@Context HttpServletResponse response)
throws ServletException, IOException{

request.setAttribute("result",
fireUserEvents(userProfileOperator.type()));
request.getRequestDispatcher("/result.jsp")
.forward(request, response);
return Response.ok().build();
}
  1. Finally, we send the default profile to the result page:
 @GET
@Path("getProfileDefault")
public Response getProfileDefault(@Context HttpServletRequest request,
@Context HttpServletResponse response)
throws ServletException, IOException{

request.setAttribute("result",
fireUserEvents(userProfileDefault.type...

Table des matiĂšres