上QQ阅读APP看书,第一时间看更新
How to do it...
- We first create UserServlet that calls 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);
}
}
- And we do the same with ProfileServlet, but by calling 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);
}
}
- And then we 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);
}
}
- Here we create a page to call our servlets:
<body>
<a href="UserServlet">User</a>
<br/>
<a href="ProfileServlet">Profile</a>
</body>
- And here are the pages called by the servlets. First is 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>
- 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>