Caché Server Pages (CSP) technology from InterSystems Corporation allows
you to build and deploy high-performance, highly-scalable Web applications. CSP lets
you dynamically generate Web pages, typically using data from a Caché database.
These pages are dynamic in that the same page may deliver different content each time
it is requested.
CSP is versatile. It can display inventory data that changes minute by minute.
It can support Web communities with thousands of active users. It can personalize
pages based on user information stored within the Caché database. It can customize
pages based on the user data to different users, depending on their requirements and
their security permissions. It can serve up HTML, XML, images, or other binary or
textual data. And it can do all this fast, because it is tightly coupled to the high-performance
Caché database.
CSP is well-suited for database applications. In addition to providing rapid
access to the built-in Caché database, it provides a number of features essential
for Web-based database applications including session management, page authentication,
and the ability to perform interactive database operations from within a Web page.
CSP supports two styles of Web development. For those who wish to develop applications
programmatically, CSP provides an object framework
for creating dynamic Web pages using classes. For those who prefer to develop applications
using
HTML files, CSP provides an HTML-based
markup language that allows the inclusion of objects and server-side scripts within
Web pages. You can combine these two techniques within an application for maximum
flexibility.
Caché Server Page requests are processed using a standard Web server
(all the leading servers are supported) and the standard HTTP protocol. An HTTP client,
typically a Web browser, requests a page from a Web server using HTTP. The Web server
recognizes this as a CSP request and forwards it to Caché using a fast server
API. The CSP Server running in Caché processes the request and returns a page
to the Web server, which in turn sends it to the browser. CSP manages communications
between the Web server and Caché and invokes application code to generate the
page.
The Web Server and the Caché server are abstract components, which may
be implemented by one or many computers. During development, all three components
may be on a single PC. In a large scale deployment, there may be multiple Web and
Caché servers in two- or three-tier configurations.
To keep this guide simple, it treats these components as though there were one
of each. It also describes CSP as though it was only serving HTML pages, although
almost everything applies directly to XML and other text formats and most things apply
to binary formats such as images.
To be productive with CSP you should have some familiarity with the following:
-
Caché Objects and Caché ObjectScript
-
-
-
Some useful resources for learning HTML and JavaScript include:
Caché comes with a set of pre-built CSP sample pages. To view these:
-
-
Make sure that the local Web server on your machine (such as IIS or
Apache) is running.
-
If you have installed Caché with the default port, this should display
a list of the various sample pages along with a short description. (If you have turned
on any of the advanced security features in Caché, this may display a login
page.)
Your First CSP Application
No introduction to a development technology is complete without the ubiquitous
Hello,
World example. This section includes a demonstration of how to create a
Hello,
World Web page using CSP in two different ways. First, there is the programmatic
version of creating a Web page objects, then there is one that shows how to create
the same page using a marked-up HTML file.
You can use CSP to create dynamic Web pages by creating a subclass of the
%CSP.Page class
and overriding its
OnPage method. Any output written to the
principal device by this method is automatically sent to a Web browser and displayed
as a Web page.
To create a simple,
Hello World page programmatically, do the
following:
-
Start the Caché Studio development environment.
-
Create a new Project within the local database's USER namespace
-
Create a new class definition using the New command within the File
menu. This will invoke the New Class Wizard.
-
On the first page of the Wizard, specify
Test for a
package name and
Hello for a class name
-
On the second page, select
CSP from the list of class
types.
-
Press the
Finish button. At this point you
will see the definition of a new CSP class within the Studio Class Editor:
Class Test.Hello Extends %CSP.Page [ ProcedureBlock ]
{
ClassMethod OnPage() As %Status
{
&html<<html>
<head>
</head>
<body>>
;To do...
&html<</body>
</html>>
Quit $$$OK
}
}
-
Within the generated
OnPage method, replace
the comment:
Write "<b>Hello, World</b>",!
-
Save and compile the new class using the Compile command within the
Build menu.
-
This application works as follows:
-
The browser sends a request for
Test.Hello.cls to
the local Web Server
-
The Web Server passes this request to the CSP Gateway (connected to
the Web Server) which, in turn, passes the request to a Caché CSP Server. In
our case the browser, Web server, and Caché Application server are all running
on the same machine. In a real deployment, these would probably be on separate machines.
-
The Caché CSP Server looks for a class called
Test.Hello and
invokes its
OnPage method (which we edited in Caché
Studio).
-
Any output that the
OnPage method writes
to the principal device (using the Write command) is sent back to the browser (via
the CSP Gateway and the Web Server).
These steps are at the heart of CSP: The rest of CSP's functionality is built
on top of this behavior.
If you like, you can make your example application more interesting by adding
more code. For example, try inserting the following lines after the line containing
Hello,
World:
Write "<ul>",!
For i = 1:1:10 {
Write "<LI> This is item ", i,!
}
Write "</ul>",!
Now your page contains an unordered (bulleted) list of 10 items. Note that in
this context Caché uses the
! character to write a carriage
return to the principal device.
HTML File-Based Development
Another way to use CSP is to create an HTML file and let the CSP Compiler transform
it into a CSP class.
To create a
Hello,World page using a marked-up HTML file, do
the following:
-
Start Caché Studio, invoke the New command within the File
menu, and specify
New CSP Page.
-
Edit the new CSP file within the Studio CSP Editor (which offers HTML/CSP
syntax coloring) and add the following:
<html>
<body>
<b>Hello, World!</b>
</body>
</html>
-
When you save the page the result is a
.csp text
file.
-
As with the previous example, you should see
Hello, World displayed
within the browser.
Note:
You can also create a marked-up HTML file using a text editor or HTML editor.
Save this file as
Hello.csp in the local directory
/cachesys/csp/user (where
cachesys is
where you have installed Caché).
This application works as follows:
-
The browser sends a request for
Hello.csp to
the local Web Server
-
The Web Server passes this request to the CSP Gateway (connected to
the Web Server) which, in turn, passes the request to a Caché CSP Server.
-
The Caché CSP Server looks for the file
Hello.csp,
and hands it to the CSP Compiler.
-
The CSP Compiler creates a new class called
csp.Hello with
an
OnPage method that writes out the contents of the
Hello.csp file.
(It actually generates a set of methods each of which are, in turn, called from the
OnPage method).
This compilation step only occurs when the
.csp file is newer
than the generated class; subsequent requests are sent directly to the generated class.
-
The CSP Server now invokes the newly generated
OnPage method
and its output is sent to the browser as in the previous example.
As with the case of programmatic development, this is a purposefully oversimplified
example included for pedagogical reasons. The CSP compiler is actually a specialized
XML/HTML processing engine that can:
-
Processing server-side scripts and expressions within an HTML page
-
Performing server-side actions when certain HTML tags are recognized.
As with the programmatic example, you can make this page more interesting by
adding programming logic. For example:
<html>
<body>
<b>Hello, World!</b>
<script language="CACHE" runat="server">
// this code is executed on the server
Write "<ul>",!
For i = 1:1:10 {
Write "<li> This is item ", i,!
}
Write "</ul>",!
</script>
</body>
</html>
As with the programmatic example, the resulting page displays an unordered (bulleted)
list of 10 items.