Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Jakarta EE Cookbook
  • Table Of Contents Toc
Jakarta EE Cookbook

Jakarta EE Cookbook - Second Edition

By : Moraes
5 (2)
close
close
Jakarta EE Cookbook

Jakarta EE Cookbook

5 (2)
By: Moraes

Overview of this book

Jakarta EE is widely used around the world for developing enterprise applications for a variety of domains. With this book, Java professionals will be able to enhance their skills to deliver powerful enterprise solutions using practical recipes. This second edition of the Jakarta EE Cookbook takes you through the improvements introduced in its latest version and helps you get hands-on with its significant APIs and features used for server-side development. You'll use Jakarta EE for creating RESTful web services and web applications with the JAX-RS, JSON-P, and JSON-B APIs and learn how you can improve the security of your enterprise solutions. Not only will you learn how to use the most important servers on the market, but you'll also learn to make the best of what they have to offer for your project. From an architectural point of view, this Jakarta book covers microservices, cloud computing, and containers. It allows you to explore all the tools for building reactive applications using Jakarta EE and core Java features such as lambdas. Finally, you'll discover how professionals can improve their projects by engaging with and contributing to the community. By the end of this book, you'll have become proficient in developing and deploying enterprise applications using Jakarta EE.
Table of Contents (14 chapters)
close
close

Using Server Push to make objects available beforehand

One of the most important new features of Jakarta Servlet 4 is its HTTP/2.0 support. It brings another cool and reliable feature Server Push.

This recipe will show you how to use Server Push in a filter and push the resources needed in every request that we want.

Getting ready

First, we need to add the required dependencies:

<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>8.0</version>
<scope>provided</scope>
</dependency>

How to do it...

To complete this recipe, please perform the following steps:

  1. First, we need to create UserServlet, which will call user.jsp:
@WebServlet(name = "UserServlet", urlPatterns = {"/UserServlet"})
public class UserServlet extends HttpServlet {

protected void doRequest(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
request.getRequestDispatcher("/user.jsp").forward(request, response);
System.out.println("Redirected to user.jsp");
}

@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
doRequest(request, response);
}

@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
doRequest(request, response);
}
}
  1. We need to do the same with ProfileServlet, but call profile.jsp:
@WebServlet(name = "ProfileServlet", urlPatterns = {"/ProfileServlet"})
public class ProfileServlet extends HttpServlet {

protected void doRequest(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
request.getRequestDispatcher("/profile.jsp").
forward(request, response);
System.out.println("Redirected to profile.jsp");
}

@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
doRequest(request, response);
}

@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
doRequest(request, response);
}
}
  1. Now, we need to create a filter that will be executed on every request (urlPatterns = {"/*"}):
@WebFilter(filterName = "PushFilter", urlPatterns = {"/*"})
public class PushFilter implements Filter {

@Override
public void doFilter(ServletRequest request,
ServletResponse response,
FilterChain chain)
throws IOException, ServletException {

HttpServletRequest httpReq = (HttpServletRequest)request;
PushBuilder builder = httpReq.newPushBuilder();

if (builder != null){
builder
.path("resources/javaee-logo.png")
.path("resources/style.css")
.path("resources/functions.js")
.push();
System.out.println("Resources pushed");
}

chain.doFilter(request, response);

}
}
  1. Next, we need to create a page so that we can call our servlets:
<body>
<a href="UserServlet">User</a>
<br/>
<a href="ProfileServlet">Profile</a>
</body>

  1. Here are the pages that will be called by the servlets. First, there's the user.jsp page:
    <head>
<meta http-equiv="Content-Type" content="text/html;
charset=UTF-8">
<link rel="stylesheet" type="text/css"
href="resources/style.css">
<script src="resources/functions.js"></script>
<title>User Push</title>
</head>

<body>
<h1>User styled</h1>
<img src="resources/javaee-logo.png">
<br />
<button onclick="message()">Message</button>
<br />
<a href="javascript:window.history.back();">Back</a>
</body>
  1. Second, the profile.jsp page is called:
    <head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="resources/style.css">
<script src="resources/functions.js"></script>
<title>User Push</title>
</head>

<body>
<h1>Profile styled</h1>
<img src="resources/javaee-logo.png">
<br />
<button onclick="message()">Message</button>
<br />
<a href="javascript:window.history.back();">Back</a>
</body>

When you run the preceding code, make sure to use the HTTPS port as it only works under this protocol; for example, https://localhost:4848/ch02-serverpush-1.0.

How it works...

A web application running under HTTP/1.0 sends a request to the server when it finds references for an image file, CSS file, and any other resources needed to render a web page.

With HTTP/2.0, you still can do this, but now, you can do this a lot better the server can now push the resources beforehand, avoiding unnecessary new requests, decreasing the server load, and improving performance.

In this recipe, our resources are represented by the following code:

   meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
link rel="stylesheet" type="text/css" href="resources/style.css">
script src="resources/functions.js"></script>

The push happens at this part of our filter:

   HttpServletRequest httpReq = (HttpServletRequest)request;
PushBuilder builder = httpReq.newPushBuilder();

if (builder != null){
builder
.path("resources/javaee-logo.png")
.path("resources/style.css")
.path("resources/functions.js")
.push();
System.out.println("Resources pushed");
}

So, when the browser needs those resources to render the web page, they are already available.

There's more...

Note that your browser needs to support the Server Push feature; otherwise, your page will work as usual. So, make sure you check that PushBuilder is null before using it and ensure all users will have the working application.

JSF 2.3 is built on top of the Server Push feature, so if you just migrate your JSF application to a Jakarta EE 8-compatible server, you get its performance boost for free!

Finally, Server Push only works under a Secure Sockets Layer (SSL) protocol, so pay attention to it.

See also

CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Jakarta EE Cookbook
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist download Download options font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon