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

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();
主站蜘蛛池模板: 祥云县| 尼木县| 图木舒克市| 噶尔县| 紫金县| 太湖县| 南城县| 漳浦县| 平顺县| 江城| 沈阳市| 遂川县| 临猗县| 华蓥市| 鹤山市| 龙岩市| 壶关县| 洛川县| 东明县| 左贡县| 泰安市| 海城市| 灌云县| 江门市| 定南县| 石城县| 公主岭市| 麟游县| 霍邱县| 柞水县| 临颍县| 通城县| 灯塔市| 平江县| 晋中市| 额尔古纳市| 隆安县| 荃湾区| 沂源县| 阳泉市| 潼关县|