Rogue Wave banner
Previous fileTop of DocumentContentsIndex pageNext file
HydraExpress Servlet Development Guide
Rogue Wave web site:  Home Page  |  Main Documentation Page

9.3 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.

//1Defines 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.
//2Forwards 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.

//1Sets the Content-Type of the response to "text/html".
//2Gets 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.

The function next retrieves the session and outputs information about the session.

//1Retrieves the session from the request. If a session for this client is active, this call returns that session. Otherwise, the servlet container creates a new session.
//2Prints the session ID. Each session the servlet container generates has a unique ID string.
//3Prints "true" if the session is newly created, "false" otherwise. Note that the println function follows the Java conventions for representing a boolean value.
//4Prints the time the session was created. This line retrieves an rwsf::DateTime from the session using getCreationTime(), then calls the asString() function of the rwsf::DateTime object.
//5Prints the time the session was last active.
//6Prints the session ID sent by the client. This may not be the ID of the active session. For example, if the client's previous session timed out, the client will request a session that no longer exists.
//7Prints "true" if the client requested a valid session, "false" otherwise.
//8Prints "true" if the client returned the session ID using a cookie, "false" if the client returned the session ID in the URL, or if the client did not return a session ID.

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.

//1Retrieves the value the client sent for the "INVALIDATE" parameter. If the request does not contain an "INVALIDATE" parameter, getParameter() returns the empty string.
//2Invalidates 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.

//1Retrieves the value the client sent for the "dataname" parameter.
//2Retrieves the value the client sent for the "datavalue" parameter.
//3Constructs a new rwsf::Attribute. An instance of rwsf::Attribute can hold an instance of an arbitrary class, provided that the class has an assignment operator and a default constructor.
//4Stores a copy of dataValue in attr using the insertion operator.
//5Stores the attribute attr in the session. The session contains a collection of attributes. Each attribute is indexed by name. Note that this line replaces any previous attribute with the same name.

The loop below extracts and prints the value of each session parameter.

//1Retrieves the names of the parameters. rwsf::Enumeration is a simple container that mimics the Java Enumeration interface. The container provides a simple interface for iterating over the items in the container.
//2Loops while there are more elements in the enumeration.
//3Retrieves the next name in the enumeration.
//4Retrieves the attribute associated with name.
//5Extracts the string within value to data, using the extraction operator. Note that a rwsf::Attribute can only be extracted to a value of the same type as the value inserted.

The rest of the function generates the HTML forms and finishes the HTML document.



Previous fileTop of DocumentContentsIndex pageNext file

© Copyright Rogue Wave Software, Inc. All Rights Reserved. All Rights Reserved. Rogue Wave is a registered trademark of Rogue Wave Software, Inc. in the United States and other countries. HydraExpress is a trademark of Rogue Wave Software, Inc. All other trademarks are the property of their respective owners.
Contact Rogue Wave about documentation or support issues.