Serverside Scripting Martin Kruli by Martin Kruli v
Server-side Scripting Martin Kruliš by Martin Kruliš (v 1. 4) 29. 11. 2018 1
Web Server (Revision) � Serving Static Pages Apache configuration HTTP Request GET /myweb/index. html. . . /var/www/myweb/ ` Internet Client Web Server HTTP Response HTTP/1. 1 200 OK Content-Length: 1019 Content-Type: text/html; . . . <contents of index. html> index. html by Martin Kruliš (v 1. 4) 29. 11. 2018 2
Web Server (Revision) � Serving Dynamic Content HTTP Request /var/www/myweb/ GET /myweb/app. cgi. . . ` stdin Internet stdout Client Web Server app. cgi HTTP Response HTTP/1. 1 200 OK Content-Length: 2049 Content-Type: text/html; . . . <contents generated by cgi> by Martin Kruliš (v 1. 4) 29. 11. 2018 3
CGI � Common Gateway Interface ◦ One of the first standards for generating dynamic web content ◦ NSCA specification from 1993 how to invoke command line applications �Current version CGI 1. 1 (RFC 3875) from 2004 ◦ Specifies only the interface �Application may be written in any language �Important information and headers are set as environment variables, POST body is directed to std. input �Response is taken from the std. output Example 1 by Martin Kruliš (v 1. 4) 29. 11. 2018 4
Fast. CGI � CGI Issues ◦ Starting a process takes some system time ◦ Each process handles exactly one request �Unable to keep session/shared data in memory � Fast CGI Improvement ◦ Fast CGI server runs independently from web server �Keeps the process pool, resources, … ◦ Communicates with web server via socket/TCP ◦ Multi-request processing may be achieved by multiplexing or multiple connections (or both) by Martin Kruliš (v 1. 4) 29. 11. 2018 5
Web Server – Fast. CGI � Improving CGI HTTP Request GET /myweb/app. cgi. . . ` Internet Client Web Server Fast. CGI Server HTTP Response HTTP/1. 1 200 OK Content-Length: 2049 Content-Type: text/html; . . . <contents generated by cgi> by Martin Kruliš (v 1. 4) 29. 11. 2018 6
Web Server (Revision) � Integrating Scripting Modules HTTP Request /var/www/myweb/ GET /myweb/index. php. . . ` mod_php Internet Client Web Server index. php HTTP Response HTTP/1. 1 200 OK Content-Length: 1984 Content-Type: text/html; . . . <contents generated by php> by Martin Kruliš (v 1. 4) 29. 11. 2018 7
PHP � PHP: Hypertext Preprocessor ◦ Popular language originally designed for the web �The language has integrate API for handling requests �Things like URL parameters, POSTed data, headers, or server settings are presented in global variables ◦ PHP script code can be directly interleaved with HTML (or other type of generated content) �The script is embedded between <? php, ? > marks �The PHP interpret process the script and replace its body with its output in the document Example 2 by Martin Kruliš (v 1. 4) 29. 11. 2018 8
WSGI � Web Server Gateway Interface ◦ Universal interface between web servers and web applications designed for the Python language �Interface is called WSGI middleware and it is implemented by both sides (server and application) ◦ Specific new features �Routing requests to application objects (by URL) �Multiple applications may run in one process �Content post-processing (e. g. , by XSLT) �Load balancing (remote processing, forwarding, …) ◦ Similar APIs �Rack (Ruby), PSGI (Perl), JSGI (Java. Script) by Martin Kruliš (v 1. 4) Example 3 29. 11. 2018 9
ASP. NET � ASP. NET ◦ Microsoft solution built on. NET platform �Supports all. NET languages (C#, VB, …) ◦ Successor to Microsoft’s Active Server Pages ◦ Requires Microsoft IIS web server �Mono version (mod_mono and Fast. CGI) exists ◦ Web. Forms �Basic building blocks for ASP. NET web pages �Similar HTML interleaving syntax as PHP �The idea is to design web pages in the same manner as desktop applications by Martin Kruliš (v 1. 4) 29. 11. 2018 10
ASP. NET � ASP. NET ◦ Web. Forms �Event-based model, events may be processed at server �The forms automatically serializes the whole state ◦ Razor syntax �Block starts with @ and does not require explicit closing ◦ MVC �Alternative type of ASP. NET applications �Default view engine is either Razor (. cshtml, . vbhtml), or Web Forms (. aspx) �Controllers are. NET classes, methods are actions �Routers select controller class and invoke an action by Martin Kruliš (v 1. 4) 29. 11. 2018 11
JSP � Java Server Pages ◦ Java-based solution for dynamic web pages ◦ Requires web server with servlet container �Apache Tomcat, Jetty, … ◦ Supports both “simple” PHP-like approach and MVC �Uses <%, %> marks for scriptlet-HTML interleaving �MVC usually uses Java. Beans as the model and Java servlets as the controller ◦ Java compilation �Compiler is integrated in the web server and compiles the page when first needed (or when changed) by Martin Kruliš (v 1. 4) 29. 11. 2018 12
Other Java Alternatives � JSP is (almost) dead… ◦ Many alternatives �Spring MVC �Spring boot �JSF �Vaadin �Play 1, Play 2 �Struts 1, Struts 2 �GWT �Grails �Wicket �… by Martin Kruliš (v 1. 4) 29. 11. 2018 13
Ruby on Rails � Ruby on Rails ◦ Ruby scripting language + Rails web framework ◦ Basic philosophy �DRY (Don’t Repeat Yourself) – avoid code duplication �Convention Over Configuration – our way is the “best” ◦ Very strict style of application development �Improves efficiency, but ties your hands ◦ Specific structure of the application �Reflects the MVC pattern �$> rails new myapp �Generates new application structure in. /myapp by Martin Kruliš (v 1. 4) 29. 11. 2018 14
Integrated Web Server � Dedicated Web Server for an Application HTTP Request GET /myweb/index. . . ` Internet Web Server Module Client Database Module HTTP Response HTTP/1. 1 200 OK Content-Length: 1984 Content-Type: text/html; . . . <contents generated by app> by Martin Kruliš (v 1. 4) 29. 11. 2018 15
Node. js � Java. Script Server-side Platform ◦ Basically a Google V 8 Java. Script engine compiled as CLI script interpreter �V 8 is used in Chrome and it is the fastest JS interpreter ◦ Contains many pre-built packages for server-side application development (sockets, HTTP, …) �HTTP server is embedded in the application, so the programmer may tune it for specific needs ◦ Aims for fast developed single-language solutions �Using Javascript on client and server allows some level of code sharing Example 4 by Martin Kruliš (v 1. 4) 29. 11. 2018 16
Statistics Web Programming Languages Usage (November 2018) PHP ASP. NET Java Ruby Others Python Java. Script by Martin Kruliš (v 1. 4) 29. 11. 2018 17
Web Application Architectures � Client-server Architectures ◦ Strict separation of two application parts �Client - data presentation, user interface �Server – business logic, data storage ◦ Both sides are connected via specific API (HTTP) �The communication latency and overhead influence the application design ◦ Three-tier architecture �Presentation (client), business logic (server), database (server, possibly dedicated machine) by Martin Kruliš (v 1. 4) 29. 11. 2018 18
Web Application Architectures � Traditional Clicks on hyperlink Submits a form Client New page is displayed (browsing) (CGI-like) Applications HTTP Request new URL (possibly form data) Data management Business logic Authentication/authorization HTML rendering HTTP Response New HTML page Web Server by Martin Kruliš (v 1. 4) 29. 11. 2018 19
Web Application Architectures � AJAX-enhanced Java. Script performs AJAX request Clicks on hyperlink Submits a form Client New page is displayed (browsing) Java. Script alters page based on the response Applications HTTP Request AJAX target URL (possibly form data) HTTP Request new URL (possibly form data) Data management Business logic Authentication/authorization HTML rendering JSON/XML/… serialization HTTP Response New HTML page Web Server HTTP Response JSON, XML, HTML fragment, … by Martin Kruliš (v 1. 4) 29. 11. 2018 20
Web Application Architectures � AJAX-only Applications Java. Script performs AJAX request Page is opened or refreshed Client Java. Script alters page based on the response (no browsing) HTTP Request AJAX target URL (possibly form data) HTTP Request URL of bootstrap page Data management Partial business logic Authentication/authorization JSON/XML/… serialization HTTP Response Bootstrap HTML page Web Server HTTP Response JSON, XML by Martin Kruliš (v 1. 4) 29. 11. 2018 21
Web Application Architectures � Single Page Applications AJAX request ~ database operation Page is opened or refreshed Client Java. Script handles almost all business logic HTTP Request AJAX target URL (possibly form data) HTTP Request URL of bootstrap page REST (CRUD) API Thin layer above database Authentication/authorization HTTP Response Bootstrap HTML page Web Server HTTP Response JSON, XML by Martin Kruliš (v 1. 4) 29. 11. 2018 22
Server-side Scripting � Specific Issues of the Server Side ◦ Traditional (CGI-like) web applications �Work in batches – client wants to perform a large task with each HTTP request �Download a page full of formatted data �Submit a form and generate a response �Difficult state management (HTTP is stateless) �AJAX - Code replication issues (at client and server side) ◦ Modern (single page) web applications �Server is just a remote API for AJAX calls �Difficult to integrate AJAX API into existing applications, or create applications that work both ways by Martin Kruliš (v 1. 4) 29. 11. 2018 23
Application Design � Event-driven Desktop GUI Application Events are processed sequentially GUI Event Main Loop Event Background Processing All components can access main memory Operating Memory Application State by Martin Kruliš (v 1. 4) 29. 11. 2018 24
Application State � Data Directly Related to User Interactions ◦ UI and Logical State (non-persistent data) �Focus, mouse cursor location �Values filled in form controls �Which application screen is visible �Work in progress in an open transaction �… ◦ Persistent data �Has to survive application shutdown �Application files or database by Martin Kruliš (v 1. 4) 29. 11. 2018 25
Web Application State � Logical State and User Interface State ◦ Component Micro-States �Focus, open-selection of a control box, … �Handled in HTML/CSS or with little help from Java. Script ◦ User Interface State �Selected screen, user transaction progress, … �URL, Cookies, Sessions, Local. Storage, … ◦ Business Logic State, Complex UI States �E. g. , Modifications in a opened data file �Added to persisted state, but localized for each user by Martin Kruliš (v 1. 4) 29. 11. 2018 26
Web Application State � Traditional Browser Web Application URL Cookies (POST Data) Database, Files, Session storage Application State Check integrity Event Batch-like processing Browsing, submitting form Example 5 Processing Script New State Memory Serialization/deserialization, encoding, … by Martin Kruliš (v 1. 4) 29. 11. 2018 27
Discussion by Martin Kruliš (v 1. 4) 29. 11. 2018 28
- Slides: 28