class GetLoginCountInteractor() {
private Presenter presenter;
public setPresenter(GetLoginCountPresenter presenter) { this.presenter = presenter; }
public void getLoginCountForUser(GetLoginCountRequest request) {
GetLoginCountResponse response = new GetLoginCountResponse();
response.loginCount = // insert code to determine loginCount by request.userId here
this.presenter.present(response);
}
class GetLoginCountRequest() {
int userId;
}
class getLoginCountResponse() {
public int loginCount;
}
interface GetLoginCountPresenter() {
public present(GetLoginCountResponse response);
}
package some.package.with.servlets
class GetLoginCountPresenterImpl implements GetLoginCountPresenter {
private httpRequest;
@Override
public present(GetLoginCountResponse response) {
httpRequest.setAttribute("loginCount", response.loginCount);
}
public void setHttpRequest(HttpServletRequest httpRequest){
this.httpRequest = httpRequest;
}
}
class showLoginCount extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
GetLoginCountRequest request = new
GetLoginCountRequest();
request.userId = 2;
GetLoginCountInteractor interactor = new GetLoginCountInteractor();
interactor.setPresenter(new GetLoginCountPresenterImpl());
interactor.getLoginCountForUser(request);
RequestDispatcher requestDispatcherObj = req.getRequestDispatcher("showLoginCount.jsp");
requestDispatcherObj.forward(req, resp);
}
}
requestDispatcher.forward
inside the Presenter forces me to catch the IOException and ServletException inside the Presenter (and not be able to use the exception mapping/handling from the servletContainer in the web.xml). Throwing it in the present method would leak those Exceptions inside my application.Maybe I'm missing the point about the Presenter exactly is in this situation. I think the HttpServlet is the controller, driving the usecase (interactor), but part of it feels like the Presenter as well (the request manipulation to define the parameters for the JSP view). Or does the design of Servlets simply not encourage the use of Presenters?
A more generic question emerging is "how to handle Exceptions from code coming used in the implementation of the Presenter?". My intuition says it should be handled in the Presenter, but I'm not so sure in this Servlet Example.