Implementing C++ Servlet Containers
by John Hinke

Listing One
class HelloWorld extends HttpServlet {
  public void doGet(HttpServletRequest req, HttpServletResponse res) {
    response.setContentType("text/plain");
    try {
      PrintWriter out = response.getWriter();
      out.println(helloWorld("Hello C++"));
    } catch(IOException e) { /* ignore for now. */ }
  }
  private native String helloWorld(String in);
}

Listing Two
extern "C" {
  JNIEXPORT jstring JNICALL Java_HelloWorld_helloWorld
    (JNIEnv *, jobject, jstring);
}

Listing Three
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWorld extends HttpServlet {
  public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException
  {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    out.println("<html><head>");
    out.println("<title>Hello World</title>");
    out.println("</head>");
    out.println("<body bgcolor=\"white\">");
    out.println("<body>");
    out.println("<h1>Hello World!</h1>");
    out.println("</body></html>");
  }
}

Listing Four
// include the necessary classes
#include <servlet/ServletOutputStream.h>
#include <servlet/ServletException.h>
#include <servlet/http/HttpServlet.h>
#include <servlet/http/HttpServletRequest.h>
#include <servlet/http/HttpServletResponse.h>
class HelloWorld : public HttpServlet {
  public:
    virtual void doGet(HttpServletRequest& request, 
                                    HttpServletResponse& response)
    {
      response.setContentType("text/html");
      ServletOutputStream& out = response.getWriter();
      out.println("<html><head>");
      out.println("<title>Hello World</title>");
        // we can mix both println and << method calls!
      out << "</head>" << endl;
      out << "<body bgcolor=\"white\">" << endl;
      out << "<body>" << endl;
      out << "<h1>Hello World!</h1>" << endl;
      out << "</body></html>" << endl;
    }
};

Listing Five
interface Servlet {
  public ServletConfig getServletConfig();
}

Listing Six
class Servlet {
  public:
    ServletConfig getServletConfig() const;
};

Listing Seven
#include <servlet/ServletOutputStream.h>
#include <servlet/http/HttpServlet.h>
#include <servlet/http/HttpServletRequest.h>
#include <servlet/http/HttpServletResponse.h>
#include <servlet/http/HttpSession.h>
class SessionExample : public HttpServlet {
  public:
    virtual void doGet(HttpServletRequest& request,
                       HttpServletResponse& response)
    {
      response.setContentType("text/html");
      ServletOutputStream& out = response.getOutputStream();
      out.println("<html><body bgcolor=\"white\"><head>");
      out.println("<title>SessionExample</title>");
      out.println("</head><body>");
        // get the session from the request
      HttpSession session = request.getSession();
        // should this session be invalidated?
      std::string invalid = request.getParameter("INVALIDATE");
      if(invalid != ""){
        session.invalidate();
      } else {
          // set any attributes that are specified
        std::string name = request.getParameter("dataname");
        std::string value = request.getParameter("datavalue");
        if(name != "") {
          session.setAttribute(name, value);
        }
          // print out all attributes in this session.
        out.println("<P>");
        out.println("Session Data<br>");
        Enumeration<std::string> names =
          session.getAttributeNames();
        while(names.hasMoreElements()) {
          std::string name = names.nextElement(); 
          std::string value = session.getAttribute(name);
          out.println(name + " = " + value + "<br>");
        }
      }
      out.println("</body>");
      out.println("</html>");
    }
};


Listing Eight
interface HttpSession {
  public Object getAttribute(String name);
  public Enumeration getAttributeNames();
  public void setAttribute(String name, Object value);
}


Listing Nine
class HttpSession {
  public:
    std::string getAttribute(const std::string& name) const;
    Enumeration<std::string> getAttributeNames() const;
    void setAttribute(const std::string& name, const std::string& value);
};

Listing Ten
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app>
  <servlet>
    <servlet-name>HelloWorld</servlet-name>
    <servlet-class>com.mycompany.HelloWorld</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>HelloWorld</servlet-name>
    <url-pattern>/HelloWorld/*</url-pattern>
  </servlet-mapping>
</web-app>



Listing Eleven
extern "C" {
  Servlet *createHelloWorld() {
    return new HelloWorld;
  }
}

Listing Twelve
<servlet-class>mydll.createHelloWorld</servlet-class>


3

