mod_servlet
C++Servlets
|
Defines a set of methods that a servlet uses to communicate with its servlet container. More...
#include <context.h>
Public Types | |
typedef std::map< std::string, std::string, std::less<> > | init_params_map |
Type definition for initial parameters map. | |
Public Member Functions | |
virtual | ~servlet_context () noexcept=default |
virtual destructor | |
const std::string & | get_context_path () const |
Return the main path associated with this context. More... | |
const std::string & | get_webapp_path () const |
Returns filesystem path of current web application. More... | |
template<typename T , typename KeyType > | |
optional_ref< const T > | get_attribute (const KeyType &key) const |
Returns the servlet container attribute with the given name, or empty reference if there is no attribute by that name. More... | |
template<typename KeyType , typename ValueType > | |
optional_ref< const ValueType > | get_attribute (const KeyType &key) const |
Finds attribute by key and casts it to specified ValueType. More... | |
template<typename ValueType > | |
bool | set_attribute (const std::string &key, ValueType &&value) |
Binds an object to a given attribute name in this servlet context. More... | |
template<typename ValueType > | |
bool | set_attribute (std::string &&key, ValueType &&value) |
Move version of set_attribute(const std::string&, ValueType&&) More... | |
template<typename KeyType > | |
bool | remove_attribute (const KeyType &key) |
Removes the attribute with the given name from the servlet context. More... | |
tree_any_map & | get_attributes () |
Returns an tree_map containing all the attributes available within this servlet context. More... | |
const tree_any_map & | get_attributes () const |
Const version of get_attributes method. More... | |
template<typename KeyType > | |
optional_ref< const std::string > | get_init_parameter (const KeyType &key) const |
Returns a reference to a std::string containing the value of the named context-wide initialization parameter, or an empty reference if the parameter does not exist. More... | |
const init_params_map & | get_init_parameters () const |
Returns the context's initialization parameters map of std::string objects. More... | |
virtual optional_ref< const std::string > | get_mime_type (string_view file_name) const =0 |
Returns the MIME type of the specified file, or null if the MIME type is not known. More... | |
Protected Member Functions | |
servlet_context (const std::string &ctx_path, const std::string &webapp_path, init_params_map &&init_params) | |
Protected constructor to be used from derrived classes. More... | |
Protected Attributes | |
const std::string & | _ctx_path |
Context path. | |
tree_any_map | _attr_map |
Attributes map (to be filled by inheriting class). | |
init_params_map | _init_params_map |
Initial parameters map. | |
const std::string & | _webapp_path |
Filesystem path of current webapp. | |
Defines a set of methods that a servlet uses to communicate with its servlet container.
There is one context per "web application". (A "web application" is a collection of servlets and content installed under a specific subset of the server's URL namespace such as /catalog
and possibly installed via a .war
file.)
In the case of a web application marked "distributed" in its deployment descriptor. In this situation, the context cannot be used as a location to share global information (because the information won't be truly global). Use an external resource like a database instead.
The servlet_context
object is contained within the servlet_config object, which the Web server provides the servlet when the servlet is initialized.
|
inlineprotected |
Protected constructor to be used from derrived classes.
ctx_path | Context path |
webapp_path | Filesystem path to the web application |
init_params | Initial parameters map |
|
inline |
Returns the servlet container attribute with the given name, or empty reference if there is no attribute by that name.
An attribute allows a servlet container to give the servlet additional information not already provided by this interface. See your server documentation for information about its attributes. Full attributes map can be retrieved using get_attributes
.
The attribute is returned as a reference to any
.
T | type of the value to return |
KeyType | a type comparable to std::string |
key | a std::string specifying the name of the attribute |
any
containing the value of the attribute, or empty reference if no attribute exists matching the given name any_bad_cast | if the stored type cannot be casted to the requested type. |
|
inline |
Finds attribute by key and casts it to specified ValueType.
T | type of the value to return |
KeyType | a type comparable to std::string |
key | Key to find attribute value by. |
any_bad_cast | if the stored type cannot be casted to the requested type. |
|
inline |
Returns an tree_map
containing all the attributes available within this servlet context.
tree_map
of attributes
|
inline |
Const version of get_attributes method.
tree_map
of attributes
|
inline |
Return the main path associated with this context.
|
inline |
Returns a reference to a std::string
containing the value of the named context-wide initialization parameter, or an empty reference if the parameter does not exist.
This method can make available configuration information useful to an entire "web application". For example, it can provide a webmaster's email address or the name of a system that holds critical data.
KeyType | a type comparable to std::string |
key | a std::string containing the name of the parameter whose value is requested |
std::string
containing the value of the initialization parameter
|
inline |
Returns the context's initialization parameters map of std::string
objects.
The returned map can be empty if the context has no initialization parameters.
tree_map
of the context's initialization parameters
|
pure virtual |
Returns the MIME type of the specified file, or null
if the MIME type is not known.
The MIME type is determined by the configuration of the servlet container, and may be specified in a web application deployment descriptor. Common MIME types are "text/html"
and "image/gif"
.
file_name | a std::string specifying the name of a file |
std::string
specifying the file's MIME type
|
inline |
Returns filesystem path of current web application.
|
inline |
Removes the attribute with the given name from the servlet context.
After removal, subsequent calls to get_attribute to retrieve the attribute's value will return an empty reference.
KeyType | a type comparable to std::string |
key | a string specifying the name of the attribute to be removed |
|
inline |
Binds an object to a given attribute name in this servlet context.
If the name specified is already used for an attribute, this method will replace the attribute with the new to the new attribute.
ValueType | type of the value to set. |
key | a std::string specifying the name of the attribute |
value | a value of the arbitrary type which will be stored in any object representing the attribute |
true
if value in the map was replaced.
|
inline |
Move version of set_attribute(const std::string&, ValueType&&)
ValueType | type of the value to set. |
key | a map key by which to store element |
value | value of the element to store |
true
if value in the map was replaced.