ServletContext interface
ServletContext Servlet API defines an interface for context object encapsulating
the interface defines a set of methods:
No. | name | Brief introduction | Brief introduction |
---|---|---|---|
01 | void | setAttribute(String key,Object value) | Object value stored in the form of key / value of |
02 | Object | getAttribute(String key) | Gets an object value by key |
03 | String | getInitParameter(String path) | Returns the context parameter value |
04 | String | getRealPath(String path) | The virtual path returns the actual path (see in use ch02) |
How to get ServletContext object
ServletContext is the interface, you can not create objects directly. ServletContext object is a container created, you can use the Servlet API provides access to the object.
Servlet class commonly used method of obtaining the context object
- getServletContext method defined in the interface ServletConfig
- Since the class definition from the Servlet implements ServletConfig indirectly interfaces, it is possible to directly call the method returns getServletContext ServletContext object
JSP file using the method of the context object - Built-in objects JSP file application context object that is, any method can be called interface ServletContext