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

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

主站蜘蛛池模板: 礼泉县| 太保市| 曲阳县| 邯郸市| 申扎县| 凤翔县| 郴州市| 昔阳县| 桃园县| 凭祥市| 濉溪县| 绥化市| 宣汉县| 通化市| 彩票| 博罗县| 福清市| 彭山县| 滁州市| 监利县| 乌审旗| 临高县| 乐都县| 逊克县| 克什克腾旗| 区。| 吉木乃县| 桓台县| 文昌市| 老河口市| 渭南市| 奉节县| 确山县| 綦江县| 杭州市| 永丰县| 武夷山市| 伊金霍洛旗| 安吉县| 正阳县| 丹阳市|