您的位置:首页 > 编程语言

Servlet编程专题2之获取ServletConfig对象

2017-10-15 23:09 369 查看
1、javax.servlet.ServletConfig接口的简单说明:
- Servlet容器在Servlet对象初始化时期传递信息给Servlet对象的Servlet配置对象。



- 在Servlet接口的init()方法中具有唯一的一个参数ServletConfig。ServletConfig是一个接口,顾名思义,就是Servlet配置,即在web.xml中对当前Servlet类中的配置信息。Servlet规范将Servlet的配置信息全部封装到了ServletConfig接口对象中。
- 在Web容器调用init()方法时,Web容器首先会将web.xml中当前Servlet类的配置信息封装为一个对象。这个对象的类型实现了ServletConfig接口,Web容器会将这个对象传递给init()方法中的ServletConfig参数。(由Web容器自动创建ServletConfig接口实现类的对象)
- ServletConfig接口实例类的对象指的就是:web.xml中的Servlet标签:
<servlet>
<servlet-name>my-servlet</servlet-name>
<servlet-class>com.geeklicreed.servlet.MyServlet</servlet-class>
<init-param>
<param-name>name</param-name>
<param-value>geeklicreed</param-value>
</init-param>
</servlet>


2、如果希望在自定义的Servlet实现类中使用ServletConfig对象,可以在类中定义一个私有ServletConfig成员变量,在init(ServletConfig config)方法中进行接收,在getServletConfig()方法中将这个成员变量进行返回即可。
public class SomeServlet implements Servlet{
private ServletConfig servletConfig;

public void init(ServletConfig config) throws ServletException{
this.servletConfig = config;
}

public ServletConfig getServletConfig(){
return servletConfig;
}
}


3、javax.servlet.ServletConfig接口中的方法介绍:

- getInitParemeter(String name)方法:获取web.xml文件中的<init-param>标签中与传递的字符串参数相同的<param-name>标签内容所对应的<param-value>标签的字符串内容。
- getInitParameterNames()方法:获取servlet初始化参数值,以字符串对象的一个枚举返回;或者返回一个空的枚举对象,如果没有任意一个初始化参数值。
- getServletContext()方法返回ServletContext对象的引用。
- getServletName()方法返回Servlet实例的名字。(web.xml中的<servlet>标签中设置的<servlet-name>中的内容)
-  java.lang.String getInitParameter(java.lang.String name) : Gets the value of the initialization parameter with the given name.
-  java.util.Enumeration<java.lang.String> getInitParameterNames() : Returns the names of the servlet's initialization parameters as an Enumeration of String objects, or an empty Enumeration if the servlet has no initialization parameters.
-  ServletContext getServletContext() : Returns a reference to the ServletContext in which the caller is executing.
-  java.lang.String getServletName() : Returns the name of this servlet instance.


4、java.util.Enumeration接口的简单介绍:
- Enumeration接口实现类对象会生成一系列元素。(每次生成一个)连续调用接口中的nextElement()方法会序列中连续的元素。
An object that implements the Enumeration interface generates a series of
elements, one at a time. Successive calls to the nextElement method
return successive elements of the series.
- java.util.Enumeration接口中的hasMoreElements()方法检测是否枚举对象中包含有更多的元素,如果存在,返回true:
-  boolean hasMoreElements() : Tests if this enumeration contains more elements.
-  E nextElement() : Returns the next element of this enumeration if this enumeration object has at least one more element to provide.


5、web.xml文件中的<context-param>标签,表示定义整个应用都可以共享的初始化上下文参数:
<!-- 定义初始化参数 -->
<context-param>
<param-name></param-name>
<param-value></param-value>
</context-param>

6、javax.servlet.ServletContext接口:表示Servlet执行的上下文环境,即web应用的上下文环境。
- 定义一组方法用于servlet与servlet容器进行通信,例如获取文件的MIME类型,调度请求或者写入日志文件,JVM中每个web应用只有一个上下文环境对象。(由Web容器自动创建ServletContext接口实现类的对象)
Defines a set of methods that a servlet uses to communicate with its servlet
container, for example, to get the MIME type of a file, dispatch requests, or
write to a log file.
There is one context per "web application" per Java Virtual Machine. (A "web
application" is a collection of servlets and content installed under a specific
subset of the server's URL namespace such as /catalog and possibly
installed via a .war file.)
7、javax.servlet.ServletContext接口中的方法介绍:
- 和getInitParemeter(String name)方法和getInitParameterNames()方法类似,不过返回的是初始化上下文参数值:
- java.lang.Object getAttribute(java.lang.String name) : Returns the servlet container attribute with the given name, or null if there is no attribute by that name.
-  java.util.Enumeration<java.lang.String> getAttributeNames() :Returns an Enumeration containing the attribute names available within this ServletContext.


- 在这个ServletContext对象中设置或者重置给定属性名的域属性:(具备应用全局性,即所有的Servlet实例对象中ServletConfig对象都可以通过getInitParemeter(String name)方法获取设置的域属性)
void setAttribute(java.lang.String name, java.lang.Object object) : Binds an object to a given attribute name in this ServletContext.


- 返回给定名字的servlet容器中的域属性值:(可以在不同的Servlet实例中进行访问)
java.lang.Object getAttribute(java.lang.String name) : Returns the servlet container attribute with the given name, or null if there is no attribute by that name.


- 从ServletContext中删除给定名字的域属性:
void removeAttribute(java.lang.String name) : Removes the attribute with the given name from this ServletContext.


- 返回web应用的上下文的路径:(即web应用的名称,/应用名)
java.lang.String getContextPath() :Returns the context path of the web application.


- 给定的虚拟路径(基于web应用)以获取本地盘符下的真实路径
java.lang.String getRealPath(java.lang.String path) : Gets the real path corresponding to the given virtual path.


- 附录:关联javaee源码操作



- 因为tomcat软件是一个实现servlet规范、jsp规范和el规范和webSockect规范的开源项目(都是JavaEE规范中的内容),所以可以关联对应版本中的tomcat源码来关联javaee源码。
- 如我在Eclipse中使用的tomcat服务器是7.0版本的,可以关联如下压缩包以关联源代码。


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息