ICT role in 21st century education and its challenges
Unit 02: Web Technologies (2/2)
1. Unit 2: Web Technologies (2/2)
Core
Web browsers and web servers
URIs
HTTP
Client-Side
Basic: HTML, CSS
Advanced: JavaScript, DOM, AJAX, HTML 5, RIA
Server-Side technologies for Web Applications
CGI
PHP
Java Servlets
JavaServer Pages (JSPs)
dsbw 2011/2012 q1 1
2. Enabling Technologies for Web Applications
Web applications use enabling technologies to make their content
dynamic and to allow their users to affect the business logic.
While a simple web site requires only a Web server, a network
connection, and client browsers; Web applications include also an
application server to manage the business logic and state.
The application server can be located on the same machine as the
Web server and can even execute in the same process space as the
Web server.
The application server is logically a separate architectural element
since it is concerned only with the execution of business logic and
can use a technology completely different from that of the Web
server.
dsbw 2011/2012 q1 2
3. Session State Management
HTTP is a stateless protocol, so web servers do not keep track of
each user request nor associate it with the previous request.
However, many Web applications need to support users’ sessions:
each session represents a single cohesive use of the system,
involving many executable Web pages and a lot of interaction with
the business logic.
The session state keeps track of a user's progress throughout the
use case session. Without a session state management mechanism,
you would have to continually supply all previous information
entered for each new Web page.
Ways to store session state:
Client Session State: URI rewriting, Cookies, Hidden fields in web
forms
Application Server Session State: In-memory dictionary or map.
Database Server State.
dsbw 2011/2012 q1 3
4. Cookies
A cookie is a piece of data that a Web server ask a Web browser to
hold on to, and to return when the browser makes a subsequent
HTTP request to that server.
Limited size: between 100 and 4K bytes.
Initially, a cookie is sent from a server to a browser by adding a line
to the HTTP headers:
Content-type: text/html
Set-Cookie: sessionid=12345; path=/; expires Mon, 09-Dec-2002
11:21:00 GMT; secure
A cookie can have up to six parameters:
Name (required)
Value (required)
Expiration date
Path
Domain
Requires a secure connection
dsbw 2011/2012 q1 4
5. Common Gateway Interface (CGI)
It is a standardized interface to allow Web servers to talk to back-
end programs and scripts that can accept input (e.g., from forms)
and generate HTML pages in response
Typically, these back-ends are scripts written in the Perl (Python or
even some UNIX shell) scripting language.
By convention, CGI scripts live in a directory called cgi-bin, which is
visible in the URI :
http://www.enwebats.com/cgi-bin/enwebats.pl
CGI’s major drawbacks:
At each HTTP request for a CGI script the Web server spawns a new
process, which is terminated at the end of execution.
CGI, like HTTP, is stateless. Therefore, terminating the process where
the CGI script is executed after each request prevents the CGI engine
to retain session state between consecutive user requests.
dsbw 2011/2012 q1 5
7. CGI: User Input Forwarding
METHOD=GET
User input is appended to the requested URI: parameters are
encoded as label/value pairs appended to the URI after a
question mark symbol.
The Web server initializes a environment variable called
QUERY_STRING with the label/value pairs.
In most OSs, environment variables are bounded to 256/1024
chars.
METHOD=POST
User input – encoded as label/value pairs too – is attached to
the HTTP request using the message body.
Upon receipt of a POST request, the Web server extracts the
label/value pairs from the message body, and writes them on
the standard input of the CGI script.
dsbw 2011/2012 q1 7
15. PHP: PHP: Hypertext Preprocessor
Scripting language that is executed on the Web server (e.g.
Apache: mod_php)
PHP files are basically HTML documents with PHP code
embedded through special tags.
PHP syntax resembles C’s or Perl’s. Version 5 is more object
oriented.
Works well with major DBMSs
Is one of the mainstays of the LAMP platform: Linux + Apache
+ MySQL + PHP
Very popular, with a huge community of developers and tool
providers.
dsbw 2011/2012 q1 15
18. Java Servlets
A Servlet is an object that receives a request and generates a
response based on that request.
A Web container is essentially the component of a Web server that
interacts with the servlets. The Web container is responsible for
Managing the lifecycle of servlets.
Mapping a URI to a particular servlet.
Ensuring that the URI requester has the correct access rights.
Servlets’ strong points:
A servlet is initialized only once. Each new request is serviced in its
own separate thread.
The Servlet API easies common tasks like to access request
parameters, create and manage session objects, etc.
It’s Java, 57up1d! (cross-platform, object-oriented, EJB, JDBC, RMI, …)
dsbw 2011/2012 q1 18
19. Java Servlets: Request Scenario
:WebBrowser :WebServer :Servlet
Container
:Servlet
init
Submit
Envia Dades req. http
(POST) req. http
(POST) service
doPost
resp. http
resp. http
destroy
X
dsbw 2011/2012 q1 19
21. Exemple “EnWEBats”: Enwebats.java
public class Enwebats extends javax.servlet.http.HttpServlet
{
public void doPost(javax.servlet.http.HttpServletRequest req,
javax.servlet.http.HttpServletResponse res)
throws javax.servlet.ServletException, java.io.IOException {
res.setContentType("text/html");
java.io.PrintWriter output = res.getWriter();
output.println("<html>");
output.println("<title>Subscription OK</title>");
output.println("<body bgcolor=white>");
output.println("<h3>Your subscription have been processed successfully:</h3>");
output.println("<table border=0>");
java.util.Enumeration paramNames = req.getParameterNames();
while (paramNames.hasMoreElements()) {
String name = (String) paramNames.nextElement();
String value = req.getParameter(name);
output.println("<tr><td>"+name+": </td><td>"+value+"</td></tr>"); }
output.println("</table></body></html>");
output.close(); }}
dsbw 2011/2012 q1 21
22. JavaServer Pages (JSP)
JSPs allows creating easily Web content that has both static and
dynamic components.
JSPs project all the dynamic capabilities of Java Servlet technology
but provides a more natural approach to creating static content.
The main features of JSPs technology are:
A language for developing JSP pages, which are text-based documents
that describe how to process a request and construct a response
Constructs for accessing server-side objects
Mechanisms for defining extensions to the JSP language
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
(HTML, XML, …)
JSP elements, which construct dynamic content.
dsbw 2011/2012 q1 22
23. JSP and Java Servlets
When a Web container is requested a JSP it may happen that:
1. The JSP had not been requested yet: The web container
compiles and initializes a servlet to service the request.
2. The JSP had already been requested: The Web container
forwards the request to the corresponding running servlet.
Therefore, JSPs have all the benefits of using Java Servlets
and, at the same time, allows separating more clearly the
application logic from the appearance of the page
That, in turn, allows
using any Web edition tool or suite.
fast development and testing
dsbw 2011/2012 q1 23
24. JSP Elements (1/2)
JSP Directives: Instructions that are processed by the JSP engine when
the page is compiled to a servlet. Directives are used to set page-level
instructions, insert data from external files, and specify custom tag
libraries. Example:
<%@ page language=="java" imports=="java.util.*" %>
JSP Declaratives: Definitions of global variables and methods.
<%! int time = Calendar.getInstance().get(Calendar.AM_PM); %>
JSP Scriptlets: Blocks of Java code embedded within a JSP page.
Scriptlet code is inserted verbatim into the servlet generated from the
page, and is defined between <% and %>.
JSP Expressions: Expressions that are evaluated inside the
corresponding servlet to be inserted into the data returned by the web
server. Example:
<h2> List of Members for the year <%= clock.getYear() %> </h2>
dsbw 2011/2012 q1 24
25. JSP elements (2/2)
JSP Tags (or Actions): elements that encapsulate common
tasks.
Some are standard, which allow forwarding requests to other
JSPs or servlets (<jsp:forward>), instantiate JavaBeans
components (<jsp:useBean>), etc. Example:
<jsp:useBean id=="clock" class=="jspCalendar" />
Some are custom, that is, they are defined by Java developers
using the JSP Tag Extension API. Developers write a Java class
that implements one of the Tag interfaces and provide a tag
library XML description file that specifies the tags and the java
classes that implement the tags.
dsbw 2011/2012 q1 25
26. “EnWEBats” example: enwebats.jsp
<%@ page language="java" import="java.util.*" %>
<html>
<head>
<title>Subscription OK</title>
</head>
<body bgcolor=white>
<h3>Your subscription have been processed successfully:</h3>
<BR>
<BR>
<table border=0 width=50%>
<% Enumeration paramNames = request.getParameterNames();
while (paramNames.hasMoreElements())
{
String name = (String)nomsParams.nextElement();
String value = request.getParameterValues(name)[0];%>
<tr>
<td><%= name %>:</td><td><%= value %></td>
</tr>
<% } %>
</table>
</body>
</html>
dsbw 2011/2012 q1 26