Presentation is loading. Please wait.

Presentation is loading. Please wait.

Server-side Scripting

Similar presentations


Presentation on theme: "Server-side Scripting"— Presentation transcript:

1 Server-side Scripting
Martin Kruliš by Martin Kruliš (v1.3)

2 Web Server (Revision) Serving Static Pages ` Web Server Internet
Apache configuration /var/www/myweb/ ` HTTP Request GET /myweb/index.html ... Web Server Internet Client index.html HTTP Response HTTP/ OK Content-Length: 1019 Content-Type: text/html; ... <contents of index.html> by Martin Kruliš (v1.3)

3 Web Server (Revision) Serving Dynamic Content ` Web Server Internet
/var/www/myweb/ ` HTTP Request GET /myweb/app.cgi ... Web Server Internet app.cgi Client stdin stdout Please note the security issues of such solution. If the web server is public, anyone in the world can make your server start a process. Furthermore, your CGI application has to be very secure, especially when dealing with inputs. HTTP Response HTTP/ OK Content-Length: 2049 Content-Type: text/html; ... <contents generated by cgi> by Martin Kruliš (v1.3)

4 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 environ- ment variables, POST body is directed to std. input Response is taken from the std. output Note that the server must be configured to recognize, when to execute CGI application and when to simply send back a requested file. E.g., apache can be configured <Directory /home/*/public_html/cgi-bin> Options ExecCGI SetHandler cgi-script </Directory> Will set the server to treat all files in each cgi-bin of each Example 1 by Martin Kruliš (v1.3)

5 FastCGI CGI Issues Fast CGI Improvement
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š (v1.3)

6 Web Server – FastCGI Improving CGI ` Web Server FastCGI Server
HTTP Request GET /myweb/app.cgi ... Web Server FastCGI Server Internet Client HTTP Response HTTP/ OK Content-Length: 2049 Content-Type: text/html; ... <contents generated by cgi> by Martin Kruliš (v1.3)

7 Web Server (Revision) Integrating Scripting Modules ` Web Server
/var/www/myweb/ ` HTTP Request GET /myweb/index.php ... Web Server index.php Internet mod_php Client HTTP Response HTTP/ OK Content-Length: 1984 Content-Type: text/html; ... <contents generated by php> by Martin Kruliš (v1.3)

8 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š (v1.3)

9 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 (JavaScript) Example 3 by Martin Kruliš (v1.3)

10 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 FastCGI) exists WebForms 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š (v1.3)

11 ASP.NET ASP.NET WebForms Razor syntax MVC
Event-based model, events may be processed at server The forms automatically serializes the whole state Razor syntax Block starts 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š (v1.3)

12 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 JavaBeans 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) There are also other efforts to employ Java for web pages. For instance the Google Web Toolkit ( is a framework that allows you to design the whole web application as a Java application. It compiles the client-side parts of the code into JavaScript and automatically handlers the client-server communication. by Martin Kruliš (v1.3)

13 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š (v1.3)

14 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 To install Ruby on Rails on Linux, I recommend using Ruby Version Manager ( The rails framework is then installed privately in local homes. Installing rails globally (using sudo and/or deb packages) did not meet with success (at least not in my case). by Martin Kruliš (v1.3)

15 Integrated Web Server Dedicated Web Server for an Application `
HTTP Request GET /myweb/index ... Web Server Module ` Internet Client Database Module HTTP Response HTTP/ OK Content-Length: 1984 Content-Type: text/html; ... <contents generated by app> by Martin Kruliš (v1.3)

16 Node.js JavaScript Server-side Platform
Basically a Google V8 JavaScript engine compiled as CLI script interpreter V8 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š (v1.3)

17 Statistics 19th of November, 2017 Other sources may give you different numbers (it is statistics after all and it depends on your data selection process); however, PHP was the most used technology in all surveys that I have seen. by Martin Kruliš (v1.3)

18 Server-side Scripting
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) Three tier architecture allows also better scaling of server tiers. Database tier may be moved to a dedicated server or even replicated. Similarly, the business logic may be divided further in services running on different machines or even in cloud. by Martin Kruliš (v1.3)

19 Server-side Scripting
Specific Issues of the Server Side Traditional 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) Code replication issues (at client and server side) Modern (single page) web applications 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š (v1.3)

20 Application Design Event-driven Desktop GUI Application Event Event
Events are processed sequentially Event GUI Main Loop Background Processing Event All components can access main memory Operating Memory Application State by Martin Kruliš (v1.3)

21 Application State Data Directly Related with User Interaction
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š (v1.3)

22 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 JavaScript User Interface State Selected screen, user transaction progress, … URL, Cookies, Sessions, LocalStorage, … Logical State, Complex UI States E.g., Modifications in a opened data file Added to persisted state, but localized for each user To make things even more complicated, we have to consider scenarios like when the user clicks the refresh button, moves back and forth in history or if the network fails when a request is being processed. by Martin Kruliš (v1.3)

23 Web Application State Traditional Web Application Event Example 5
Database, Files, Session storage URL Cookies (POST Data) Application State Browser Application State Check integrity Event Batch-like processing New State Browsing, submitting form Processing Script Memory Example 5 Serialization/deserialization, encoding, … by Martin Kruliš (v1.3)

24 Web Application Architectures
Single Page Applications (SPA) Web Server Internet Client Browser downloads static content (HTML, JS, …) State is maintained at client only Server plays role of a HTTP frontend to a database HTML document and scripts AJAX, WebSockets, … ajax.php by Martin Kruliš (v1.3)

25 REST API Representational State Transfer (REST)
Server API which offers retrieval and manipulation with application resources in a HTTP-compliant way Resources are identified by URIs Operations are performed by HTTP requests REST formal constraints are Client-server model Stateless interface (no client context is cached at server) Cacheable (response defines whether it can be cached) Uniform interface Layered system (proxies, servers may be replicated) Applications that conform to REST constraints, are typically called “RESTful”. by Martin Kruliš (v1.3)

26 REST API Representational State Transfer (REST)
HTTP request methods reflect desired operations GET – retrieve the resource (nullipotent) POST – append new sub-entity in the resource PUT – insert/replace the resource (idempotent) DELETE – remove the resource (idempotent) Example API for photo gallery /gallery – collection of all galleries /gallery/kittens - photos in gallery with ID=kittens /gallery/kittens/kitten01 – photo kitten01 by Martin Kruliš (v1.3)

27 REST API REST Example GET POST PUT DELETE /gallery /gallery/kittens
(collection of galleries) /gallery/kittens (photos in gallery) …/kitten01 (single photo) GET Get the list of all galleries (JSON) Get the list of photos in the gallery (JSON) Get the image (jpeg) POST Create a new gallery Create a new photo in a gallery Not generally used. Perhaps for adding image metadata… PUT Replace list of galleries (atypical) Replace entire list of photos in gallery Replace/insert an image (of given ID) DELETE Empty the whole application Remove all photos and the gallery Remove the image by Martin Kruliš (v1.3)

28 Discussion by Martin Kruliš (v1.3)


Download ppt "Server-side Scripting"

Similar presentations


Ads by Google