官术网_书友最值得收藏!

  • Jakarta EE Cookbook
  • Elder Moraes
  • 104字
  • 2021-06-24 16:12:40

How to do it...

You need to perform the following steps to try this recipe:

  1. We will create a servlet:
@WebServlet(name = "ServerPush", urlPatterns = {"/ServerPush"})
public class ServerPush extends HttpServlet {

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

PushBuilder pb = request.newPushBuilder();
if (pb != null) {
pb.path("images/javaee-logo.png")
.addHeader("content-type", "image/png")
.push();
}

try (PrintWriter writer = response.getWriter();) {
StringBuilder html = new StringBuilder();
html.append("<html>");
html.append("<center>");
html.append("<img src='images/javaee-logo.png'><br>");
html.append("<h2>Image pushed by ServerPush</h2>");
html.append("</center>");
html.append("</html>");
writer.write(html.toString());
}
}
}

  1. To try it, run the project in a Jakarta EE 8 server and open this URL:
https://localhost:8181/ch01-servlet/ServerPush

Let's now see how this works.

主站蜘蛛池模板: 虹口区| 商南县| 威海市| 瓮安县| 贵定县| 云浮市| 隆昌县| 永州市| 阜阳市| 牟定县| 乐都县| 鹤庆县| 茂名市| 磐石市| 顺平县| 哈巴河县| 桑植县| 博野县| 黄陵县| 绥芬河市| 刚察县| 定襄县| 遵义县| 阿拉善盟| 渝中区| 南宫市| 福建省| 咸宁市| 泸定县| 紫阳县| 大港区| 平昌县| 冀州市| 嘉定区| 星子县| 会泽县| 同德县| 泸西县| 东兰县| 合作市| 班玛县|