HOWTO and other general documents
This document gives step-by-step instructions for creating your first web application using CSP and C++ Servlet technology.
-
Set up
-
Build and install CPPSERV with Apache module (detailed instructions are available at SourceForge).
-
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
-
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.
-
Simple servlet
Now let us create our first servlet. The servlet will accespt request, and respond with simple "Hello World" web page.
-
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<<"<HTML><HEAD><TITLE>Hello World CPPSERV Servlet</TITLE></HEAD>"
"<BODY>Hello World!</BODY></HTML>";
}
};
EXPORT_SERVLET(HelloServlet)
-
Compile it into shared library.
g++ -shared -o hello.so hello.cpp
-
Create engine.xml<BR>
<?xml version="1.0"?>
<listener protocol="unix" path="/tmp/cppserv.sock"/>
<app name="">
<servlet name="HelloServlet" dso="./hello.so"/>
</app>
-
Start cppserv
cppserv -P ./eng.pid -c ./engine.xml
-
Test it out by opening http://localhost/~yourusername/cppserv/HelloServlet
-
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.
-
First create actual CSP:
<%@ page session="false" %>
<html>
<head>
<title>Hello World CSP (C++ Server Pages) page</title>
</head>
<body>
Hello World!
</body>
</html>
-
Preprocess the hello.csp file:
cxxsp_compile hello.csp csphello.cpp
-
Compile dso:
g++ -shared -o csphello.so csphello.cpp
-
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>
-
Restart cppserv and test your first CSP at http://localhost/~yourusername/cppserv/hello.csp