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

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.

主站蜘蛛池模板: 织金县| 清水河县| 巧家县| 沐川县| 安塞县| 远安县| 东阳市| 大方县| 澄城县| 梁河县| 桐乡市| 高青县| 云和县| 息烽县| 曲麻莱县| 浦江县| 嘉祥县| 牡丹江市| 永春县| 英德市| 晋宁县| 怀远县| 赞皇县| 礼泉县| 衡阳县| 西华县| 铅山县| 大厂| 石首市| 淳安县| 镇沅| 涡阳县| 上思县| 兴城市| 平阳县| 泗洪县| 华宁县| 桦川县| 呼伦贝尔市| 乐山市| 辉县市|