Presentation is loading. Please wait.

Presentation is loading. Please wait.

M-V-C for web applications. Model for Web Applications model consists of data and system state database tables –persistent data session information –current.

Similar presentations

Presentation on theme: "M-V-C for web applications. Model for Web Applications model consists of data and system state database tables –persistent data session information –current."— Presentation transcript:

1 M-V-C for web applications

2 Model for Web Applications model consists of data and system state database tables –persistent data session information –current system state data business logic (eCommerce) –rules governing the transaction

3 View for web applications view gives a presentation of the model client-side presentation in a browser window –(D)HTML –CSS stylesheets –server-side templates administrative information –server output logs

4 Controller for web applications controller handles events user-generated events –client-side scripting –http request processing –redirection –preprocessing system maintenance –web application management

5 M-V-C Example PHP /CGI Web Server Web Browser presentationrequest processingprogram logic controllerviewmodel two-tier client-server architecture

6 M-V-C Example JSP /ASP /CF Web Server Web Browser entity database view controller view/ controller model multi-tier client server architecture

7 development of absence monitoring web app model data –student ids and names –number of absences model API –list names –list names and absences –update absences implement as a relational database

8 development of absence monitoring web app views –attendance register –list of absences –add a student –delete a student implemented in a browser –DHTML interface –pages dynamically generated from model

9 development of absence monitoring web app controller –handle requests for views generate correct page from the database –update the model translate user action into a database update –update the views refresh browser when view changes

10 build the model design data structure implement tables create SQL queries –support all required functionality test queries against sample data –this is a simple view

11 build the view develop server side scripts to query the database –SQL already tested is the model API design web pages and embed the scripts –view now updates from the model

12 build the controller add client side scripts –JavaScript –HMTL forms –input validation add navigation functionality –frames –layers update confirmation pages

13 web application frameworks

14 technologies designed to implement web apps in M-V-C –model 2 architecture provide standard re-useable components for model, view and controller greatly ease the design of large sophisticated web apps significant learning curve

15 web application frameworks typically xml configuration files glue components into an application implement standard web concepts –interface features (forms) –request and response objects –sessions –database interactions many frameworks exist

16 web application frameworks Many frameworks are being developed… –JavaServer Faces, Struts, Webwork2 –WebObjects (.NET specific) –Model Glue (ColdFusion specific) –Velocity, Fusebox, Mach II, Maypole, Catalyst, Tapestry, ZNF, Phrame, Cocoon, Ruby on Rails, … Most, but not all, are based around M-V-C

17 CGI programming

18 Common Gateway Interface interface between web server and other programs (cgi scripts) information passed as environment variables passed to standard input (STDIN) script outputs to standard output (STDOUT) output is http response message

19 CGI Environment Web Server defines –working directory –preset variables –filehandles (links to resources on the server) CGI script must produce –minimal set of response headers e.g. Content-Type: text/html –content of http response

20 Environment Variables provide info about the web server and the client information drawn from http request headers SERVER_NAME REMOTE_ADDR CONTENT_LENGTH CONTENT_TYPE

21 Server-Script interface STDIN –Web server launches CGI program and provides standard input STDOUT –CGI program outputs response to web server STDERR –Web server handles CGI program error output –Apache appends it to error log

22 CGI Output headers: –Content-Type print Content-Type:text/html\n\n; –Location print Location:someFile.html\n\n; –Status print 503 Service unavailable;

23 CGI Example


25 Ice Cream Stand Design Browser Web Server CGI Script Present order form and response Handle request and response Produce order form Process order form

26 CGI script design Input –Form data Output –Order form –Order response Self-referencing form

27 ice cream stand CGI script


29 CGI is programmer-oriented HTML embedded in the program HTML generated as a series of function calls requires –knowledge of HTML tags –programming skills

30 Does CGI implement M-V-C? No! Data processing (model) is inseparable from response page generation (view) Also contains elements of controller –Handles request headers and creates response headers

31 CGI security problems scripts can be corrupted by user data –hidden fields –arbitrary commands embedded in text fields file permissions file locations trust relationships between web server and other machines

32 speed of CGI each request creates a new process overhead of communication through CGI overhead of interpretation and compilation Possible solutions (only partly effective) –code optimisation –Fast CGI –mod_perl with Apache

33 Alternatives to CGI Java servlets JSP - Java Server Pages PHP ASP - Active Server Pages Coldfusion

34 Java Servlets

35 Servlets add functionality to a web server comparable to CGI –More tightly defined –Separate http handling from middleware –Deployed in a web container (see later) vendor and platform independent (Java) integrate with other Java technologies –J2EE framework

36 Servlets efficient –permanently available, no compile overhead robust –encapsulation, type-checking, error handling secure –specialised interfaces to other server resources that are not vulnerable to attack

37 Servlets implement javax.servlet.Servlet interface public void init(ServletConfig c) run on initialisation public void service (ServletRequest req, ServletResponse res) runs for each request and response public void destroy () end of servlet life

38 Web Server Servlet Class init(ServletConfig c) service(ServletRequest r, ServletResponse s) destroy() once at first request or at server start every request once when server shuts down webcontainer

39 HTTP servlets Most commonly used servlet subclass –javax.servlet.http.HttpServlet implements additional methods to handle http functionality service() method passes handling to more specific sub-class methods –doGet, doPost …

40 The Hello World servlet import*; import javax.servlet.*; import javax.servlet.http.*; public class HelloWorld extends HttpServlet{

41 The Hello World servlet public void doGet (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType(text/html); Printwriter out = res.getWriter();

42 The Hello World servlet out.println ( ); out.println (Hello World); out.println ( ); out.println ( Hello World ); out.println ( ); }

43 Servlets vs CGI similar idea –web container like CGI environment –request and response objects vs std I/O servlet compilation once only –much faster, even though run in JVM security problems greatly reduced –web container is much more secure but still HTML embedded in code

44 Java Server Pages

45 Java Server Pages (JSP) Template for page generation Separates code from HTML HTML with additional jsp tags processed on server side links to other Java entities for more complex processing/ database access platform independent

46 JSP elements A JSP is a template for generating a web page –Response to an http request JSP elements are tags embedded in HTML JSP scripting elements –Specify Java code to be run when template is requested –Separate the coding from HTML content Fits with M-V-C philosophy

47 JSP Digital Clock Date and Time Simple JSP Example


49 JSP scripting elements Three different kinds of scripting, determining when each is executed: Insert snippets of Java code embed a code expression, which evaluates in the response (no ;) declare variables and methods

50 Examples

51 result

Download ppt "M-V-C for web applications. Model for Web Applications model consists of data and system state database tables –persistent data session information –current."

Similar presentations

Ads by Google