Professional Documents
Culture Documents
Last updated:
8.12.2008
use cookies. At first use creates a new Session. Can be used anywhere except inside scripting elements and after <c:cookie expire= > tag. <c:cookie expire="1"> Outputs the cookie above mimetype. Can only be used once and no param-value pairs can be used after it. Flow Control IF tag: <c:if test="${number != 0}"> <c:/if> Inserts an if statement into the script, test=${ } uses the same syntax as standard C if. Foreach: <c:foreach var=i items=${array}> <c:/foreach> Not properly implemented yet. If the user wishes to use Cookies or supportlib functionality supportlib.hh must be included. Example runs: Test 1: ~/C++/CSP$ ./CSP demo.csp Test 2: ~/C++/CSP$ export REQUEST_METHOD="GET" ~/C++/CSP$ export QUERY_STRING="fname=Matti&lname=Meik%C3%A4l%C3%A4inen" ~/C++/CSP$ ./CSP demo2.csp INPUT(.csp) OUTPUT(.cc) <%@ page import="supportlib.hh" %> <%@ include file="header.csp" %> <% int number = 5; %> <c:if test="${number == 5}" > <% std::cout << "Numero tosiaan on viisi!" << std::endl; %> <c:/if> </body> </html> #include <iostream> #include "supportlib.hh" int main(void){ std::cout << "Contenttype:text/html;charset=iso-8859-1\r\n\r\n" << std::endl; std::cout << "<html>\n"; std::cout << "<body>\n"; std::cout << "<h1>Guest book</h1>\n"; std::cout << "<br>\n"; int number = 5; if (number == 5) { std::cout << "Numero tosiaan on viisi!" << std::endl; } std::cout << "</body>\n"; std::cout << "</html>\n"; } <%@ page import="supportlib.hh" %> <c:cookie param="demoID" value="666"> <c:cookie expire="1"> #include <iostream> #include "supportlib.hh" int main(void){
<html> <body> <h1>Support Library Demo</h1> <br> <% Support suplib; std::string firstname, lastname; firstname = suplib.getParam("fname"); lastname = suplib.getParam("lname"); %> <c:if test="${firstname.compare("") != 0}"> <% std::cout << "Nimesi on " << firstname << " " << lastname << std::endl; %> <c:/if> </body> </html>
Session session; session.setCookie("demoID", "666"); std::cout << session.setCookie(1) << std::endl; std::cout << "Contenttype:text/html;charset=iso-8859-1\r\n\r\n" << std::endl; std::cout << "<html>\n"; std::cout << "<body>\n"; std::cout << "<h1>Support Library Demo</h1>\n"; std::cout << "<br>\n"; Support suplib; std::string firstname, lastname; firstname = suplib.getParam("fname"); lastname = suplib.getParam("lname"); if (firstname.compare("") != 0) { std::cout << "Nimesi on " << firstname << " " << lastname << std::endl; } std::cout << "</body>\n"; std::cout << "</html>\n"; }
Program architecture
CSP-compiler consists of 4 modules, CSPCompiler (main) preprosesses the script and writes everything to a stringstream. This stringstream is then sent to the interpreter for further processing. The taglib parses all tags starting with <c: and returns the interpretion. Write has separate functions for writing headers, body and mimetype. Supportlibrary is included in the resulting c++ application and includes functions used at runtime to handle HTTP sessions and read parameters submitted via HTTP GET. If Interpreter completes successfully (no exceptions are thrown) main() writes the .cc file and uses g++ to compile it. The diagram below illustrates the structure of the program. The structure of the program evolved somewhat over the course of the project. The structure is fairly modular, simple and can be extended without too many changes. Taglib was intended to include a tag extension API but we didn't have enough time to implement it. Supportlibrary can easily be replaced with a user defined library. Every module has a clear purpose.
Class Diagram 1.
Known bugs
Supportlib::getParam() supports and other special characters but this only works on command line, not in the browser. Foreach is not properly implemented. We'll try to fix these shortcomings before the demo! Extra whitespaces within tags are generally not supported and will cause errors.
References
Stanley B. Lippman, Jose Lajoie, Barbara E. Moo: C++ Primer (4th Edition).