Web Applications

Java, JavaScript, HTTP persistent client state mechanisms, i.e. Cookies

November 9th, 1998

Ari Muittari
Electrical and Communications Engineering
Helsinki University of Technology
ari.muittari@ntc.nokia.com

Abstract

This paper explains the above mentioned Web Applications (There is another paper written by Mika Leivo which covers HTTP, HTML, Browsers and Server side dynamics). The first part describes Java language and platform and presents some related products. The second part describes JavaScript, a script language widely used in the Web, and shortly explains its connections to other Web applications. The third part describes a stateful session with HTTP headers according to RFC 2109 and explains the Cookies.


Table of Contents

1. Java
1.1 Introduction
1.2 Language Features
1.3 Java Platform
1.3.1 Java Virtual Machine
1.3.2 Java programs
1.3.3 Application Programming Interface
1.3.4 Compile and Runtime Environments
1.3.5 About performance
1.4 Specifications
1.4.1 Standardization
1.4.2 White papers
1.5 Products
1.5.1 Products from Sun
1.5.2 JAMBALA - Ericsson's digital wireless IN Platform
2. JavaScript
2.1 Introduction
2.2 Components of JavaScript Language
2.2.1 Core language
2.2.2 Client-side JavaScript
2.2.3 Server-side JavaScript
2.3 LiveConnect
2.4 JavaScript vs. Java
3. HTTP Persistent Client State Mechanisms (Cookies)
3.1 Introduction
3.2 Specifications and Standards
3.3 State and Sessions
3.4 Origin Server (Server-side) Role
3.4.1 Phases of a session
3.4.2 Set-Cookie response header to User Agent
3.5 User Agent (Client-side) Role
3.5.1 Interpreting Set-Cookie
3.5.2 Rejecting Cookies
3.5.3 Cookie Management
3.5.4 Cookie request header to Origin Server
3.6 Example
3.7 User Agent Implementation
3.7.1 Implementation limits
3.7.2 Controlling privacy
4. Conclusions
5. References


1. Java

1.1 Introduction

The Java programming language has been introduced in late 1995 by Sun Microsystems. JavaSoft released a newer version of Java (JDK 1.1) in early 1997 and the next version (JDK 1.2) is coming soon. Java is becoming a platform of choice for application development in key areas such as Internet development, distributed computing, enterprise computing, and so on. With the Java platform being deployed in more and more widely-used operating systems, Java is well on its way to becoming the most important computing platform that promises "Write Once, Run Anywhere" capability [1].

1.2 Language features

Java originated as part of a research project at Sun to develop advanced software for a wide variety of network devices and embedded systems. The result was a language platform that has proven ideal for developing secure, network-based end-user applications which can be deployed in environments ranging from the Web and the desktop, to network-embedded systems. Sun desdcribes Java in its white paper The Java Language: An Overview [2] as following terms:

1.3 Java Platform

The Java platform differents from other platforms (like MS Windows or UNIX), it sits on top of these platforms (or sits directly on the hardware) and is designed to deliver and run interactive, dynamic and secure applets and applications on a networked computer system.

1.3.1 Java Virtual Machine

The Java Virtual Machine (JVM) is an abstract machine designed to hide the underlying operating system from Java programs. It is a software processor that sits on the top of the existing processors. The JVM can also be implemented directly by hardware (java chip processor). The JVM specification defines the exact interfaces and adapters required to port the JVM to any platform. JVM specification also defines a machine-independent class file format for compiled Java programs.

Figure 1. Java Platform

1.3.2 Java programs

The Java language and runtime environment enables two different kinds of programs:

The Java Server API enables server side programs:

Servlets make use of the Java standard extension classes in the packages javax.servlet (the basic Servlet framework) and javax.servlet.http (extensions of the Servlet framework). Typical uses for HTTP Servlets include:

1.3.3 Application Programming Interface

The application programming interface (API) provides a high level abstraction to low level system services, such as file I/O, process control, windowing system, etc. The Java language comes with a set of pre-defined classes which help user to implement complex tasks with minimum coding. APIs are grouped into Java packages by function. A package in Java provides a separate unique namespace. The APIs are divided into the Java Core APIs and the Java Standard Extension APIs.

