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.
Getting Started
To be productive with CSP you should have some familiarity with the following:
Some useful resources for learning HTML and JavaScript include:
The CSP Samples
Caché comes with a set of pre-built CSP sample pages. To view these:
  1. Start Caché.
  2. Make sure that the local Web server on your machine (such as IIS or Apache) is running.
  3. Start your browser and visit: http://localhost:1972/csp/samples/menu.csp
This should display a list of the various sample pages along with a short description.
The CSP Tutorial
Caché includes a detailed online tutorial, Building Web Applications With Caché, that covers building a Web application using Caché and CSP.
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.
Class-Based Development
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:
  1. Start the Caché Studio development environment.
  2. Create a new Project within the local database's USER namespace
  3. Create a new class definition using the New command within the File menu. This will invoke the New Class Wizard.
  4. On the first page of the Wizard, specify “Test” for a package name and “Hello” for a class name
  5. On the second page, select “CSP” from the list of class types.
  6. 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
        }
    }
  7. Within the generated OnPage method, replace the comment:
     ; To do...
    With a Write statement:
     Write "<b>Hello, World</b>",!
    
  8. Save and compile the new class using the Compile command within the Build menu.
  9. Start a browser and type in the following URL: http://localhost/csp/user/Test.Hello.cls
At this point you should see Hello, World” displayed within the browser.
This application works as follows:
  1. The browser sends a request for Test.Hello.cls to the local Web Server
  2. 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.
  3. The Caché CSP Server looks for a class called Test.Hello and invokes its OnPage method (which we edited in Caché Studio).
  4. 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:
  1. Start Caché Studio, invoke the New command within the File menu, and specify “New CSP Page”.
  2. 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>
    
  3. When you save the page the result is a .csp text file.
  4. Start your browser and type in the following URL: http://localhost/csp/user/Hello.csp
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:
  1. The browser sends a request for Hello.csp to the local Web Server
  2. 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.
  3. The Caché CSP Server looks for the file Hello.csp, and hands it to the CSP Compiler.
  4. 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.
  5. 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:
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.