Main  /  Edit  /  History  /   /  Users Area

Tutorial: "Hello World!" - Code

First we need to include one header of cppcms library:

#include <cppcms/application.h>
#include <iostream>
using namespace std;
using namespace cppcms;

Then we implement our quite simple and useless web application:

class my_hello_world : public application {
public:
    my_hello_world(worker_thread &worker) :
        application(worker) 
    {
    };
    virtual void main();
};

Notes:

  1. The base class cppcms::application receives a reference to cppcms::worker_thread object as parameter in its constructor, thus, you should implement a constructor that receives this parameter and passes it to base class.
  2. The virtual member function "main" is actually the function that is called upon any page request.
void my_hello_world::main()
{
    cout<<"<html>\n"
          "<body>\n"
          "  <h1>Hello World</h1>\n"
          "</body>\n"
          "</html>\n";
}

This function writes to cppcms::application::cout (not std::cout) a "Hello World" html message.

Remark: Usually you will not use "cout" directly, it is usually used as an output for template engine.

int main(int argc,char ** argv)
{
    try {
        manager app(argc,argv);
        app.set_worker(new application_factory<my_hello_world>());
        app.execute();
    }
    catch(std::exception const &e) {
        cerr<<e.what()<<endl;
    }
}

We create an object of type cppcms::manager that controls general settings like: web server API, run time mode. cache and more. It parses command line arguments, reads configuration file and defines all required properties.

Then we need to create a factory class that would create objects that would process requests. The template cppcms::application_factory defines such factory for your class my_hello_world. Now we can call the main loop of our application execute()

This framework throws exceptions of type cppcms::cppcms_error that are derived from std::runtime_error and can be catch using std::exception.

Now we can build our application running:

gcc hello.cpp -o hello.fcgi -lcppcms

About

CppCMS is a web development framework for performance demanding applications.

Support This Project

SourceForge.net Logo

Поддержать проект

CppCMS needs You


Navigation

Main Page



Valid CSS | Valid XHTML 1.0