Java Base APIs (version 1.1)

Java Standard Extensions API

The Java Standard Extension API extends the capabilities of Java beyond the Java Core API and contributes a lot to making Java into a software framework for various types of computing tasks. Examples of extension APIs are:

1.3.4 Compile and Runtime Environments

Java Development Kit (JDK)

The Java Development Kit is a software development and deployment platform which contains a wide set of software and tools that developers need to compile, debug, and run Java applets and applications. These tools are designed to be used from the command line. The current release of JDK is 1.1 (newest one is 1.1.7). The upcoming version JDK 1.2 offers improvements in functionality, performance, security and global support.

Figure 2. Java Development Toolkit

Java Runtime Environment (JRE)

The developer writes Java Language source code (.java files) and compiles it to bytecodes (.class files). These bytecodes are instructions for the Java Virtual Machine. To create an applet, the developer next stores these bytecode files on an HTTP server, and adds an <applet code=filename> tag to a Web page, which names the entry-point bytecode file.

When an end user visits that page, the <applet> tag causes the bytecode files to be transported over the network from the server to the end user's browser in the Java Platform. At this end, the bytecodes are loaded into memory and then verified for security before they enter the Virtual Machine.

Once in the Virtual Machine, the bytecodes are interpreted by the Intepreter, or optionally turned into machine code by the just-in-time (JIT) code generator, known more commonly as the JIT Compiler. The Interpreter and JIT Compiler operate in the context of the runtime system (threads, memory, other system resources). Any classes from the Java Class Libraries (API) are dynamically loaded as needed by the applet.

Once a Java application has been developed, the JRE can be bundled in as part of the final product. This is useful when the latest version of the Java runtime is not installed on the customer's machine, or if the runtime is not installed at all.

Figure 3. Source code is compiled to bytecodes, which are executed at runtime

1.3.5 About performance

There are many factors that affects the performance of Java. Some of them are [1]:

There are also some techniques to be used to gain higher performance [5]:

Currently Java programs which are compiled with JIT compilers can achieve 30 to 40 % of the performance of native C++ programs. It is estimated that Java could eventually attain 60 to 70 % of the speed of native C++, or even could match speed of native C++.

1.4 Specifications

1.4.1 Standardization

In March 1997 Sun standardized Java platform specificiations by applying to ISO/IEC JTC1 for recognition as a Publicly Available Specification (PAS) submitter. ISO/IEC JTC1 stands for International Organization for Standardization/International Electrotechnical Commission Joint Technical Committee. The new PAS process was designed by JTC1 to speed the conversion of de facto industry standards like the Java Platform into ISO International Standards [2].

1.4.2 White papers

There are many high-level, overview documents of different features and aspects of Java. These papers cover following topics:

1.5 Products

1.5.1 Products from Sun

There are many published and under development products. To name few of them:

1.5.2 JAMBALA - Ericsson's digital wireless IN Platform

Ericsson has adapted Java technology in a new system for running a telecommunications network. JAMBALA [6] was published on September 1998 by Ericsson being the first telecommunications product which fully leverage the Java platform.

Network nodes like service control point (SCP) and a home location register/authentication center (HLR/AC) are implemented directly in JAMBALA. As an intelligent network (IN) node JAMBALA system e.g. tracks subscriber information, locates subscribers, handles special services like call forwarding and voice mail and manages subscriber registration and record-keeping.

JAMBALA platform uses Ericsson's own TelORB operating system. Java Virtual Machine (JVM) runs on top of TelORB enabling the use of Java technology for the development of new applications. By using JVM in platform, operators can use JavaBean technology and commercially available software development tools (like Java Studio) to implement and customize end-user services.

2. JavaScript

2.1 Introduction

JavaScript is Netscape's cross-platform, object-based scripting language for client and server applications. Client applications run in a browser and server applications run on a server.

