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

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.

主站蜘蛛池模板: 博客| 武冈市| 晴隆县| 普安县| 卓资县| 银川市| 依安县| 陇川县| 石狮市| 南丹县| 五峰| 南和县| 沛县| 太仆寺旗| 孟连| 三河市| 阿克| 科技| 晋州市| 泸州市| 高雄市| 民乐县| 宣武区| 大邑县| 丰城市| 大港区| 龙里县| 通海县| 临沭县| 平潭县| 香河县| 陇南市| 弋阳县| 青海省| 灵丘县| 韶山市| 清徐县| 喜德县| 平舆县| 临泽县| 嘉善县|