Java EE 8 Design Patterns and Best Practices
上QQ阅读APP看书,第一时间看更新

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);