CPPSERV


Home Projects Jobs Clientele Contact
CPPSERV Documentation Download TODO Mailing lists Bug tracker News RSS Feed Browse source

HOWTO and other general documents

Getting started with CPPSERV guide

This document gives step-by-step instructions for creating your first web application using CSP and C++ Servlet technology.
  1. Set up
    1. Build and install CPPSERV with Apache module (detailed instructions are available at SourceForge).
    2. Enable Apache module and start apache. This step highly depends on distribution. For example, in Gentoo, which actually has a package for CPPSERV, all one has to do is edit /etc/conf.d/apache2 and add "-D CPPSERV" to APACHE2_OPTS variable. Another recommended setting is to enable mod_userdir (URLs of form http://my.server.com/~someuser), to make development a bit easier. For distributions that do not provide their own package for CPPSERV, one should edit httpd.conf, adding LoadModule directive. For more information on loading moduled into Apache see httpd.apache.org
    3. Configure apache<->cppserv connection on apache side. This is normally done in .htaccess in the directory which corresponds to virtual path servlets will be invoked through. For example: If we want to invoke servlets as http://localhost/~ilya/cppserv/HelloServlet we have to create /home/ilya/public_html/cppserv/.htaccess. Put following into the htaccess file:
      CServ On
      CServUnixPath /tmp/cppserv.sock
      
      CServ On line tells apache that any requests that map to this directory should be forwarded to CPPSERV for for processing. CServUnixPath /tmp/ilya-cppserv.sock is path to Unix-domain socket, where CPPSERV will be listening for requests. It should be accessible by both apache and you, and unique for each cppserv instance.
  2. Simple servlet
    Now let us create our first servlet. The servlet will accespt request, and respond with simple "Hello World" web page.

    1. Define HelloServlet class (class name can be any standard C++ class). Let's create it in hello.cpp file.
      #include <servlet/HttpServlet.h>
      #include <servlet/HttpServletRequest.h>
      #include <servlet/HttpServletResponse.h>
      class HelloServlet : public servlet::HttpServlet
      {
      public:
          virtual void service(servlet::HttpServletRequest& req, servlet::HttpServletResponse& resp)
          {
              std::ostream &out=resp.getOutputStream();
              out<<"&lt;HTML&gt;&lt;HEAD&gt;&lt;TITLE&gt;Hello World CPPSERV Servlet&lt;/TITLE&gt;&lt;/HEAD&gt;"
                   "&lt;BODY&gt;Hello World!&lt;/BODY&gt;&lt;/HTML&gt;";
          }
      };

      EXPORT_SERVLET(HelloServlet)
      
    2. Compile it into shared library. g++ -shared -o hello.so hello.cpp
    3. Create engine.xml<BR>
      <?xml version="1.0"?>
      <listener protocol="unix" path="/tmp/cppserv.sock"/>
      <app name="">
          <servlet name="HelloServlet" dso="./hello.so"/>
      </app>
      
    4. Start cppserv
      cppserv -P ./eng.pid -c ./engine.xml
    5. Test it out by opening http://localhost/~yourusername/cppserv/HelloServlet
  3. CSP (C++ Server Pages)
    Now it's time to create our first CSP. It will do exactly the same thing as hello world servlet, that is, output "Hello World" web page.
    1. First create actual CSP:
      <%@ page session="false" %>
      <html>
      <head>
          <title>Hello World CSP (C++ Server Pages) page</title>
      </head>
      <body>
      Hello World!
      </body>
      </html>
      
    2. Preprocess the hello.csp file: cxxsp_compile hello.csp csphello.cpp
    3. Compile dso: g++ -shared -o csphello.so csphello.cpp
    4. Add CSP section to engine.xml. Now engine.xml will look as follows:
      <?xml version="1.0"?>
      <listener protocol="unix" path="/tmp/cppserv.sock"/>
      <app name="">
          <servlet name="HelloServlet" dso="hello.so"/>
          <csp name="hello.csp" dso="./csphello.so"/>
      </app>
      
    5. Restart cppserv and test your first CSP at http://localhost/~yourusername/cppserv/hello.csp

SourceForge.net Logo