Server Push
One highlight feature of HTTP/2 is Server Push. Servlet 4.0 addPushBuilder
to handle push.An exmaple of enable Servlet Push.
@WebServlet(urlPatterns="")
@ServletSecurity(httpMethodConstraints={
@HttpMethodConstraint(value="GET", transportGuarantee=CONFIDENTIAL) })
public class PushServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException {
PushBuilder pushBuilder = req.newPushBuilder().
path("main.css");
pushBuilder.push();
res.getWriter().println("<html><head><title>HTTP2 Test</title><link rel=\"stylesheet\" href=\"main.css\"></head><body>Hello Servlet Push!!!</body></html>");
}
}
There is a warning flag on the https icon of address bar, the SSL certificate in Glassfish v5 is not recognised by Firefox. Click it and add
localhost
to the exception and make Firefox trust it.In the Network tab of developer tools, you will the main.css is initialized by Push.
Grab the source codes from my github account, and have a try.
评论