Implementing the Servlet Class
The SessionExample servlet supports both the HTTP GET method and the HTTP POST method. The servlet creates the same output for either method, so the doPost() function is simple -- the function simply forwards the request and response to the doGet() function.
// SessionExample.cpp
#include "SessionExample.h"
#include <rwsf/servlet/http/HttpSession.h>
#include <rwsf/core/Attribute.h>
#include <rwsf/core/DateTime.h>
#include <rwsf/core/Enumeration.h>
#include <string>
RWSF_DEFINE_SERVLET(SessionExample) // 1
void
SessionExample::doPost(rwsf::HttpServletRequest& request,
rwsf::HttpServletResponse& response)
{
doGet(request, response); // 2
}
The doGet() function implements the behavior of the servlet. Because this servlet is simple, the function contains all of the servlet behavior. Further, the function freely mixes static content and dynamic content. A production application would take more care to separate the presentation and the logic, and to break the function into more manageable units.
void
SessionExample::doGet(rwsf::HttpServletRequest& request,
rwsf::HttpServletResponse& response)
{
response.setContentType("text/html"); // 1
rwsf::ServletOutputStream& out = response.getOutputStream(); // 2
The next several lines produce the header of the HTML document and begin the document body.
out.println("<html>");
out.println("<body bgcolor=\"white\">");
out.println("<head>");
std::string title = "SessionExample";
out.println("<title>" + title + "</title>");
out.println("</head>");
out.println("<body>");
out.println("<h3>" + title + "</h3>");
The function next retrieves the session and outputs information about the session.
rwsf::HttpSession session = request.getSession(); // 1
out.print("SessionID ");
out.print(session.getId()); // 2
out.println("<br>");
out.print("SessionIsNew ");
out.print(session.isNew()); // 3
out.println("<br>");
out.println("SessionCreated ");
out.println(session.getCreationTime().asString( // 4
rwsf::DateTime::http ) + "<br>");
out.println("SessionsLastAccessed ");
out.println(session.getLastAccessedTime()
.asString(rwsf::DateTime::http )); // 5
out.println("<br>");
out.println("SessionRequestedID " +
request.getRequestedSessionId() + "<br>"); // 6
out.print("SessionRequestedIDValid ");
out.print(request.isRequestedSessionIdValid()); // 7
out.println("<br>");
out.print("SessionFromCookie ");
out.print(request.isRequestedSessionIdFromCookie()); // 8
out.println("<br>");
The servlet checks to see if the client has asked to invalidate the session. If so, the servlet invalidates the session and skips ahead to generate the forms.
std::string invalidate = request.getParameter("INVALIDATE"); // 1
if(invalidate != ""){
session.invalidate(); // 2
}
If the client has not requested that the server invalidate the session, adds the parameter as a new attribute.
else {
std::string dataName = request.getParameter("dataname"); // 1
std::string dataValue = request.getParameter("datavalue");
// 2
if(dataName != "") {
rwsf::Attribute attr; // 3
attr << dataValue; // 4
session.setAttribute(dataName, attr); // 5
}
The loop below extracts and prints the value of each session parameter.
out.println("<P>"); // 1
out.println("SessionData<br>");
rwsf::Enumeration<std::string> names =
session.getAttributeNames(); // 2
while (names.hasMoreElements()) { // 3
std::string name = names.nextElement(); // 4
rwsf::Attribute value = session.getAttribute(name); // 5
std::string data;
value >> data; // 6
out.println(name + " = " + data + "<br>");
}
}
The rest of the function generates the HTML forms and finishes the HTML document.
out.println("<P>");
out.print("<form action=\"");
out.print(response.encodeURL("SessionExample"));
out.println("\" method=POST>");
out.println("SessionDataName");
out.println("<input type=text size=20 name=dataname>");
out.println("<br>");
out.println("SessionDataValue");
out.println("<input type=text size=20 name=datavalue>");
out.println("<br>");
out.println("<input type=submit>");
out.println("</form>");
out.println("<P>GET based form:<br>");
out.print("<form action=\"");
out.print(response.encodeURL("SessionExample"));
out.println("\" method=GET>");
out.println("SessionDataName");
out.println("<input type=text size=20 name=dataname>");
out.println("<br>");
out.println("SessionsDataValue");
out.println("<input type=text size=20 name=datavalue>");
out.println("<br>");
out.println("<input type=submit>");
out.println("</form>");
out.println("<P>");
out.println("<P>Invalidate session:<br>");
out.print("<form action=\"");
out.print(response.encodeURL("SessionExample"));
out.println("\" method=POST>");
out.println("<input type=\"hidden\" name=INVALIDATE value=TRUE>");
out.println("<input type=submit value=\"Invalidate session\">");
out.println("</form>");
out.print("<p><a href=\"");
out.print(response.encodeURL("SessionExample"));
out.println("?dataname=foo&datavalue=bar\">URL encoded</a>");
out.println("</body>");
out.println("</html>");
}