Javascript can be thought of as an extension to HTML which allows developers to include functionality on their HTML pages. Client-side objects are mainly the components of an HTML web page (forms, text boxes, buttons) and they allow processing of user input. Server-side objects are those that facilitate the handling of requests that come from clients and maintain persistent data using special objects, files, and relational databases. Applications can access Java and CORBA distributed-object applications through JavaScript's LiveConnect functionality [7,8].

2.2 Components of JavaScript Language

The JavaScipt language comprises of three components: Core language, Server-side and Client-side JavaSript.

Figure 4. The components of JavaScript language

2.2.1 Core language

Server-side and client-side JavaScript share the same core language. This core language corresponds to ECMA-262 (ECMAScript Language Specification) [9]. The European standards body ECMA has approved ECMA-262 and submitted it to ISO/IEC JTC 1 for adoption. The core language contains a set of core objects. It also defines object model and other language features such as its expressions, statements and operators. Although server-side and client-side JavaScript use the same core functionality, in some cases they use them differently [7].

Microsoft incorporated its version of the language, called JScript, in Internet Explorer 3. The core language part of JScript is essentially identical to that of JavaScript 1.1 in Netscape Navigator 3. However, these two browsers differs in their document object models [10].

2.2.2 Client-side JavaScript

Both client-side and server-side JavaScript includes the core language. Both of these includes also some additions of their own, like predefined objects.

Client-side JavaScript statements are embedded directly in HTML pages and are interpreted by the browser at runtime. Because production applications frequently have greater performance demands, JavaScript applications that take advantage of its server-side capabilities are compiled before they are deployed.

When the browser (or client) requests HTML page, the server sends the full content of the document, including HTML and JavaScript statements to the client. The client reads the page from top to bottom, displaying the results of the HTML and executing JavaScript statements as it goes. This process produces the results that the user sees [7].

Figure 5. Client-side JavaScript

Client-side JavaScript statements in an HTML page can respond to user events such as mouse-clicks, form input, and page navigation. Without causing any network transmission a JavaScript function can e.g. verify that users enter valid information into a form requesting a telephone number.

2.2.3 Server-side JavaScript

On the server JavaScript is also embedded in HTML pages. The server-side statements can connect to databases, share information across users of an application, access the file system on the server, or communicate with other applications through LiveConnect and Java.

HTML pages that use server-side JavaScript are compiled into bytecode executable files. These application executables are run in concert with a Web server that contains the JavaScript runtime engine. This makes creating JavaScript applications a two-stage process.

In the first stage the developer creates HTML pages and JavaScript files. These files are then compiled into a single executable.

Figure 6. Server-side JavaScript during development

In the second stage a client browser requests a page that was compiled into the application. The runtime engine finds the compiled representation of that page in the executable, runs any server-side JavaScript statements found on the page, and dynamically generates the HTML page to return. The result of executing server-side JavaScript statements might add new HTML or client-side JavaScript statements to the original HTML page. The runtime engine then sends the newly generated page over the network to the client, which runs any client-side JavaScript and displays the results.

Figure 7. Server-side JavaScript during runtime

In contrast to standard CGI programs, all JavaScript source is integrated directly into HTML pages. This makes development and maintenance easier. Server-side JavaScript's Session Management Service contains objects that can be used to maintain data that persists across client requests, multiple clients, and multiple applications. Server-side JavaScript's LiveWire Database Service provides objects for database access that serve as an interface to Structured Query Language (SQL) database servers [7].

2.3 LiveConnect

LiveConnect creates a connection between HTML elements, Java, JavaScript, and plug-ins [7]:

LiveConnect extends the object model of JavaScript to include objects and data types that are not a part of the HTML world. HTML, for instance, does not have a form element that displays real-time stock ticker data; nor does HTML have the capability to treat a sound file like anything more than a URL to be handed off to a helper application. With LiveConnect scripts can treat the applet that displays the stock ticker as an object whose properties and methods can be modified after the applet loads; scripts can also tell the sound when to play or pause by controlling the plug-in that manages the incoming sound file.

The backbone of the LiveConnect functionality in the browser is the Java Virtual Machine (JVM). Because LiveConnect is a proprietary Netscape technology, not all facilities are available in Internet Explorer [10].

2.4 JavaScript vs. Java

