SOEN 6011 Software Engineering Processes Section SS Fall 2007 Dr Greg Butler
Week 6 Servlets and JSP Larman Controller Principle Fowler Front Controller –GoF Command pattern –Java reflection to instantiate Command
Servlets, The General Idea Applet as a client side Java application. Servlet as a server side technology for implementing part of the functionality of an application. HTTP (URL) requests cause a servlet to be: –Instantiated (if it did not exist). –Run. –The servlet builds a response which is then sent back to the client (usually in the form of HTML).
Servlet: HelloWeb public class HelloWebServlet extends HttpServlet { protected void doGet( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ServletOutputStream out = response.getOutputStream(); out.println("Hello Web!"); }
Java Server Page Servlet: –Embed HTML inside code. JSP –Embed code inside HTML.
HelloWeb.jsp page contentType="text/html; charset=iso " language="java"... %> Hello Web!
Hello.jsp page contentType="text/html; charset=iso " language="java"... %> Hello
Java Server Page Implemented as a (special kind of) servlet.
Controller Illustration: Web-based EA
PATTERN: Controller What object in the domain (or application coordination layer) receives requests for work from the UI layer?
PATTERN: Controller (Larman 17.13) Problem: What object in the domain (or application coordination layer) receives requests for work from the UI layer? System operations (see SSD): major input events to the system The controller is the first object beyond the UI layer that is responsible for receiving or handling a system operations message. Note that UI objects delegate system operation request to a controller.
PATTERN: Controller (Larman 17.13) Solution: Assign the responsibility to a class representing one of the following choices: A façade controller, which represents 1.the overall system 2.A root object 3.A device that the software is running within, or 4.A major subsystem A use case or session controller which represents a use case scenario in which the system operation occurs
Fig
Domain Data Source Transaction Script Domain Model Data Mapper Row Data Gateway Front Controller Template View Transform View Page Controller Presentation Active Record Fowler’s EA Patterns Table Module Table Data Gateway
Front Controller Fowler: A controller that handles all requests for a web site. The Front Controller consolidates all request handling by channeling requests through a single handler object. This object can carry out common behaviour which can be modified at runtime with decorators. This handler then dispatches to command objects for behaviour particular to a request.
Front Controller GoF design pattern: Command Use a Command object to encapsulate the steps that are executed by a command. Command::doIt() executes command Allows Command::unDoIt() etc GoF design pattern: Decorator Decorator::m() adds additional behaviour, but delegates to real Object::m() for real behaviour Eg, logging command execution before/after executing command
Page vs Front Controller – Differences in URL Page Controllers for A … Front Controller for A1 Base URL: …/frontController ?command=ViewTaskList …/frontController ?command=AddTaskGrade&title=abc&…
Front Controller
Front Controller: Sequence Diagram
Front Controller: ProcessRequest
Front Controller (cont’d)