Presentation is loading. Please wait.

Presentation is loading. Please wait.

JSP Architecture Outline  Model 1 Architecture  Model 2 Architecture.

Similar presentations


Presentation on theme: "JSP Architecture Outline  Model 1 Architecture  Model 2 Architecture."— Presentation transcript:

1 JSP Architecture Outline  Model 1 Architecture  Model 2 Architecture

2 Model 1 architecture  JSP-Centric Architecture –JavaBeans or EJB Model Objects –View JSP pages –Action JSP pages

3 Model 1 architecture

4  In the Model 1 architecture, the incoming request from a web browser is sent directly to the JSP page, which is responsible for processing it and replying back to the client.  There is still separation of presentation from content, because all data access is performed using beans.

5 Person Example Program- Navigation Flow

6 Person Example Program – List Page  Presents a list of Person objects  Allows Edit and Create Operations

7 Person Example Program – Edit Page Typical HTML Form Page Displays Existing Model Object or Empty New Object

8 Model 1 Architecture - Overview  JSP Components Perform Display and Processing Tasks

9 Model 1 Architecture – Display Details View JSP Pages request model objects from business tier Standard JSP Tags used to display data <jsp:useBean id="person" class="com.wiley.compBooks.nyberg.ch02.objects.Person" scope="request">... person = PersonService.findPersonById(id);...... <INPUT TYPE="text" NAME="firstName" value=" " size="50"/>

10 Model 1 Architecture – Form/Submit Details  HTML Form is posted to unique Action JSP page  Other techniques include “Self Posting” and “Next-Page Posting”  HTTP Request Parameters extracted using setProperty with * <jsp:useBean id="person" class="com.wiley.compBooks.nyberg.ch02.objects.Person" scope="request" />  Action JSP page performs validation and model updates

11 Model 1 Architecture – Navigation Details  View JSP Pages hardcode links to other View pages  Action JSP Pages hardcode links to destination View pages // Return to form page if errors are encountered, leaving Person // on request if (errors.size()>0) { %> <% } else {... redirect(response,"./ShowPeople.jsp"); }

12 Model 1 Architecture – Summary Benefits of JSP-Centric Approach Drawbacks of JSP-Centric Approach Small number of components required to build a given application Architecture produces a tightly- coupled application with hard coded page names Small number of technologies, reducing learning curve for inexperienced resources Action JSP pages are primarily Java code, but cannot be developed, compiled, and debugged as easily as pure Java code Re-use of processing and validation logic is hampered by its placement in form-specific action JSP pages

13 Model 2 architecture  Servlet-Centric Architecture –JavaBeans or EJB Model Objects –View JSP pages –Servlet or Command Classes

14 Model-View-Controller  Model-View-Controller approach came from SmallTalk community - Involved notification/event models, direct manipulation of model objects  MVC today is basically a Tiered architecture -Interposes Controller components between View and Model components -Controller responsible for navigation, presentation-tier logic, validation -Emphasizes separation of presentation logic and model objects

15 Features  Clients do not request pages directly.  All clients requests go to a controller servlet.  Each request includes data: The requested action Any parameters for that action.  Controller servlet: Decides which page should be returned to user. Augments REQUEST (not response) object with additional data to be displayed to user.

16 Advantages  MVC approach simplifies JSP pages: No navigation code inside them. No complex data manipulation (db access, etc.)  Clean separation of presentation and processing logic.  The front components present a single point of entry into the application, thus making the management of application state, security, and presentation uniform and easier to maintain.  Multiple views using the same model

17 Model 2 Architecture

18  The processing is divided between presentation (JSPs) and front components (controllers).  Presentation components are JSP pages that generate the HTML/XML response that determines the user interface when rendered by the browser.  Front components do not handle any presentation issues, but rather, process all the HTTP requests. They are responsible for creating any beans or objects used by the presentation components, as well as deciding, depending on the user's actions, which presentation component to forward the request to.  Front components can be implemented as either a servlet or JSP page.

19 Model 2 Architecture Issues  Single or Multiple controller servlets  Different views to be supported  Single functionality in page

20 Controller Responsibilities  Request processing  Creation of any beans or objects used by the presentation JSP  Deciding, depending on the user's actions, which JSP to forward the request to.  Data validation

21 Controller Design Poorly designed controller if (op.equals("createUser")) { model.createUser(request.getAttribute("user"), request.getAttribute("pass")); } else if (op.equals("changeUserInfo") { //... and so on... }

22 Controller Design Controller using Command Pattern public abstract class Action { protected Model model; public Action(Model model) { this.model = model; } public abstract String getName(); public abstract Object perform(HttpServletRequest req); }

23 Controller Design public class CreateUserAction extends Action { public CreateUserAction(Model model) { super(model);} public String getName() { return "createUser"; } public Object perform(HttpServletRequest req) { return model.createUser(req.getAttribute("user"), req.getAttribute("pass")); }

24 Controller Design public class ControllerServlet extends HttpServlet { private HashMap actions; public void init() throws ServletException { actions = new HashMap(); CreateUserAction cua = new CreateUserAction(model); actions.put(cua.getName(), cua); } public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {

25 Controller Design String op = getOperation(req.getRequestURL()); Action action = (Action)actions.get(op); Object result = null; try { result = action.perform(req); } catch (NullPointerException npx) { }

26 View Responsibilities  There is no processing logic within the presentation JSP itself: it is simply responsible for retrieving any objects or beans that may have been previously created by the Servlet, and extracting the dynamic content for insertion within static templates.

27 Sample View Best Available Flights <jsp:useBean id="customer" class="moreservlets.TravelCustomer" scope="session" /> Finding flights for <jsp:getProperty name="customer" property="frequentFlyerTable" />

28 Tag Libraries and M2 Client Controller Action JSP Page Dispatch forward Server side objects Tag libraries and business objects JSP page include


Download ppt "JSP Architecture Outline  Model 1 Architecture  Model 2 Architecture."

Similar presentations


Ads by Google