- Java EE 8 Design Patterns and Best Practices
- Rhuan Rocha Jo?o Purifica??o
- 368字
- 2021-07-23 16:54:53
Implementing FrontController
Here, we have an implementation of MyAppController, which is a FrontController to treat all the requests of an application:
import com.rhuan.action.Command.AbstractCommand;
import com.rhuan.action.Command.HomeCommand;
import com.rhuan.action.Command.LoginCommand;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
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 = "MyAppController", urlPatterns = "/myapp/*")
public class MyAppController extends HttpServlet {
private static Logger logger =
LogManager.getLogger(MyAppController.class);
private final String PAGE_ERROR = "/pageError.jsp";
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 {
String resultPage;
AbstractCommand command = null;
try {
//Create a correspondent Command.
if(request.getSession().getAttribute("USER") == null)
command = new LoginCommand();
else command = new HomeCommand();
//Execute the Command that return a page.
resultPage = command.execute();
} catch (Exception e) {
logger.error(e.getMessage());
resultPage = PAGE_ERROR;
}
//Dispatch to correspondent page.
getServletContext().getRequestDispatcher(resultPage)
.forward(request, response);
}
}
In the following code snippet, it is very important to note that urlPattern is used to define which requests a context will send to our controller. Here's how we do this:
//Defining the urlPattern to Front Controller
@WebServlet(name = "MyAppController", urlPatterns = "/myapp/*")
public class MyAppController extends HttpServlet {
...
}
On the urlPattern, the value is "/myapp/*". As previously shown in the preceding code snippet, this URL pattern ("/myapp/*") establishes that all requests to the myapp URI are sent to our controller. For example, http://ip:port/context/myapp/myfuncionality is sent to our controller.
When we implement this pattern, it is very important to pay attention to the use of attributes on servlets, because all the class attributes on a servlet are shared with all threads or all requests.
All GET requests or POST requests are sent to the processRequest method, which implements the logic to send the request to the respective command and executes the respective logic. After the correct command is set, the respective command is executed and the page is dispatched. Here, we have the line that executes the command and dispatches the request to the correct page:
//Execute a Command
resultPage = command.execute();
Dispatching the request to the corresponding page:
//Dispatch to correspondent page.
getServletContext().getRequestDispatcher(resultPage)
.forward(request, response);
- Citrix XenApp Performance Essentials
- Learning Android Intents
- Kali Linux滲透測試全流程詳解
- SharePoint 2013 WCM Advanced Cookbook
- 新手學電腦從入門到精通(Windows 10+Office 2016版)
- 奔跑吧 Linux內核(入門篇)
- 竹林蹊徑:深入淺出windows驅動開發
- Joomla! 3 Template Essentials
- NetDevOps入門與實踐
- Ubuntu Linux操作系統實用教程
- iOS 10快速開發:18天零基礎開發一個商業應用
- Java EE 7 Developer Handbook
- Learn Quantum Computing with Python and IBM Quantum Experience
- Windows 8玩全不求人
- Linux指令從初學到精通