Servlet study notes -ServletConfig, ServletContext

1 Introduction

  In the "Servlet working principle and process" we have learned the principle and process Servelt, as well as analysis methods Servlet interface. The following were learning, several other classes and instances encountered in the course of their work Servlet.

2、ServletConfig

  When the Servlet container initialization Servlet, init method of the Servlet Servlet container will pass an object as a parameter ServletConfig instance. In ServletConfig object encapsulates a Servlet initialization program parameters needed while ServletConfig object also holds a reference to an instance of ServletContext object, this object will ServletContext example shows context Servlet program required for the operation in the vessel.

package javax.servlet;

import java.util.Enumeration;

public interface ServletConfig {

    public String getServletName();

    public ServletContext getServletContext();

    public String getInitParameter(String name);

    public Enumeration<String> getInitParameterNames();
}

  The source code, we can know, the ServletConfig class provides four methods as follows:

  • getServletName (); Servlet used to get the name of the current instance. Servlet instance names defined in the application descriptor released, or if not defined, then Servlet instance name, class name will be used as an example Servlet class name.
  • ServletContext returns a reference example; getServletContext ().
  • getInitParameter (String name); save some initial parameters required in ServletConfig Servlet examples, and storage in the form of key-value. By this method we can obtain the key name of the initialization parameter.
  • the getInitParameterNames (); return ServletConfig example, all the parameters corresponding to the stored key set, return the result of type Enumeration.
3、ServletContext

  When analyzing ServletConfig we know: ServletConfig example, holds a reference to a ServletContext instance of an object. ServletContext instance object context means the program running in the container Servlet needs.
  ServletContext example provides a method of interacting with a range Servlet container, for example, acquiring the MIME type of the file acquired distribution request requesting persistence or logs.
  Each Web application in a JVM are only a ServletContext object. In a Web application, which may include a number of Servlet examples and other content. If you deploy an application into multiple containers in a distributed environment, Web applications will be on each Java virtual machine has a ServletContext object. In the distributed case, each ServletContext object is not really all the information the Web application, so all the information is generally used to store Web applications and external resources, such as using a database instead.

package javax.servlet;

import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Enumeration;
import java.util.EventListener;
import java.util.Map;
import java.util.Set;

import javax.servlet.descriptor.JspConfigDescriptor;

public interface ServletContext {

    public static final String TEMPDIR = "javax.servlet.context.tempdir";
    public static final String ORDERED_LIBS = "javax.servlet.context.orderedLibs";

    public String getContextPath();

    public ServletContext getContext(String uripath);

    public int getMajorVersion();

    public int getMinorVersion();

    public int getEffectiveMajorVersion();

    public int getEffectiveMinorVersion();

    public String getMimeType(String file);

    public Set<String> getResourcePaths(String path);

    public URL getResource(String path) throws MalformedURLException;

    public InputStream getResourceAsStream(String path);

    public RequestDispatcher getRequestDispatcher(String path);

    public RequestDispatcher getNamedDispatcher(String name);
    
    @SuppressWarnings("dep-ann")
    // Spec API does not use @Deprecated
    public Servlet getServlet(String name) throws ServletException;

    @SuppressWarnings("dep-ann")
    // Spec API does not use @Deprecated
    public Enumeration<Servlet> getServlets();

    @SuppressWarnings("dep-ann")
    // Spec API does not use @Deprecated
    public Enumeration<String> getServletNames();

    public void log(String msg);

    @SuppressWarnings("dep-ann")
    // Spec API does not use @Deprecated
    public void log(Exception exception, String msg);

    public void log(String message, Throwable throwable);

    public String getRealPath(String path);

    public String getServerInfo();

    public String getInitParameter(String name);

    public Enumeration<String> getInitParameterNames();

    public boolean setInitParameter(String name, String value);

    public Object getAttribute(String name);

    public Enumeration<String> getAttributeNames();

    public void setAttribute(String name, Object object);

    public void removeAttribute(String name);

    public String getServletContextName();

    public ServletRegistration.Dynamic addServlet(String servletName, String className);

    public ServletRegistration.Dynamic addServlet(String servletName, Servlet servlet);

    public ServletRegistration.Dynamic addServlet(String servletName,
            Class<? extends Servlet> servletClass);

    public <T extends Servlet> T createServlet(Class<T> c)
            throws ServletException;

    public ServletRegistration getServletRegistration(String servletName);

    public Map<String, ? extends ServletRegistration> getServletRegistrations();

    public FilterRegistration.Dynamic addFilter(String filterName, String className);

    public FilterRegistration.Dynamic addFilter(String filterName, Filter filter);

    public FilterRegistration.Dynamic addFilter(String filterName,
            Class<? extends Filter> filterClass);

    public <T extends Filter> T createFilter(Class<T> c) throws ServletException;

    public FilterRegistration getFilterRegistration(String filterName);

    public Map<String, ? extends FilterRegistration> getFilterRegistrations();

    public SessionCookieConfig getSessionCookieConfig();

    public void setSessionTrackingModes(
            Set<SessionTrackingMode> sessionTrackingModes);

    public Set<SessionTrackingMode> getDefaultSessionTrackingModes();

    public Set<SessionTrackingMode> getEffectiveSessionTrackingModes();

    public void addListener(String className);

    public <T extends EventListener> void addListener(T t);

    public void addListener(Class<? extends EventListener> listenerClass);

    public <T extends EventListener> T createListener(Class<T> c)
            throws ServletException;

    public JspConfigDescriptor getJspConfigDescriptor();

    public ClassLoader getClassLoader();

    public void declareRoles(String... roleNames);

    public String getVirtualServerName();
}

  It defines a number of methods ServletContext class, the following main common method of learning about the role:

  • getMimeType (String file); obtaining a corresponding file specified MIME type, MIME type determined by the Servlet container. Common MIME types "text / htm", "image / gif" and the like.
  • getResourcePaths (String path); or directory listing all files in the specified directory. such as:
     * /welcome.html
     * /catalog/index.html
     * /catalog/products.html
     * /catalog/offers/books.html
     * /catalog/offers/music.html
     * /customer/login.jsp
     * /WEB-INF/web.xml
     * /WEB-INF/classes/com.acme.OrderServlet.class,
     * getResourcePaths("/") returns {"/welcome.html", "/catalog/",
     * "/customer/", "/WEB-INF/"}
     * getResourcePaths("/catalog/") returns {"/catalog/index.html",
     * "/catalog/products.html", "/catalog/offers/"}
  • URL getResource (String path) throws MalformedURLException; returns a resource map to the specified URL path
  • getResourceAsStream (String path); Returns the specified directory corresponding to the resource objects InputStream
  • getRequestDispatcher (String path); the resources are packed into a specified directory RequestDispatcher object, and return.
  • getNamedDispatcher (String name); and getRequestDispatcher (String path) method is similar, but the name of the method according to Example Servlet RequestDispatcher encapsulated object.
  • log (String msg); writes the specified information to the Servlet container log file, the log file is configurable by the Servlet container.
  • log (String message, Throwable throwable); and a method of acting on the same, but more information stack.
  • getRealPath (String path); return path resources, real storage path of the server file system.
  • getServerInfo (); Returns the name of the currently running container.
  • getInitParameter (String name); obtaining initialization parameters Web application. For example, the method can obtain the parameter information as follows in:
//tomcat下server.xml配置
<Context path="/testcontext" docBase="/context"  
         privileged="true" antiResourceLocking="false" antiJARLocking="false"  
         debug="0" reloadable="true">  
    <Parameter name="test" value="xxxx" />  
</Context>  
//项目下web.xml配置
<context-param>  
    <param-name>age</param-name>  
    <param-value>24</param-value>  
</context-param>  
  • the getInitParameterNames (); obtaining a set of key initialization parameters.
  • setInitParameter (String name, String value); setting initialization parameters must be set before ServletContext loaded, otherwise there will be an exception.
  • getAttribute (String name); and getInitParameter (String name); a similar manner, except that the process parameters not taken to initialize Servlet examples.
  • getAttributeNames (); getInitParameterNames (); similar.
  • setAttribute (String name, Object object); and setInitParameter (String name, String value); the like, but is not limited ServletContext loaded before completion.
  • removeAttribute (String name); Removes the specified parameters.
  • getServletContextName (); Gets the name corresponding to the current instance ServletContext.
  • addServlet (String servletName, String className); servlet instance to dynamically register a ServletContext object. And return ServletRegistration.Dynamic object.
  • addServlet (String servletName, Servlet servlet); overloaded method of the above method, the same effect.
  • addServlet (String servletName, Class servletClass <extends Servlet?>); overloaded method of the above method, the same effect.
  • createServlet (Class c) throws ServletException; Servlet instance created according to Class.
  • getServletRegistration (String servletName); obtaining the object according to the Servlet corresponding to ServletRegistration instance name, the object included in the object details Servlet.
  • getServletRegistrations (); Get all ServletRegistration object container, return type Map <String, extends ServletRegistration?>.
  • addFilter (String filterName, String className); add filters to ServletContext object.
  • addFilter (String filterName, Filter filter); overloads.
  • addFilter (String filterName, Class <extends Filter?> filterClass); overloaded methods
  • createFilter (Class c) throws ServletException; create a filter, the return type
  • getFilterRegistration (String filterName); ServletRegistration corresponding to the object acquired in accordance with the filter name, the object included in the details of the filter object.
  • getFilterRegistrations (); Get all FilterRegistration object container, return type Map <String, extends FilterRegistration?>
  • getSessionCookieConfig (); return object context SessionCookieConfig
  • setSessionTrackingModes (Set sessionTrackingModes); Set Session tracking mode.
  • getDefaultSessionTrackingModes (); Get the current Session tracking mode the default collection
  • getEffectiveSessionTrackingModes (); Session tracking mode currently active set
  • addListener (String className); added to ServletContext listener object.
  • addListener (T t); overloads
  • addListener (Class listenerClass <extends EventListener?>); overloaded methods
  • createListener (Class c) throws ServletException; create Listeners
  • getJspConfigDescriptor (); Gets JspConfigDescriptor instance object is used to obtain a web application using <jsp-config> configuration parameters.
Published 71 original articles · won praise 3 · Views 5242

Guess you like

Origin blog.csdn.net/hou_ge/article/details/104710309