JavaScript and Java are similar in some ways but fundamentally different in others. The JavaScript language resembles Java but does not have Java's static typing and strong type checking. JavaScript supports most Java expression syntax and basic control-flow constructs. Java's object-oriented model means that programs consist exclusively of classes and their methods. Java's class inheritance and strong typing generally require tightly coupled object hierarchies.

In contrast to Java's compile-time system of classes built by declarations, JavaScript supports a runtime system based on a small number of data types representing numeric, Boolean, and string values. These requirements make Java programming more complex than JavaScript authoring.

Programming in JavaScript is easier to learn particularly of those who already know the basics of HTML and it requires only a text editor and a JavaScript-enabled browser. Java and JavaScript together makes a powerful combination through JavaScript's LiveConnect functionality [7,10].

JavaScript

Java

Interpreted (not compiled) by client.

Compiled bytecodes downloaded from server, executed on client.

Object-based. No distinction between types of objects. Inheritance is through the prototype mechanism and properties and methods can be added to any object dynamically.

Object-oriented. Objects are divided into classes and instances with all inheritance through the class hieararchy. Classes and instances cannot have properties or methods added dynamically.

Code integrated with, and embedded in, HTML.

Applets distinct from HTML (accessed from HTML pages).

Variable data types not declared (loose typing).

Variable data types must be declared (strong typing).

Dynamic binding. Object references checked at runtime.

Static binding. Object references must exist at compile-time.

Cannot automatically write to hard disk.

Cannot automatically write to hard disk.

Table 1. JavaScript compared to Java.

3. HTTP Persistent Client State Mechanisms (Cookies)

3.1 Introduction

HTTP as such is a stateless protocol. HTTP servers respond to each client request without relating that request to previous or subsequent requests [11].

CGI provides only a limited means for a server to identify its repeat visitors. CGI includes a variable sent from the visitor's browser to the server identifying the visitors IP address. Earlier this was useful because Internet users were assigned a unique IP address which could identify their machine. As WWW has grown, Internet Service Providers (ISP's) have reserved pools of IP addresses for customers. When accessing the ISP's computer, customer is assigned one of those IP addresses; and when connection is terminated, that address is returned to the pool for assignment to a different customer. CGI couldn't anymore identify a customer by tracking dynamic IP-addresses.

A great deal of the value of Web is the ability to create "dynamic pages", whose content change depending on information provided by the particular visitor. As demand for a more exact system of identifying repeat visitors increased, Netscape published a "Magic Cookie" in its browser in 1995 [12].

Cookie is a piece of information sent by a server to a browser who is expected to save it and send it back to the server whenever the browser makes additional requests from the server. Cookies may contain information such as login or registration information, user preferences, etc. When a server receives a request from a browser that includes a Cookie, the server is able to use the information stored in that Cookie. For example, the server may customize what is sent back to the user, or keep a log of particular user's requests [13].

Use of Cookies has increased tremendously since they offer numerous benefits to the Web services as well as individual users. Still many Web users are barely aware of cookies, which might be one reason for fear and denial of using them. Due to a lot of hype and misunderstanding people think that their privacy is intruded on by cookies. It is not always clear who is placing information in the Cookie file, and for what purpose.

3.2 Specifications and Standards

The specifications and standards relating to Cookies are [14]:

In this paper the following description of Cookies is based on HTTP State Management Mechanism, RFC 2109, Proposed Standard [11].

3.3 State and Sessions

A session is a series of HTTP requests and responses between clients and servers that want to exchange state information which relates to a larger context. This context might be used to create e.g. an online shopping cart in which user selections can be aggregated before purchase, or a magazine browsing system, in which a user's previous reading affects which offerings are presented.

There are many different potential contexts and thus many different potential types of session. The designers' paradigm for sessions created by the exchange of cookies has these key attributes:

The session here doesn't refer to a persistent network connection but to a logical session created from HTTP requests and responses.

3.4 Origin Server (Server-side) Role

3.4.1 Phases of a session

3.4.2 Set-Cookie response header to User Agent

The syntax of Set-Cookie response header is

set-cookie

=

