jump to navigation

Servlet Context August 23, 2005

Posted by Coolguy in Servlets/Jsp.
trackback

Nutshell

  • Context parameters are available to entire webapp
  • Any servlet or JSP in the webapp can access context parameters
  • Eg:
    <context-param>
    <param-name>myname
    <param-value>DummyValue
    </context-param>
    getServletContext.getInitParameter(“myname”);
  • To get ServletContext
    getServletConfig.getServletContext.getInitParameter(“myname”); or
    getServletContext.getInitParameter(“myname”);
  • You need to use getServletConfig.getServletContext.getInitParameter(“myname”); if you are in a Servlet class that doesn’t inherit GenericServlet or HttpServlet.

Detail

  • The ServletContext interface defines a servlet’s view of the Web application within which the servlet is running.
  • The Container Provider is responsible for providing an implementation of the ServletContext interface in the servlet container.
  • Using the ServletContext object, a servlet can log events, obtain URL references to resources, and set and store attributes that other servlets in the context can access.
  • ServletContext is rooted at a known path within a Web server. For example, a servlet context could be located at http://www.mycorp.com/catalog. All requests that begin with the /catalog request path, known as the context path, are routed to the Web application associated with the ServletContext.
  • There is one instance object of the ServletContext interface associated with each Web application deployed into a container.
  • In cases where the container is distributed over many virtual machines, a Web application will have an instance of the ServletContext for each JVM.
  • The following methods of the ServletContext interface allow the servlet access tocontext initialization parameters associated with a Web application as specified bythe Application Developer in the deployment descriptor:• getInitParameter• getInitParameterNames
  • Initialization parameters are used by an Application Developer to convey setup information. Typical examples are a Webmaster’s e-mail address, or the name of a system that holds critical data.
  • A servlet can bind an object attribute into the context by name.
  • Any attribute bound into a context is available to any other servlet that is part of the same Webapplication.
  • The following methods of ServletContext interface allow access to this functionality:• setAttribute• getAttribute• getAttributeNames• removeAttribute
  • Context attributes are local to the JVM in which they were created.
  • When information needs to be shared between servlets running in a distributed environment, the information should be placed into a session
  • The ServletContext interface provides direct access only to the hierarchy of static content documents that are part of the Web application, including HTML, GIF, and JPEG files, via the following methods of the ServletContext interface:• getResource• getResourceAsStream
  • The getResource and getResourceAsStream methods take a String with a leading “/” as an argument that gives the path of the resource relative to the root of the context.
  • This hierarchy of documents may exist in the server’s file system, in a Web application archive file, on a remote server, or at some other location.
  • These methods are not used to obtain dynamic content. For example, in a container supporting the JSP a method call of the form getResource(“/index.jsp”) would return the JSP source code and not theprocessed output.
  • The full listing of the resources in the Web application can be accessed using the getResourcePaths(String path) method.
  • Web servers may support multiple logical hosts sharing one IP address on a server. This capability is sometimes referred to as “virtual hosting”. In this case, each logical host must have its own servlet context or set of servlet contexts. Servlet contexts can not be shared across virtual hosts.
  • A temporary storage directory is required for each servlet context. Servletcontainers must provide a private temporary directory for each servlet context, andmake it available via the javax.servlet.context.tempdir context attribute.
  • This common convenience provided in many servlet engine implementations.
  • The container is not required to maintain the contents of the temporary directory when the servlet container restarts, but is required to ensure that the contents of the temporary directory of one servlet context is not visible to the servlet contexts of other Web applications running on the servletcontainer.
Advertisements

Comments»

No comments yet — be the first.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: