All Downloads are FREE. Search and download functionalities are using the official Maven repository.

javax.servlet.ServletContext Maven / Gradle / Ivy

There is a newer version: 4.0.66
Show newest version
/*
 * Copyright (c) 1998-2018 Caucho Technology -- all rights reserved
 *
 * This file is part of Resin(R) Open Source
 *
 * Each copy or derived work must preserve the copyright notice and this
 * notice unmodified.
 *
 * Resin Open Source is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Resin Open Source is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
 * of NON-INFRINGEMENT.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Resin Open Source; if not, write to the
 *
 *   Free Software Foundation, Inc.
 *   59 Temple Place, Suite 330
 *   Boston, MA 02111-1307  USA
 *
 * @author Scott Ferguson
 */

package javax.servlet;

import javax.servlet.descriptor.JspConfigDescriptor;
import java.io.InputStream;
import java.util.*;

/**
 * ServletContexts encapsulate applications.  Applications are generalized
 * virtual hosts; a URL prefix defines a distinct application.
 * So /myapp and /yourapp could define different applications. As a
 * degenerate case, each virtual host has its own ServletContext.
 *
 * 

Each application is entirely distinct. Each has its own: *

    *
  • Class loader -- each application gets its own beans and servlets. *
  • ServletContext attributes *
  • Servlets and servlet mappings (e.g. *.jsp could map to different * servlets in different applications.) *
  • File root *
  • Mime mapping *
  • Real-path mapping (aliases) *
* *

URIs are relative to the application root (e.g. /myapp) for * most ServletContext methods. So you can define user workspaces with * identical JSP files and servlets in different applications. * *

Including and forwarding

* *

Forwarding and including files, the Servlet equivalent of SSI are * handled by the RequestDispatcher methods. * *

Global initialization

* *

There is no direct equivalent of a global.jsa. To initialize * and cleanup shared classes on start and stop, use a load-on-startup * servlet. The init() method will be called when the application starts * and the destroy() method will be called when the application finishes. * *


 *   <servlet servlet-name='global'
 *            servlet-class='test.InitServlet'
 *            load-on-startup/>
 * 
* *

Basic configuration

* * In the resin.conf, to define the /myapp application with a document * root in /www/myweb, add the following to the resin.conf. * *

 *   <web-app id='/myapp' app-dir='/www/myweb'/>
 * 
* *

Servlet and Bean locations (class loaders)

* * Each application has its own directories to load application servlets * and beans. By default these are WEB-APP/classes and WEB-APP/lib. * To add a servlet test.MyServlet, create the java file: *
/www/myweb/WEB-APP/classes/test/MyServlet.java
* *

Load balancing

* * When using load balancing with a web server, each JVM will have its own * application object. The attributes are not shared. In contrast, * sessions are always sent to the same JVM. * *

So the application object is best used as a cache rather than * as a way for servlets to communicate. */ public interface ServletContext { public static final String ORDERED_LIBS = "javax.servlet.context.orderedLibs"; public static final String TEMPDIR = "javax.servlet.context.tempdir"; /** * Returns the URL prefix for the ServletContext. */ public String getServletContextName(); /** * Returns a server-specific string identifying the servlet engine. */ public String getServerInfo(); /** * Returns the major version of the servlet API. */ public int getMajorVersion(); /** * * @return major version of the spec the app is based on * @Since Servlet 3.0 */ public int getEffectiveMajorVersion(); /** * Returns the minor version of the servlet API. */ public int getMinorVersion(); /** * @return minor version of the spec the app is based on * @Since Servlet 3.0 */ public int getEffectiveMinorVersion(); /** * Returns the value of an initialization parameter from the configuration * file. * * The Resin configuration looks something like: *


   * <web-app id='/myapp' app-dir='/www/myapp'>
   *   <context-param name1='value1'/>
   *   <context-param name2='value2'/>
   * </web-app>
   * 
* * @param name init parameter name * @return init parameter value */ public String getInitParameter(String name); /** * Returns an enumeration of all init parameter names. */ public Enumeration getInitParameterNames(); /** * Returns the ServletContext for the uri. * Note: the uri is not relative to the application.T * * @param uri path relative to the root * @return the ServletContext responsible for the given uri. */ public ServletContext getContext(String uri); /** * Returns the context-path for the web-application. */ public String getContextPath(); /** * Returns the real file path for the given uri. The file path will * be in native path format (with native path separators.) * *

See ServletRequest to return the real path relative to the * request uri. * * @param uri path relative to the application root to be translated. * @return native file path for the uri. */ public String getRealPath(String uri); /** * Returns a request dispatcher for later inclusion or forwarding. This * is the servlet API equivalent to SSI includes. The uri is relative * to the application root. * *

The following example includes the result of executing inc.jsp * into the output stream. If the context path is /myapp, the equivalent * uri is /myapp/inc.jsp * *

   *   RequestDispatcher disp;
   *   disp = getRequestDispatcher("/inc.jsp?a=b");
   *   disp.include(request, response);
   * 
* *

See ServletRequest to return a request dispatcher relative to the * request uri. * * @param uri path relative to the app root (including query string) * for the included file. * @return RequestDispatcher for later inclusion or forwarding. */ public RequestDispatcher getRequestDispatcher(String uri); /** * Returns a request dispatcher based on a servlet name. * * @param servletName the servlet name to include or forward to. * @return RequestDispatcher for later inclusion or forwarding. */ public RequestDispatcher getNamedDispatcher(String servletName); /** * Returns the mime type for the given uri. * * @param uri path relative to the application root. */ public String getMimeType(String uri); /** * Returns an attribute value. * * @param name of the attribute. * @return stored value */ public Object getAttribute(String name); /** * Returns an enumeration of all the attribute names. */ public Enumeration getAttributeNames(); /** * Sets an attribute value. Because servlets are multithreaded, * setting ServletContext attributes will generally need synchronization. * *

A typical initialization of an application attribute will look like: *

   * ServletContext app = getServletContext();
   * Object value;
   * synchronized (app) {
   *   value = app.getAttribute("cache");
   *   if (value == null) {
   *     value = new Cache();
   *     app.setAttribute("cache", value);
   *   }
   * }
   * 
* * @param name of the attribute. * @param value value to store */ public void setAttribute(String name, Object value); /** * Removes an attribute. Because servlets are multithreaded, * removing ServletContext attributes will generally need synchronization. * * @param name of the attribute. */ public void removeAttribute(String name); /** * Logs a message. */ public void log(String msg); /** * Logs a message and a stack trace. */ public void log(String message, Throwable throwable); /** * Returns the resource for the given uri. In general, the * RequestDispatcher routines are more useful. * * @param uri path relative to the application root. */ public java.net.URL getResource(String uri) throws java.net.MalformedURLException; /** * Returns the set all resources held by the application. */ public Set getResourcePaths(String prefix); /** * Returns the resource as a stream. In general, the * RequestDispatcher routines are more useful. * * @param path path relative to the application root. * @return InputStream to the resource. */ public InputStream getResourceAsStream(String path); /** * @deprecated */ public Servlet getServlet(String name) throws ServletException; /** * @deprecated */ public Enumeration getServlets(); /** * @deprecated */ public Enumeration getServletNames(); /** * @deprecated */ public void log(Exception exception, String msg); /** * Sets the session cookie configuration * * @Since Servlet 3.0 */ public SessionCookieConfig getSessionCookieConfig(); /** * The session tracking mode * * @Since Servlet 3.0 */ public void setSessionTrackingModes(Set modes); /** * The session tracking mode * * @Since Servlet 3.0 */ public Set getDefaultSessionTrackingModes(); /** * The session tracking mode * * @Since Servlet 3.0 */ public Set getEffectiveSessionTrackingModes(); /** * Sets init parameter * @param name * @param value * @return */ public boolean setInitParameter(String name, String value); /** * Adds a servlet with the given className to context */ public ServletRegistration.Dynamic addServlet(String servletName, String className); /** * Adds a servlet to context */ public ServletRegistration.Dynamic addServlet(String servletName, Servlet servlet); /** * Adds a servlet class to the servlet container. */ public ServletRegistration.Dynamic addServlet(String servletName, Class servletClass); /** * Creates a servlet instance using the web-apps injection. */ public T createServlet(Class c) throws ServletException; /** Returs servlet registration using servletName * @param servletName * @return a ServletRegistration object */ public ServletRegistration getServletRegistration(String servletName); /** * Returns servlet registrations * @return */ public Map getServletRegistrations(); /** * Adds a dynamic filter registration using className * @param filterName * @param className * @return */ public FilterRegistration.Dynamic addFilter(String filterName, String className); /** * Adds a dynamic filter registration using filter * * @param filterName * @param filter * @return */ public FilterRegistration.Dynamic addFilter(String filterName, Filter filter); /** * Adds a filter using filterClass * @param filterName * @param filterClass * @return */ public FilterRegistration.Dynamic addFilter(String filterName, Class filterClass); /** * Create a filter using class * @param c * @param * @return * @throws ServletException */ public T createFilter(Class c) throws ServletException; /** * Returns filter registration sing filterName * @param filterName * @return */ public FilterRegistration getFilterRegistration(String filterName); /** * Returns filter registrations * @return */ public Map getFilterRegistrations(); public void addListener(String className); public void addListener(T t); public void addListener(Class listenerClass); public T createListener(Class listenerClass) throws ServletException; public JspConfigDescriptor getJspConfigDescriptor(); public ClassLoader getClassLoader(); public void declareRoles(String... roleNames); }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy