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

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

主站蜘蛛池模板: 洛宁县| 宣武区| 清河县| 深圳市| 改则县| 万源市| 体育| 顺平县| 乌什县| 开远市| 安徽省| 长阳| 大埔区| 莲花县| 章丘市| 海晏县| 陈巴尔虎旗| 成安县| 分宜县| 栾川县| 靖边县| 长治县| 阿克| 华宁县| 周口市| 辽中县| 北宁市| 静海县| 易门县| 中山市| 仪征市| 深水埗区| 于田县| 沂水县| 玉龙| 孝感市| 上犹县| 祁连县| 阜南县| 青河县| 江孜县|