MVC Model 2 Architecture & AddressBook case study

Slides:



Advertisements
Similar presentations
Web Development with Karsten Schulz Terp-Nielsen Master Principal Sales Consultant Oracle Denmark.
Advertisements

Apache Struts Technology
 Copyright Wipro Technologies JSP Ver 1.0 Page 1 Talent Transformation Java Server Pages.
Advanced Java Server Pages An more detailed look at JSPs.
28/1/2001 Seminar in Databases in the Internet Environment Introduction to J ava S erver P ages technology by Naomi Chen.
Integrating Servlets and JavaServer Pages Vijayan Sugumaran School of Business Administration Oakland University Parts of this presentation provided by.
Application Architectures Vijayan Sugumaran Department of DIS Oakland University.
Structure of a web application1 Dr Jim Briggs. MVC Structure of a web application2.
Apache Struts Technology A MVC Framework for Java Web Applications.
TimeTracker 2, Take 1  Servlets Web Interface (jsp) Servlet (business logic and processing) App Engine Datastore Form Submit R/W.
ECE356 – Database Systems Lab 1 – Building a Web Project with NetBeans Tiuley Alguindigue Lab Instructor – University of Waterloo, E & CE Dept. Fall 2013.
Struts 2.0 an Overview ( )
UNIT-V The MVC architecture and Struts Framework.
Lecture 2 - Struts ENTERPRISE JAVA. 2 Contents  Servlet Deployment  Servlet Filters  Model View Controllers  Struts  Dependency Injection.
Java Server Pages B.Ramamurthy. Topics for Discussion 8/20/20152 Inheritance and Polymorphism Develop an example for inheritance and polymorphism JSP.
AddressBook using Servlets and Intro to JSP Lec - 33.
Netbeans – jsp.zip Introduction to JSP Netbeans – jsp.zip.
M. Taimoor Khan * Java Server Pages (JSP) is a server-side programming technology that enables the creation of dynamic,
Objectives Java Servlet Web Components
Design Patterns Phil Smith 28 th November Design Patterns There are many ways to produce content via Servlets and JSPs Understanding the good, the.
Chapter 7 Java Server Pages. Objectives Explain how the separation of concerns principle applies to JSP Describe the operation and life-cycle of a JSP.
Java Server Pages Lecture July Java Server Pages Java Server Pages (JSPs) provide a way to separate the generation of dynamic content (java)
JSP Architecture Outline  Model 1 Architecture  Model 2 Architecture.
Java Server Pages A JSP page is a text-based document that contains two types of text: static template data, which can be expressed in any text-based format,
COMP 321 Week 7. Overview HTML and HTTP Basics Dynamic Web Content ServletsMVC Tomcat in Eclipse Demonstration Lab 7-1 Introduction.
Chapter 8 Script-free pages. Problem with scripting in JSP When you use scripting (declaration, scriplet, expressions) in your JSP, you actually put Java.
Chapter 5 Being a Web App. Very few servlet or JSP stands alone Many times in our application, different servlets or JSPs need to share information 
® IBM Software Group © 2007 IBM Corporation J2EE Web Component Introduction
Stanisław Osiński, 2002JSP – A technology for serving dynamic web content Java Server Pages™ A technology for serving dynamic web content Stanisław Osiński,
Case Study + MVC Lec Error Pages By means of the page directive, a JSP can be given the responsibility of an Error page An Error JSP will be called.
Webcommerce Computer Networks Webcommerce by Linnea Reppa Douglas Martindale Lev Shalevich.
J2EE Overview Web Programming CSCI J2EE multi-tier architecture Servlet: Java class loaded into Web server JSP page: enhanced HTML page that is.
Simple MVC. An example: join.jsp
Java Servlets & Java Server Pages Lecture July 2013.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 Session Management.
JSP Tag Libraries Lec Last Lecture Example We incorporated JavaBeans in “Course Outline” Example But still have to write java code inside java.jsp.
Copyright © 2002 ProsoftTraining. All rights reserved. JavaServer Pages.
CSC 2720 Building Web Applications Frameworks for Building Web Applications.
Fall 2007cs4201 Advanced Java Programming Umar Kalim Dept. of Communication Systems Engineering
Apache Struts. J2EE Web Application using MVC design pattern Why MVC? Separate components = easier maintenance – Model component holds object data – View.
CourseOutline Example & JavaBeans Lec Start with Example Displaying Course Outlines User will select either course “web design & development” or.
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
JSP Pages. What and Why of JSP? JSP = Java code imbedded in HTML or XML –Static portion of the page is HTML –Dynamic portion is Java Easy way to develop.
JS (Java Servlets). Internet evolution [1] The internet Internet started of as a static content dispersal and delivery mechanism, where files residing.
CS562 Advanced Java and Internet Application Introduction to the Computer Warehouse Web Application. Java Server Pages (JSP) Technology. By Team Alpha.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 JSP Application Models.
COMP9321 Web Application Engineering Semester 2, 2015 Dr. Amin Beheshti Service Oriented Computing Group, CSE, UNSW Australia Week 3 1COMP9321, 15s2, Week.
1 Copyright © 2004, Oracle. All rights reserved. Oracle Application Development Framework.
Vakgroep Informatietechnologie – Onderzoeksgroep (naam) Web Centric Design of Distributed Software.
Chapter 3 JSP Overview. The Problem with Servlets processing the request and generating the response are both handled by a single servlet class Java programming.
©SoftMoore ConsultingSlide 1 Overview of Servlets and JavaServer Pages (JSP)
Implementation Struts Framework for well-architectured web applications Model-View-Controller design pattern.
1 Java Server Pages A Java Server Page is a file consisting of HTML or XML markup into which special tags and code blocks are inserted When the page is.
Chapter 6 Chapter 6 Server Side Programming (JSP) Part 1 1 (IS 203) WebProgramming (IS 203) Web Programming.
 Java Server Pages (JSP) By Offir Golan. What is JSP?  A technology that allows for the creation of dynamically generated web pages based on HTML, XML,
