- Java EE 8 Design Patterns and Best Practices
- Rhuan Rocha Jo?o Purifica??o
- 198字
- 2021-07-23 16:54:54
Implementing DownloadFrontController
Here, we have the implementation of DownloadFrontController, which is a Servlet used to download files:
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(name = "DownloadFrontController", urlPatterns = "/download/*")
public class DownloadFrontController extends HttpServlet {
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
processRequest(request,response);
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
processRequest(request,response);
}
protected void processRequest(HttpServletRequest
request, HttpServletResponse
response)
throws ServletException, java.io.IOException {
//If user is logged the request is sent to
ApplicationController,
// then one error 401 is sent to client.
if(Objects.nonNull(request.getSession().getAttribute("USER"))) {
//Send the request to ApplicationController
new DownloadApplicationController(request,
response).process();
}
else {
response.sendError(401);
}
}
}
In the preceding block of code, we have the DownloadFrontController class with the logic to process a request. This class is a servlet that responds to all requests sent to /download/* using the following line of code:
@WebServlet(name = "DownloadFrontController", urlPatterns = "/download/*")
All GET requests or POST requests are sent to the processRequest method, inside which we have the code to send the request to DownloadApplicationController. The following line of code does just that:
//Send the request to ApplicationController
new DownloadApplicationController(request, response).process();
推薦閱讀
- Linux操作系統基礎
- 精通Linux內核開發
- 高性能Linux服務器構建實戰:運維監控、性能調優與集群應用
- Windows Vista融會貫通
- 嵌入式系統及其應用(第三版)
- Linux內核設計的藝術:圖解Linux操作系統架構設計與實現原理
- NetDevOps入門與實踐
- 跟老男孩學Linux運維:Shell編程實戰
- VMware Horizon View Essentials
- Cassandra 3.x High Availability(Second Edition)
- Linux操作系統
- Office 365 User Guide
- Linux操作系統案例教程(第2版)
- openEuler操作系統核心技術與行業應用實踐
- BuddyPress Theme Development