Jakarta EE Cookbook
上QQ阅读APP看书,第一时间看更新

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.