Java Programming: Advanced Topics 1 Building Web Applications Chapter 13.
Bayu Priyambadha, S.Kom. Static content  Web Server delivers contents of a file (html) 1. Browser sends request to Web Server 3. Web Server sends HTML.
APACHE STRUTS ASHISH SINGH TOMAR ast2124. OUTLINE Introduction The Model-View-Controller Design Pattern Struts’ implementation of the MVC Pattern Additional.
JSP / Servlets and Beans
JSP / Servet (2) Bayu Priyambadha, S.Kom. MVC (Model View Controller)  Model (data logic)  Java Class (standard class, servlet, JavaBeans, or other.
SDJ INFOSOFT PVT. LTD. 2 BROWSERBROWSER JSP JavaBean DB Req Res Application Layer Enterprise server/Data Sources.
Apache Struts Technology A MVC Framework for Java Web Applications.
CS320 Web and Internet Programming Web Application and MVC Chengyu Sun California State University, Los Angeles.
A Presentation Presentation On JSP On JSP & Online Shopping Cart Online Shopping Cart.
CS 562 Advanced Java and Internet Application Computer Warehouse Web Application By Team Alpha :-  Puja Mehta (102163)  Mona Nagpure (102147)
Java Web Development with NetBeans IDE -- Kai Qian Chapter 9 Case Study.
Enterprise Java v050228MVC1 Model, View, Controller Web Architecture.
Structure of a web application
Design and Maintenance of Web Applications in J2EE
Chengyu Sun California State University, Los Angeles
Presentation transcript:

MVC Model 2 Architecture & AddressBook case study Lec - 40

Last Lecture Review

Page-Centric Approach request database JSP JSP response Web-app = collection of JSPs + easy to get started (and carried away) - code is a mixture of presentation, business & data access logic: a maintenance nightmare - won’t scale - lots of duplication Credit: Java E-commerce – Martin Cooke

Page-with-Bean Approach (MVC Model 1) request database JSP bean JSP response Web-app = collection of JSPs + beans + factors some of the business logic into beans - insufficient separation of logics Credit: Java E-commerce – Martin Cooke

MVC Model 2 Architecture

MVC Model 2 Architecture Introduces a Controller Centralizes logic for Dispatching Requests to the next view based on: The Request URL Input Parameters Application state Single Point of Control For security & logging Encapsulates Incoming Data Into a form usable by the back-end MVC model.

MVC Model 2 Credit: www.javapassion.com

Program Flow (Page with-Bean approach) addperson .jsp saveperson .jsp uses addbookerror .jsp exception PersonInfo JavaBeans PersonDAO uses searchperson .jsp showperson .jsp

Program Flow (Page with-MVC approach) addperson .jsp saveperson .jsp controller (JSP/servlet) exception addbook error .jsp uses searchperson .jsp showperson .jsp PersonInfo PersonDAO JavaBeans

Case Study: Address Book using MVC Model 2 (JSP as controller) Addressbookmodel2ex1

Address Book using MVC Model 2 (ni) Add another JSP (controller.jsp) that Acts as a controller addperson.jsp & searchperson.jsp will submit requests to it. Identifies the page which initiates the request Uses JavaBeans to save or search persons to/from database Forwards or redirects the request to appropriate (saveperson.jsp or showperson.jsp) page

Give code of both example in handout (controller as servlet and JSP)

Introducing Servlet as Controller ? (ni) JSP as Controller Doing only processing – no view available Includes logic for selecting pages JSP is really not a good place for such logic JSPs are built for presentation (view) only. Solution: Use Servlet as Controller

Servlet Pure Servlet JSP Java Bean Public class OrderServlet … { public void doGet(…){ … if(bean.isOrderValid(…)){ bean.saveOrder(req); forward(“conf.jsp”); } Servlet Pure Servlet public class OrderServlet … { public void dogGet(…){ if(isOrderValid(req)){ saveOrder(req); … out.println(“<html><body>”); } private void isOrderValid(…){ private void saveOrder(…){ <html> <body> <c:forEach items=“${order}”> … </c:forEach> </body> </html> JSP Java Bean isOrderValid() saveOrder() ------------------ private state

Implementing JSP Model 2 (ni) As Servlet acts as a Controller Communication from servlet to JSP is necessary RequestDispatcher interface can be used to forward the request to another Servlet or JSP RequestDispatcher rd = request.getRequestDispatcher(“URL”); rd.forward(request, response);

Implementing JSP Model 2 (ni) Calling Error Pages from Servlet Servlet can use existing error pages Set the request attribute to javax.servlet.jsp.JspException with the exception you want to pass After that, forward the request to the error page

Implementing JSP Model 2 (ni) Calling Error Pages from Servlet (cont.) try{ ….. }catch (SQLException sqlex){ request.setAttribute(“javax.servlet.jsp.JspException” , sqlex); RequestDispatcher rd = request.getRequestDispatcher(“error.jsp”); rd.forward(rquest, response); }

Case Study: Address Book using MVC Model 2 (Servlet as controller) netbeans Project: addressbookusingmodel2ex2

Choosing between Model 1 & Model 2

MVC Model 1 (ni) Page Centric May encourage spaghetti JSP pages Business logic may get lost in display pages Use JavaBeans that captures business logic (instead of scriptlet) Page selection is done by each page JSPs are harder to debug than straight java code

MVC Model 2 (ni) Servlet Centric Loosens the coupling between the pages and improves the abstraction between presentation and business logic Use JSPs for pure data display and input collection activities Most of the business logic can be debugged through the servlet before passed to JavaBeans and JSP

JSP Model 2 Architecture (ni) Applying MVC Design Pattern Browser Servlet (Controller) Data 1 3 2 5 response request JSP (View) JavaBean (Model) instantiate 4 Model 2 binds Servlets and JSP pages to go hand-in-hand using MVC pattern. Servlet acts as the Controller and is in charge of the request processing and creation of any beans or objects (Models) used by the JSP. JSP is working as View and there is not much processing logic within the JSP page itself,it is simply responsible for retrieving objects and/or beans, created by the Servlet, extracting dynamic content from them and put them into the static templates. Model 2 takes advantage of predominant strengths of both technologies, using JSP to generate presentation layer and Servlets to perform process-intensive tasks. Here the servlet acts as the Controller and is in charge of the request processing and the creation of any beans or objects used the JSP, as well as deciding, depending on the user’s request, which JSP page to forward the request to. JSP is working as View and there is no processing logic within the JSP page itself, it is simply responsible for retrieving objects and/or beans, created by the Servlet, and extracting dynamic content from these objects/beans and put them in the static templates. This approach truly separate the contents from presentation, leads to better design and clearly defined responsibilities, so reduces the maintenance headaches.