- Jakarta EE Cookbook
- Elder Moraes
- 288字
- 2021-06-24 16:12:45
How to do it...
To complete this recipe, please perform the following steps:
- 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);
}
}
- 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);
}
}
- 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);
}
}
- 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>
- 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>
- 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.
推薦閱讀
- Mastering Concurrency Programming with Java 8
- 精通Nginx(第2版)
- 數(shù)字媒體應(yīng)用教程
- Getting started with Google Guava
- 密碼學(xué)原理與Java實(shí)現(xiàn)
- PostgreSQL 11從入門到精通(視頻教學(xué)版)
- Android程序設(shè)計(jì)基礎(chǔ)
- Getting Started with Gulp
- Angular開發(fā)入門與實(shí)戰(zhàn)
- Python網(wǎng)絡(luò)爬蟲技術(shù)與應(yīng)用
- Unity 2017 Game AI Programming(Third Edition)
- Node.js從入門到精通
- Python Projects for Kids
- PHP動(dòng)態(tài)網(wǎng)站開發(fā)實(shí)踐教程
- Unity 3D UI Essentials