ServletContext Interface | |
public interface ServletContext | |
The ServletContext interface declares methods that a servlet uses to communicate with its host server. The methods declared in this interface allow a servlet to obtain information about the server on which it is running. Example: Using ServletContextA ServletContext object is used to obtain information about the server on which a servlet is running. The ServletContext object is also used to obtain a second ServletContext object that is associated with another resource.
import javax.servlet.*;
import java.io.*;
import java.util.*;
public class ServerInfoServlet extends GenericServlet {
String attributeName;
public void service(ServletRequest request, ServletResponse response)
throws ServletException, IOException {
response.setContentType("text/plain");
PrintWriter pw = response.getWriter();
// A ServletContext object is used to obtain information about
// the server.
ServletContext sc = getServletContext();
pw.println("Server info is " + sc.getServerInfo());
// Two additional attributes are added.
sc.setAttribute("status", "OK");
sc.setAttribute("mood", "happy");
pw.println();
pw.println("Server Attributes");
Enumeration e = sc.getAttributeNames();
while (e.hasMoreElements()) {
attributeName = (String) e.nextElement();
pw.println("name: " + attributeName + " value: "
+ sc.getAttribute(attributeName));
}
// The original ServletContext object can be used to obtain a
// ServletContext of another resource
pw.println();
pw.println("Other resource info");
String path = sc.getRealPath("/tmp.txt");
ServletContext sc2 = sc.getContext(path);
pw.println("MIME type: " + sc2.getMimeType(path));
pw.close();
}
}
Finally, the tmp.txt file contains:
blah, blah, blah
|
getContext() | |
public ServletContext getContext(String URLpath) | Method |
getContext() returns the ServletContext object for the resource at the specified path on the server. The path argument is an absolute URL beginning with "/". |
getMimeType() | |
public String getMimeType(String file) | Method |
getMimeType() returns MIME type of the specified file or null if the MIME type cannot be ascertained. Typical return values will be "text/plain", "text/html", or "image/jpg". |
getRealPath() | |
public String getRealPath(String virtualPath) | Method |
getRealPath() returns a String object containing the real path, in a form appropriate to the platform on which the servlet is running, corresponding to the given virtual path. An example of a virtual path might be "/blah.html". |
getServerInfo() | |
public String getServerInfo() | Method |
getServerInfo() returns a String object containing information on the server on which the servlet is running. At a minimum, the String will contain the servlet container name and version number. |
getInitParameter() | |
public String getInitParameter(String name) | Method |
getInitParameter() returns a String object containing the value of the specfied initialization parameter, or null if the parameter does not exist. |
getInitParameterNames() | |
public Enumeration getInitParameterNames() | Method |
getInitParameterNames() returns a Enumeration containing the initialization parameters associated with the invoking ServletContext object |
log() | |
public void log(String message) public void log(String message, Throwable thr) |
Method |
log() is used to write a message to the servlet's log file. The second version writes an explanatory message and a stack trace for the specified Throwable exception to the log file. |
getNamedDispatcher() | |
public RequestDispatcher getNamedDispatcher(String servletName) | Method |
A RequestDispatcher object sends requests from client machines to the appropriate resource (servlet, HTML file, etc.) on the server. The server creates the RequestDispatcher object which is used as a wrapper around a particular resource. getNamedDispatcher() returns a RequestDispatcher object that will be wrapped around the named servlet. |
getRequestDispatcher() | |
public RequestDispatcher getRequestDispatcher(String path) | Method |
A RequestDispatcher object sends requests from client machines to the appropriate resource (servlet, HTML file, etc.) on the server. The server creates the RequestDispatcher object which is used as a wrapper around a particular resource. getRequestDispatcher() returns an RequestDispatcher object that acts as a wrapper around the resource located at the specified path. The path must begin with "/", and is interpreted relative to the current context root. |
getResource() | |
public URL getResource(String path)throws MalformedURLException | Method |
getResource() returns a URL object that is mapped to the specified path. The path must begin with "/" and is interpreted relative to the current context root. |
getResourceAsStream() | |
public InputStream getResourceAsStream(String path) | Method |
getResourceAsStream() returns the resource at the specified path as an InputStream object. |
getAttribute() | |
public Object getAttribute(String name) | Method |
getAttribute() returns the value of the specified attribute name. The return value is an Object or sub-class if the attribute is available to the invoking ServletContext object, or null if the attribute is not available. |
getAttributeNames() | |
public Enumeration getAttributeNames() | Method |
getAttributeNames() returns an Enumeration containing the attribute names available to the invoking ServletContext object. |
removeAttribute() | |
public void removeAttribute(String name) | Method |
removeAttribute() makes the specified attribute unavailable to the invoking ServletContext object. Subsequent calls to the getAttribute() method for this attribute will return null. |
setAttribute() | |
public void setAttribute(String name, Object value) | Method |
setAttribute() binds a value to a specified attribute name. |
getMajorVersion() | |
public int getMajorVersion() | Method |
getMajorVersion() returns the major version of the Java Servlet API that the server supports. For servers supporting version 2.2 of the Servlet specification, this method will return 2. |
getMinorVersion() | |
public int getMinorVersion() | Method |
getMinorVersion() returns the minor version of the Java Servlet API that the server supports. For servers supporting version 2.2 of the Servlet specification, this method will return 2. |
getServlet() | |
public Servlet getServlet(String name)throws ServletException | Deprecated |
getServlet() returns the specified Servlet. This method was deprecated as of Java Servlet API 2.1 and will be permanently removed in a future version of the Servlet API. |
getServletNames() | |
public Enumeration getServletNames() | Deprecated |
getServletNames() returns an Enumeration containing the servlet names known to the invoking ServletContext object. This method was deprecated as of Java Servlet API 2.1 and will be permanently removed in a future version of the Servlet API. |
getServlets() | |
public Enumeration getServlets() | Deprecated |
getServlets() returns an Enumeration containing the Servlet objects known to the invoking ServletContext object. This method was deprecated as of Java Servlet API 2.1 and will be permanently removed in a future version of the Servlet API. |
log() | |
public void log(Exception exception, String message) | Deprecated |
This version of the log() method was deprecated as of Java Servlet API 2.1. The version that takes a Throwable argument should be used in its place. |