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> RWSF_DEFINE_SERVLET(SessionExample) // 1 void SessionExample::doPost(rwsf::HttpServletRequest& request, rwsf::HttpServletResponse& response) { doGet(request, response); // 2 }
//1 | Defines servlet SessionExample for the servlet container. The RWSF_DEFINE_SERVLET macro expands to the code the container needs to load the servlet. Each servlet must be defined for the container as described in Section 6.5. |
//2 | Forwards the request and response to the doGet() function. |
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
//1 | Sets the Content-Type of the response to "text/html". |
//2 | Gets a reference to the output stream of the response. The class also provides print() and println() functions that mimic the Java methods. |
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() + "<br>"); // 4 out.println("SessionsLastAccessed "); out.println(session.getLastAccessedTime().asString()); // 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 }
//1 | Retrieves the value the client sent for the "INVALIDATE" parameter. If the request does not contain an "INVALIDATE" parameter, getParameter() returns the empty string. |
//2 | Invalidates the session if the client sent an "INVALIDATE" parameter. Note that when a session is invalidated, all of the attributes in the session are destroyed. |
If the client has not requested that the server invalidate the session, the servlet adds a new attribute, then prints the names and values of all the attributes in the session.
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 } out.println("<P>"); out.println("SessionData<br>");
The loop below extracts and prints the value of each session parameter.
rwsf::Enumeration<std::string> names = session.getAttributeNames(); // 1 while (names.hasMoreElements()) { // 2 std::string name = names.nextElement(); // 3 rwsf::Attribute value = session.getAttribute(name); // 4 std::string data; value >> data; // 5 out.println(name + " = " + data + "<br>"); } } // else
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>"); }
©2004-2007 Copyright Quovadx, Inc. All Rights Reserved.
Quovadx and Rogue Wave are registered trademarks of Quovadx, Inc. in the United States and other countries. All other trademarks are the property of their respective owners.
Contact Rogue Wave about documentation or support issues.