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

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();
主站蜘蛛池模板: 锦州市| 峨眉山市| 武宣县| 新龙县| 乌兰浩特市| 青龙| 嘉定区| 商丘市| 株洲县| 德州市| 昆山市| 象山县| 铜鼓县| 壶关县| 枞阳县| 呼和浩特市| 乌兰浩特市| 彰化县| 大方县| 绥中县| 留坝县| 昭平县| 灵台县| 玛纳斯县| 阜新市| 许昌市| 彭水| 丰顺县| 交口县| 浪卡子县| 肥东县| 改则县| 斗六市| 深泽县| 新田县| 蓝山县| 康定县| 巴楚县| 滦南县| 衡南县| 临夏县|