"Set-Cookie:" cookies

cookies

=

1#cookie

cookie

=

NAME "=" VALUE *(";" cookie-av)

NAME

=

attr

VALUE

=

value

attr

=

token

value

=

token | quoted-string

cookie-av

=

"Comment" "=" value

 

|

"Domain" "=" value

 

|

"Max-Age" "=" value

 

|

"Path" "=" value

 

|

"Secure"

 

|

"Version" "=" 1*DIGIT

av comes from attribute-value pair, attr ["=" value]. Attributes are case-insensitive. White space is allowed between tokens. Most attributes require a value.

Set-Cookie response header comprises the token Set-Cookie:, followed by a comma-separated list of one or more cookies. Each cookie begins with a NAME=VALUE pair, followed by zero or more semi-colon-separated attribute-value pairs in any order.

3.5 User Agent (Client-side) Role

3.5.1 Interpreting Set-Cookie

The user agent keeps separate track of state information that arrives via Set-Cookie response headers from each origin server (as distinguished by name or IP address and port). The user agent applies these defaults for optional attributes that are missing:

3.5.2 Rejecting Cookies

To prevent possible security or privacy violations, a user agent rejects a cookie (shall not store its information) if any of the following is true:

Examples:

3.5.3 Cookie Management

If a user agent receives a Set-Cookie response header whose NAME is the same as a pre-existing cookie, and whose Domain and Path attribute values exactly (string) match those of a pre-existing cookie, the new cookie supersedes the old. However, if the Set-Cookie has a value for Max-Age of zero, the (old and new) cookie is discarded. Otherwise cookies accumulate until they expire.

Because user agents have finite space in which to store cookies, they may also discard older cookies to make space for newer ones, using, for example, a least-recently-used algorithm.

If a Set-Cookie response header includes a Comment attribute, the user agent should store that information in a human-readable form with the cookie and should display the comment text as part of a cookie inspection user interface.

User agents should allow the user to control cookie destruction. An infrequently-used cookie may function as a "preferences file" for network applications, and a user may wish to keep it even if it is the least-recently-used cookie. One possible implementation would be an interface that allows the permanent storage of a cookie through a checkbox.

3.5.4 Cookie request header to Origin Server

When it sends a request to an origin server, the user agent sends a Cookie request header to the origin server if it has cookies that are applicable to the request, based on the request-host, the request-URI and the cookie's age. The syntax for the header is:

cookie

=

"Cookie:" cookie-version

1*((";" | ",") cookie-value)

cookie-value

=

NAME "=" VALUE [";" path] [";" domain]

cookie-version

=

"$Version" "=" value

NAME

=

attr

VALUE

=

value

attr

=

token

value

=

token | quoted-string

path

=

"$Path" "=" value

domain

=

"$Domain" "=" value

The value of the cookie-version attribute must be the value from the Version attribute, if any, of the corresponding Set-Cookie response header. Otherwise the value for cookie-version is 0. The value for the path attribute must be the value from the Path attribute, if any, of the corresponding Set-Cookie response header. Otherwise the attribute should be omitted from the Cookie request header. The value for the domain attribute must be the value from the Domain attribute, if any, of the corresponding Set-Cookie response header. Otherwise the attribute should be omitted from the Cookie request header.

The following rules apply to choosing applicable cookie-values from among all the cookies the user agent has.

If multiple cookies satisfy the criteria above, they are ordered in the Cookie header such that those with more specific Path attributes precede those with less specific.

3.6 Example

