Java Reference
In-Depth Information
You'll start with the controller class. In Listing 14‐1, you have implemented a simple controller that
responds to any HTTP GET request made to the /users/* path. The mapping of this relationship is
dei ned in the web.xml i le:
<servlet>
<servlet-name>FrontController</servlet-name>
<servlet-class>com.devchronicles.mvc.plain.FrontController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FrontController</servlet-name>
< url‐pattern > /users/* < /url‐pattern >
</servlet-mapping>
In the listing, you map the controller class com.devchronicles.mvc.plain.FrontController to
the request URL /users/* . Therefore, for every request made to this URL, it is directed to the
FrontController class for processing.
SERVLETS 3.0
Alternatively, you can annotate the controller class with the request URL like so:
@WebServlet({"/users/*"}). This annotation removes the need to dei ne the servlet
mapping in the web.xml .
The doGet() method is invoked for such requests, and an Action
object is retrieved from the
AbstractActionFactory , which determines the location of the view that should be returned to the user.
y
LISTING 14 ‐1: The refactored UserService class
package com.devchronicles.mvc.plain;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class FrontController extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Action action =
AbstractActionFactory. getInstance ().getAction(request);
String view = action.execute(request, response);
getServletContext().getRequestDispatcher(view).forward(request,
response);
}
}
Search WWH ::




Custom Search