Java EE 8 Cookbook
eBook - ePub

Java EE 8 Cookbook

Elder Moraes

Share book
  1. 382 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Java EE 8 Cookbook

Elder Moraes

Book details
Book preview
Table of contents
Citations

About This Book

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

Frequently asked questions

How do I cancel my subscription?
Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Do you support text-to-speech?
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Is Java EE 8 Cookbook an online PDF/ePUB?
Yes, you can access Java EE 8 Cookbook by Elder Moraes in PDF and/or ePUB format, as well as other popular books in Computer Science & Programming in Java. We have over one million books available in our catalogue for you to explore.

Information

Year
2018
ISBN
9781788290258
Edition
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 of contents