Most detail of request and response headers has been omitted.

  1. User Agent -> Server. User identifies self via a form.
  2. POST /acme/login HTTP/1.1

  3. Server -> User Agent. Cookie reflects user's identity.
  4. HTTP/1.1 200 OK

    Set-Cookie: Customer="WILE_E_COYOTE"; Version="1"; Path="/acme"

  5. User Agent -> Server. User selects an item for "shopping basket."
  6. POST /acme/pickitem HTTP/1.1

    Cookie: $Version="1"; Customer="WILE_E_COYOTE"; $Path="/acme"

  7. Server -> User Agent. Shopping basket contains an item.
  8. HTTP/1.1 200 OK

    Set-Cookie:Part_Number="Rocket_Launcher_0001"; Version="1"; Path="/acme"

  9. User Agent -> Server. User selects shipping method from form.
  10. POST /acme/shipping HTTP/1.1

    Cookie: $Version="1"; Customer="WILE_E_COYOTE"; $Path="/acme"; Part_Number="Rocket_Launcher_0001"; $Path="/acme"

  11. Server -> User Agent. New cookie reflects shipping method.
  12. HTTP/1.1 200 OK

    Set-Cookie: Shipping="FedEx"; Version="1"; Path="/acme"

  13. User Agent -> Server. User chooses to process order.
  14. POST /acme/process HTTP/1.1

    Cookie: $Version="1"; Customer="WILE_E_COYOTE"; $Path="/acme"; Part_Number="Rocket_Launcher_0001"; $Path="/acme"; Shipping="FedEx"; $Path="/acme"

  15. Server -> User Agent. Transaction is complete.

HTTP/1.1 200 OK

The user agent makes a series of requests on the origin server, after each of which it receives a new cookie. All the cookies have the same Path attribute and (default) domain. Because the request URLs all have /acme as a prefix, and that matches the Path attribute, each request contains all the cookies received so far.

3.7 User Agent Implementation

3.7.1 Implementation limits

User agent implementations have limits on the number and size of cookies that they can store. User agents should provide each of the following minimum capabilities individually, although not necessarily simultaneously:

User agents created for specific purposes or for limited-capacity devices should provide at least 20 cookies of 4096 bytes, to ensure that the user can interact with a session-based origin server.

3.7.2 Controlling privacy

An origin server could create a Set-Cookie header to track the path of a user through the server. Users may think this behavior as an intrusive accumulation of information. It is required that a user agent gives the user control over such a possible intrusion, although this user interface is unspecified. The control mechanisms provided shall at least allow the user

Such control could be provided by, for example, mechanisms

It should be possible to configure a user agent never to send Cookie headers, in which case it can never sustain state with an origin server. When the user agent terminates execution, it should let the user discard all state information. Alternatively, the user agent may ask the user whether state information should be retained; the default should be "no". If the user chooses to retain state information, it would be restored the next time the user agent runs.

4. Conclusions

Java is a modern object oriented platform independent language and computing environment which has in a very short time acquired a recognized position as the language of Internet as well as in many other key areas, like embedded devices. JavaScript is an easy to use object based scripting language which allows wide audience of HTML page authors to add more functionality in their pages and at the same time minimizes the use of network bandwidth. Cookies make a Web site individual and customizable by letting Web application developers store information on the client so that this information is available from one session to another.

These three Web applications separately, and especially when used together, form a rich set of means to develope the use of Internet yet more versatile and user friendly.

5. References

[1] World Wide Web - Beyond the Basics, Prentice Hall, 1998 http://www.prenhall.com/abrams/

[2] Sun Microsystems' source for Java technology http://java.sun.com/

[3] Bruce Eckel. Thinking in Java, Prentice Hall, 1998 http://www.phptr.com

[4] Stefan Steiger, Servlet Essentials http://www.novocode.com/doc/servlet-essentials/

[5] Byte, May 1998, How to soup up Java

[6] JAMBALA - Ericsson's digital wireless IN Platform http://www.ericsson.se/Review/er3_98/art3/art3.html

[7] Netscape's DevEdge Online for Developers http://developer.netscape.com/

[8] A Beginner's Guide to JavaScript by Rajesh Vijayakumar http://jsguide.simplenet.com/

[9] ECMAScript Language Specification http://www.ecma.ch

[10] Danny Goodman's JavaScript Pages http://www.dannyg.com/javascript/index.html

[11] RFC 2109, HTTP State Management Mechanism, February 1997

[12] The Webmaster Report of Magic Cookies http://www.gh-interactive.com/WRTOC.HTML

[13] Glossary of Internet Terms http://www.matisse.net/files/glossary.html

[14] Cookie Specification http://portal.research.bell-labs.com/~dmk/cookie.html