How to Use Attributes in Request and Context
Attributes are named objects stored in ServletRequest and ServletContext. They are used by containers to provide additional data to the web application, especially for container-specific extensions. You can also set attributes yourself for communication between components, for example if your servlet invokes a JSP, you can use pass arguments as request attributes.
By convention, attribute names should start with your package name. Names starting with java., javax. or com.sun. are reserved. The following example prints all available attributes:
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException {
response.setContentType("text/plain; charset=UTF-8");
response.setCharacterEncoding("UTF-8");
PrintWriter writer = response.getWriter();
writer.println("Request attributes:");
for (String name: Collections.list((Enumeration<String>) request.getAttributeNames()))
writer.printf("%s=%s\n", name, request.getAttribute(name));
writer.println("Context attributes:");
ServletContext ctx = getServletContext();
for (String name: Collections.list((Enumeration<String>) ctx.getAttributeNames()))
writer.printf("%s=%s\n", name, ctx.getAttribute(name));
}
This is a list of all standard attributes that are part of the Servlet API:
| Where? | Name | Type | Description |
|---|---|---|---|
| ServletContext | javax.servlet.context.tempdir | java.io.File | temporary directory (more) |
| ServletRequest | javax.servlet.request.cipher_suite | String | for SSL requests only: name of cipher |
| ServletRequest | javax.servlet.request.key_size | Integer | for SSL requests only: key size in bits |
| ServletRequest | javax.servlet.request.X509Certificate | X509Certificate[] | for SSL requests only: array of client certificates |
| ServletRequest | javax.servlet.include.request_uri | String | included servlets only: included request URI (getRequestURI) |
| ServletRequest | javax.servlet.include.context_path | String | included servlets only: included context path (getContextPath) |
| ServletRequest | javax.servlet.include.servlet_path | String | included servlets only: included context path (getServletPath) |
| ServletRequest | javax.servlet.include.path_info | String | included servlets only: included path info (getPathInfo) |
| ServletRequest | javax.servlet.include.query_string | String | included servlets only: included query string (getQueryString) |
| ServletRequest | javax.servlet.forward.request_uri | String | forwarded servlets only: original request URI (getRequestURI) |
| ServletRequest | javax.servlet.forward.context_path | String | forwarded servlets only: original context path (getContextPath) |
| ServletRequest | javax.servlet.forward.servlet_path | String | forwarded servlets only: original context path (getServletPath) |
| ServletRequest | javax.servlet.forward.path_info | String | forwarded servlets only: original path info (getPathInfo) |
| ServletRequest | javax.servlet.forward.query_string | String | forwarded servlets only: original query string (getQueryString) |
| ServletRequest | javax.servlet.error.status_code | Integer | for error pages only: HTTP status code |
| ServletRequest | javax.servlet.error.exception_type | Class | for error pages only: exception class |
| ServletRequest | javax.servlet.error.message | String | for error pages only: error message |
| ServletRequest | javax.servlet.error.exception | Throwable | for error pages only: exception |
| ServletRequest | javax.servlet.error.request_uri | String | for error pages only: original request URI (getRequestURI) |
| ServletRequest | javax.servlet.error.servlet_name | String | for error pages only: servlet name |

