java-ServletConfig-ServletContext-重定向、请求分派理解汇总

更新时间:2024-04-24 02:49:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

java中ServletContext与ServletConfig详解

ervletContext相当与一个全局空间.服务器只有一个.所有客户端都是共享的这个东西.而getServletContext因为相当 与当前的servlet而言.每个用户都是访问它一个.所以不可能每个用户都new一个ServletContext.则用get得到一个引用就行了.下 面是我自己的测试报告:ServletConfig只能被本servlet访问.其它Servlet不能访问. web.xml:

//----Servlet声明---

This is the description of my J2EE component

This is the display name of my J2EE component TestServlet

servlet.TestServlet

com

/WEB-INF/user.xml

则在Servlet.java里:String s = this.getServletConfig().getInitParameter(\这句就是获取com的值----/WEB-INF/user.xml

而对于ServletContext的用法则是其它Servlet都可以访问: web.xml: ....

context

avalible during application ......

在Servlet.java里代码:

String n = this.getServletContext().getInitParameter(\

这 句就是获取com的值----avalible during application HttpServletRequest,HttpServletResponse:这两个属性的作用范围最小.时间上:只是本身请求和应答完成就失效.当 然转发是把当前的request对象取出来传给另一个资源.其实本身的request对象还是只生存到本次请求结束.response也同样.空间上:只 能发送请求的客户端有效.HttpSession:一次连结到客户端关闭.时间作用范围比上面两个大.空间任用范围相同.ServletConfig:从 一个servlet被实例化后.对任何客户端在任何时候访问有效.但仅对本servlet有效.一个servlet的ServletConfig对象不能 被另一个servlet访问.ServletContext:对任何servlet.任何人在任何时间都有效.这才是真正全局的对象.那 么.ServletConfig参数和ServletContext参数到底应该如何使用.如何取得? 一般来说.对整个应用的配置.为了不使用\硬编码\应该配置为ServletContext参数.比如字符集设定.

................

charset

GB2312 .................

注 意以上格式只是2.0以后的标准格式.旧容器(引擎)采用服务商自己的格式配置.注意它的父元素应该是也就是说它是对一个 应用作用的.而如果只有一个特定的servlet要设定的参数.其它servlet不能共享.应该配置为ServletConfig参数.如一个读取附件 的servlet要用到绝对目录.而别的servlet不会用到:

GetAtt

mail.GetAttServlet

absPath

/usr/mail/ax/axman/Maildir/

不 用说.因为在标签中已经指定了name和class,也就是说只有mail.GetAttServlet这个 servlet中才能取到path,而别的Servlet是不能取到的.那么如何访问这两个对象的参数呢?访问ServletConfig参数:

首 先要取得ServletConfig对象.然后调用它的getInitParameter();方法.要访问ServletConfig对象.jsp中直 接使用config内置对象.但因为你的JSP编译后的servlet一般不会被加到web.xml中的.所以一般不会通过jsp来取对本JSP编译后的 servlet的配置参数.那么在servlet中要得到ServletConfig对象有两种方法: 在inii()方法中取到:通过init的重载方法传递 .....

public class Test extends HttpServlet {

ServletConfig config;

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

.................. }

然后在下面的方法中就可以访问config对象.但要注意.为了确保能从构造方法中到到当前servlet的

config对象.应该调用父类的构造方法: .....

public class Test extends HttpServlet {

ServletConfig config;

public void init(ServletConfig config) throws ServletException {

super.init(config); this.config = config; }

.................. }

通过getServletConfig()方法直接到时.这样做的好处是不必调手工传递属性.想在任何时候都可以得到.

还有第三种方法.要自己实现一些接口.这里作为一般讨论就不介绍了.

要访问ServletContext对象.只要从现有的ServletConfig对象getServletContext()就可以了.然后调用它的getInitParameter()方法就可以获取它的参数. /----下面是ServletContext详解---------/ ServletContext接口简述

ServletContext接口的简述:public interface ServletContext

定 义了一系列方法用于与相应的servlet容器通信.比如:获得文件的MIME类型.分派请求.或者是向日志文件写日志等.每一个web-app只能有一 个ServletContext.web-app可以是一个放置有web application 文件的文件夹.也可以是一个.war的文件.ServletContext对象包含ServletConfig对象之中.ServletConfig对象 在servlet初始化时提供servlet对象. getContext()方法概述:public ServletContext getContext(java.lang.String uripath) 返回一个指定URL地址的ServletContext对象.该方法允许servlets获得对服务器的各部分上下文的访问权.并根据需要从上下文获得 RequestDispatcher对象.这个指定的URL路径必须带有\被解释为服务器文档根目录下的相对路径.并跟其它web-app主机的上 下文根目录匹配. 在一个安全的环境下.servlet容器会返回null.

getMajorVersion()方法概述:public int getMajorVersion()

返回servlet容器支持的Servlet API的版本号.所有实现都必须返回整型数2. getMinorVersion()方法略.

getMimeType()方法概述:public java.lang.String getMimeType(java.lang.String file)

返回指定文件的文件类型.如果文件类型未知.则返回null.文件类型由servlet容器的配置决定并在一个web-app中被指定.一般情况下的文件类型是:\和\ getResourcePaths()方法概述:public java.util.Set getResourcePaths(java.lang.String path) 返回一个存储web-app中所有资源路径的Set(集合).

路径以”/\结尾表示一个子目录.并以\开头表示一个对于web-app的相对路径. 例子:

/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(\将返回Set {\; getResourcePaths(\将返回Set {\\\

如果子目录为空.返回null.

getResource()方法概述:public java.net.URL getResource(java.lang.String path) throws java.net.MalformedURLException

返 回由path指定的资源路径对应的一个URL对象.该path必须以“/\开头并作为当前目录的相对位置.该方法允许servlet容器使serlets 中的一个资源变为可用.该资源允许是一个本地资源或者是一个远程文件系统.这些资源可以在数据库中.或者在一个.war文件中.servlet容器必须实 现URL句柄和URLCOnnection对象.这些对象对于资源访问是必需的. 如果没有匹配的资源.该方法返回null. 某些容器甚至可以向该方法所返回的资源写数据. 这个资源可以直接返回.所以请求一个a.jsp文件将返回JSP源文件.用一个RequestDispatcher对象取而代之.可以包含执行的结果.

这个方法功能有别于java.lang.Class.getResource.一个基于class loader检索资源的方法.但前者不依赖于class loaders. getResourceAsStream() 方法概述:public java.io.InputStream getResourceAsStream(java.lang.String path) 返回一个由String path指定位置资源的InputStream.返回的InputStream可以是任意类型和长度的. getRequestDispatcher()方法概述:public RequestDispatcher getRequestDispatcher(java.lang.String path)

返回一个RequestDispatcher对象.该对象扮演着一个给定资源包装者的角色.一个RequestDispatcher对象可以用于传送一个请求到特定资源或者把特定资源包含到一个响应当中.该特定资源可以是动态的也可以是静态的.

通过getContext()方法可以为外部contexts资源获取RequestDispatcher对象.如果ServletContext不能获取RequestDispatcher对象.返回null.

getNamedDispatcher()方法概述:public RequestDispatcher getNamedDispatcher(java.lang.String name)

为 指定名字的servlet对象返回一个RequestDispatcher对象.Servlet和JSP页面可以通过服务器管理或web application deployment descriptor被命名.一个Servlet实例的名称可以由ServletConfig.getServletName()决定.

getServlet()方法、getServlets()方法、getServletNames()方法.不推荐使用. log(java.lang.String msg)方法概述:public void log(java.lang.String msg)

把指定的信息写进servlet日志文件.通常是事物日志.日志文件的名称和类型与servlet容器有关.

public void log(java.lang.Exception exception,java.lang.String msg) 略

public void log(java.lang.String message,java.lang.Throwable throwable) 略 getRealPath()方法概述:public java.lang.String getRealPath(java.lang.String path)

返回一个指定虚拟路径的真实路径(完整路径)的字符串(得到项目的真实路径例如: C:\\Program Files\\Apache Software Foundation\\Tomcat 5.5\\webapps\\XMLR ).

举例:虚拟路径\将返回服务器文件系统中的绝对路径\http://host/contextPath/index.html\当中的contextPath是ServletContext的上下文路径. 返回的路径将适用于servlet容器所运行的操作系统.假如无法把虚拟路径映射为真实路径.该方法将返回null.(比如当路径指定的内容是源于.war文件) getServerInfo()方法概述:public java.lang.String getServerInfo() 返回servlet容器的名称和版本号.

返回的字符串格式是servername/versionnumber(服务器名/版本号).例如:the JavaServer Web Development Kit 将返回字符串”JavaServer Web Dev Kit/1.0”.

servlet容器还将返回其它可选信息.如:“JavaServer Web Dev Kit/1.0 (JDK 1.1.6; Windows NT

4.0 x86)”

getInitParameter()方法概述:public java.lang.String getInitParameter(java.lang.String name) 返回上下文定义的变量的值.如果变量不存在.返回null. 见ServletConfig.getInitParameter(java.lang.String).

getInitParameterNames()方法概述:public java.util.Enumeration getInitParameterNames() 返回上下文定义的所以变量的枚举函数.如果空则返回空枚举函数.

getAttribute()方法概述:public java.lang.Object getAttribute(java.lang.String name) 返回指定名字的servlet容器变量值.如果无该变量则返回null.

getAttributeNames()方法概述:public java.util.Enumeration getAttributeNames() 返回servlet容器的所有变量的枚举函数.如果空则返回空枚举函数.

setAttribute()方法概述:public void setAttribute(java.lang.String name,java.lang.Object object) 在servlet容器内绑定一个指定对象给一个指定的名字.如果该名字已经绑定到一个对象.则用该对象覆盖之.

If listeners are configured on the ServletContext the container notifies them accordingly. (不会翻译.求助)

如果传递一个null值.则相当于调用removeAttribute().

removeAttribute()方法概述:public void removeAttribute(java.lang.String name) 移除指定名字的servlet容器变量.

getServletContextName()方法概述:public java.lang.String getServletContextName() 返回web application的名字.

最后.见Servlet.getServletConfig(), ServletConfig.getServletContext() (StartupServlet.java) /*

* @class: StartupServlet * @version: 1.0

* @Date: 2004/12/16 15:51:40 */

package com.chinacreator.ac.javabean.servlet; import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.net.*;

import com.chinacreator.tousu.application.*;

import com.chinacreator.tousu.application.ApplicationDocument.*; import com.chinacreator.ac.javabean.log.*; import com.chinacreator.ac.javabean.util.*; import com.chinacreator.ac.javabean.servlet.*; /** *

*

Title: StartupServlet

*

Description: 启动加载的servlet类

*

Copyright: Copyright (c) 2004

*

Company: chinacreator

* @author hui.deng

* @version 1.0 * @see */

public class StartupServlet extends HttpServlet {

private static final Log log = new Log(StartupServlet.class); private ApplicationDocument document = null; //Initialize global variables

public void init() throws ServletException {

ServletContext application = this.getServletContext();

AccessControlFacade facade = new AccessControlFacade();

DictionaryProcessor dictionaryProcessor = facade.getAllDictionariesWrapper(); /**

* 存入字典

* @param dictionaryProcessor 字典处理器 */

application.setAttribute(Constant.DICTIONARY_OBJECT_NAME, dictionaryProcessor); } }

(web.xml)

startupservlet

com.chinacreator.ac.javabean.servlet.StartupServlet 10

startupservlet /startup (get.jsp) /**

* 取出字典

* @return Object 字典处理器 */

public Object getDictionaryProcessor() {

return application.getAttribute(Constant.DICTIONARY_OBJECT_NAME); }

重定向、请求分派、ServletConfig和ServletContext

重定向和请求分派

一. 重定向

1.HttpServletRequest接口提供的sendRedirect()方法用于生成302响应码和Location响应头,从而通知客户端去重新访问Location响应头中指定的URL,其完整的定义语法如下:

public void sendRedirect(String location)throws IOException;

2.其中的location参数指定了重定向的URL,它可以使用绝对URL和相对URL,Servlet容器会自动将相对URL转换成绝对URL后,再生成location头字段。

3.把这个Web应用程序部署到Web容器Tomcat中,启动Tomcat。然后按如图4.13所示在浏览器地址栏中输入:http://localhost:8080/jsp_04_servlet/servlet1?name=test

4.请求之后,服务器返回响应给客户端浏览器。

二. 请求分派

1.Servlet API中定义了一个RequestDispatcher接口,俗称请求分派器。它定义了如下两个方法:

public void forward(ServletRequest request,ServletResponse response)

throws ServletException,IOException;

public void include(ServletRequest request,ServletResponse response)

throws ServletException,IOException;

三. 获取RequestDispatcher实例的方式主要有两种:

1.调用ServletContext接口提供的getRequestDispatcher(String url)方法。

2.调用ServletRequest接口提供的getRequestDispatcher(String url)方法。

3.RequestDispatcher接口的forward()方法用于将请求转发到RequestDispatcher实例封装的资源,由新的资源对客户端作出响应,它的原理可以用图4.16来表示。

4.同样使用“http://localhost:8080/jsp_04_servlet/servlet1?name=test”路径访问这个Servlet1,客户端浏览器将得到如图4.17所示的效果。

四. 重定向和请求分派的比较

HttpServletResponse的sendRedirect()方法实现的重定向和RequestDispatcher的forward()方法实现的请求转发的比较:

1.请求分派只能将请求转发给同一个Web应用中的其他组件;而重定向不仅可以定向到当前应用程序中的其他资源,也可以重定向到其他站点的资源上。

2.重定向的访问过程结束后,浏览器地址栏中显示的URL会发生改变,由初始的URL地址变成重定向的目标URL;而请求转发过程结束后,浏览器地址栏保持初始的URL地址不变。

利用请求域属性传递对象数据

一HttpServletRequest接口中提供了几个方法用来操作请求实例中存储的对象:

1.public void setAttribute(String name,Object obj):将对象存储进HttpServletRequest实例中。

2.public Object getAttribute(String name):检索存储在HttpServletRequest实例中的对象。

3.public Enumeration getAttributeNames():返回包含HttpServletRequest实例中的所有属性名的Enumeration对象。

二publicvoid removeAttribute(String name):从HttpServletRequest实例中删除指定名称的属性。

ServletConfig和ServletContext

一ServletContext的其他用途

1.public void setAttribute(String name,Object obj):根据指定名name把对象obj存放到应用上下文范围中。

2.public Object getAttribute(String name):根据指定名从应用上下文范围中获取到该属性对象。

3.public void removeAttribut(String name):根据指定名从应用上下文范围中移除该属性。

Servlet的线程安全问题

一使用synchronized

使用synchronized关键字同步操作成员变量和共享数据的代码,就可以防止可能出现的线程安全问题。

二尽量少使用成员变量和共享数据

1.ServletContext是可以多线程同时读/写成员变量和共享数据的,线程是不安全的。

2.ServletRequest对象在service方法的范围内是有效的,不要试图在service方法结束后仍然保存请求对象的引用。

3.Servlet本身就是多线程的,在Servlet中再创建线程,将导致执行情况复杂化,出现多线程安全问题。

ServletConfig、ServletContext 的学习

关于Config参数和Context参数的访问

我们先来回顾一下各种内置对象的作用范围

HttpServletRequest,HttpServletResponse:这两个属性的作用范围最小。

时间上:只是本身请求和应答完成就失效,当然转发是把当前的request对象取出来传给另一

个资源,其实本身的request对象还是只生存到本次请求结束,response也同样。 空间上:只能发送请求的客户端有效。

HttpSession:一次连结到客户端关闭,时间作用范围比上面两个大,空间任用范围相同。

ServletConfig:从一个servlet被实例化后,对任何客户端在任何时候访问有效,但仅对本servlet

有效,一个servlet的ServletConfig对象不能被另一个servlet访问。

ServletContext:对任何servlet,任何人在任何时间都有效,这才是真正全局的对象。

那么,ServletConfig参数和ServletContext参数到底应该如何使用,如何取得?

一般来说,对整个应用的配置,为了不使用“硬编码”,应该配置为ServletContext参数,比如字

符集设定。 .................

charset GB2312 .................

注意以上格式只是2。0以后的标准格式,旧容器(引擎)采用服务商自己的格式配置。注意它的

父元素应该是也就是说它是对一个应用作用的。

而如果只有一个特定的servlet要设定的参数,其它servlet不能共享,应该配置为ServletConfig

参数,如一个读取附件的servlet要用到绝对目录,而别的servlet不会用到:

GetAtt

mail.GetAttServlet

absPath

/usr/mail/ax/axman/Maildir/

不用说,因为在标签中已经指定了name和class,也就是说只有mail.GetAttServlet这个\\r

servlet中才能取到path,而别的Servlet是不能取到的。

那么如何访问这两个对象的参数呢? 访问ServletConfig参数:

首先要取得ServletConfig对象,然后调用它的getInitParameter();方法。要访问

ServletConfig对象,jsp中直接使用config内置对象,但因为你的JSP编译后的servlet一般不会被

加到web.xml中的,所以一般不会通过jsp来取对本JSP编译后的servlet的配置参数,那么在servlet

中要得到ServletConfig对象有两种方法:

在inii()方法中取到:通过init的重载方法传递 .....

public class Test extends HttpServlet {

ServletConfig config;

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

.................. }

然后在下面的方法中就可以访问config对象。但要注意,为了确保能从构造方法中到到当前servlet的

config对象,应该调用父类的构造方法: .....

public class Test extends HttpServlet {

ServletConfig config;

public void init(ServletConfig config) throws ServletException {

super.init(config); this.config = config; }

.................. }

通过getServletConfig()方法直接到时,这样做的好处是不必调手工传递属性,想在任何时候都可 以得到。

还有第三种方法,要自己实现一些接口,这里作为一般讨论就不介绍了。

要访问ServletContext对象,只要从现有的ServletConfig对象getServletContext()就可以了,然后\\r

调用它的getInitParameter()方法就可以获取它的参数。

按说:ServletContext对象的作用域比ServletConfig作用域大,为什么要从ServletConfig中到得

ServletContext对象呢?我个人认为:容器保存了很多个ServletContext对象,请求时容器到底取哪一个\\r

给你呢?那就取其中包含ServletConfig信息的那个给你,就是说取ServletConfig对象的父级对象。就好

象HttpSession要从requset中取得一样,就是取那个包含当前requese对象的session对象给你,这只是我

的个人想法,还没有来得及看具体实现。反正就这么用吧。 ============================== javax.servlet

Interface ServletContext Method Detail getContext

public ServletContext getContext(java.lang.String uripath)

Returns a ServletContext object that corresponds to a specified URL on the server.

This method allows servlets to gain access to the context for various parts of the server, and as needed obtain RequestDispatcher objects from the context. The given path must be begin with \is interpreted relative to the server's document root and is matched against the context roots of other web applications hosted on this container.

In a security conscious environment, the servlet container may return null for a given URL.

Parameters:

uripath - a String specifying the context path of another web application in the

container. Returns:

the ServletContext object that corresponds to the named URL, or null if either none exists or the container wishes to restrict this access. See Also:

RequestDispatcher

getMajorVersion

public int getMajorVersion()

Returns the major version of the Java Servlet API that this servlet container supports. All implementations that comply with Version 2.3 must have this method return the integer 2. Returns: 2

getMinorVersion

public int getMinorVersion()

Returns the minor version of the Servlet API that this servlet container supports. All implementations that comply with Version 2.3 must have this method return the integer 3. Returns: 3

getMimeType

public java.lang.String getMimeType(java.lang.String file)

Returns the MIME type of the specified file, or null if the MIME type is not known. The

MIME type is determined by the configuration of the servlet container, and may be specified in a web application deployment descriptor. Common MIME types are

\ and \.

Parameters:

file - a String specifying the name of a file

Returns:

a String specifying the file's MIME type

getResourcePaths

public java.util.Set getResourcePaths(java.lang.String path)

Returns a directory-like listing of all the paths to resources within the web application whose longest sub-path matches the supplied path argument. Paths indicating subdirectory paths end with a '/'. The returned paths are all relative to the root of the web application and have a leading '/'. For example, for a web application containing

/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(\getResourcePaths(\returns {\\\Parameters:

the - partial path used to match the resources, which must start with a /

Returns:

a Set containing the directory listing, or null if there are no resources in the web application whose path begins with the supplied path. Since: Servlet 2.3

getResource

public java.net.URL getResource(java.lang.String path)

throws java.net.MalformedURLException

Returns a URL to the resource that is mapped to a specified path. The path must begin with a \

This method allows the servlet container to make a resource

available to servlets from any source. Resources can be located on a local or remote file system, in a database, or in a .war file. The servlet container must implement the URL handlers and

URLConnection objects that are necessary to access the resource.

This method returns null if no resource is mapped to the pathname. Some containers may allow writing to the URL returned by this method using the methods of the URL class.

The resource content is returned directly, so be aware that requesting a .jsp page returns the JSP source code. Use a

RequestDispatcher instead to include results of an execution. This method has a different purpose than

java.lang.Class.getResource, which looks up resources based on a class loader. This method does not use class loaders.

Parameters:

path - a String specifying the path to the resource

Returns:

the resource located at the named path, or null if there is no resource at that path Throws:

java.net.MalformedURLException - if the pathname is not given in the correct form

getResourceAsStream

public java.io.InputStream getResourceAsStream(java.lang.String path)

Returns the resource located at the named path as an InputStream object.

The data in the InputStream can be of any type or length. The path must be specified according to the rules given in getResource. This method returns null if no resource exists at the specified path. Meta-information such as content length and content type that is available via getResource method is lost when using this method. The servlet container must implement the URL handlers and URLConnection objects necessary to access the resource.

This method is different from java.lang.Class.getResourceAsStream, which uses a class loader. This method allows servlet containers to make a resource available to a servlet from any location, without using a class loader.

Parameters:

name - a String specifying the path to the resource

Returns:

the InputStream returned to the servlet, or null if no resource exists at the specified path

getRequestDispatcher

public RequestDispatcher getRequestDispatcher(java.lang.String path)

Returns a RequestDispatcher object that acts as a wrapper for the resource located at the given path. A RequestDispatcher object can be used to forward a request to the

resource or to include the resource in a response. The resource can be dynamic or static.

The pathname must begin with a \to the current context root. Use getContext to obtain a

RequestDispatcher for resources in foreign contexts. This method returns null if the ServletContext cannot return a RequestDispatcher.

Parameters:

path - a String specifying the pathname to the resource

Returns:

a RequestDispatcher object that acts as a wrapper for the resource at the specified path

See Also:

RequestDispatcher, getContext(java.lang.String)

getNamedDispatcher

public RequestDispatcher getNamedDispatcher(java.lang.String name)

Returns a RequestDispatcher object that acts as a wrapper for the named servlet.

Servlets (and JSP pages also) may be given names via server

administration or via a web application deployment descriptor. A servlet instance can determine its name using ServletConfig.getServletName().

This method returns null if the ServletContext cannot return a RequestDispatcher for any reason.

Parameters:

name - a String specifying the name of a servlet to wrap

Returns:

a RequestDispatcher object that acts as a wrapper for the named servlet See Also:

RequestDispatcher,

ServletConfig.getServletName()

getContext(java.lang.String),

getServlet

public Servlet getServlet(java.lang.String name) throws ServletException

Deprecated. As of Java Servlet API 2.1, with no direct replacement.

This method was originally defined to retrieve a servlet from a ServletContext. In this version, this method always returns null and remains only to preserve binary compatibility. This method will be permanently removed in a future version of the Java Servlet API. In lieu of this method, servlets can share information using the ServletContext class and can perform shared business logic by invoking methods on common non-servlet classes.

getServlets

public java.util.Enumeration getServlets()

Deprecated. As of Java Servlet API 2.0, with no replacement.

This method was originally defined to return an Enumeration of all the servlets known to this servlet context. In this version, this method always returns an empty enumeration and remains only to preserve binary compatibility. This method will be permanently removed in a future version of the Java Servlet API.

getServletNames

public java.util.Enumeration getServletNames()

Deprecated. As of Java Servlet API 2.1, with no replacement.

This method was originally defined to return an Enumeration of all the servlet names known to this context. In this version, this method always returns an empty Enumeration and remains only to preserve binary compatibility. This method will be permanently removed in a future version of the Java Servlet API.

log

public void log(java.lang.String msg)

Writes the specified message to a servlet log file, usually an event log. The name and type of the servlet log file is specific to the servlet container. Parameters:

msg - a String specifying the message to be written to the log file

log

public void log(java.lang.Exception exception, java.lang.String msg)

Deprecated. As of Java Servlet API 2.1, use log(String message, Throwable throwable) instead.

This method was originally defined to write an exception's stack trace and an explanatory error message to the servlet log file.

log

public void log(java.lang.String message,

java.lang.Throwable throwable)

Writes an explanatory message and a stack trace for a given Throwable exception to the servlet log file. The name and type of the servlet log file is specific to the servlet container, usually an event log. Parameters:

message - a String that describes the error or exception throwable - the Throwable error or exception

getRealPath

public java.lang.String getRealPath(java.lang.String path)

Returns a String containing the real path for a given virtual path. For example, the path

\a request for \of this ServletContext..

The real path returned will be in a form appropriate to the computer and operating system on which the servlet container is running, including the proper path separators. This method returns null if the servlet container cannot translate the virtual path to a real path for any reason (such as when the content is being made available from a .war archive).

Parameters:

path - a String specifying a virtual path

Returns:

a String specifying the real path, or null if the translation cannot be performed

getServerInfo

public java.lang.String getServerInfo()

Returns the name and version of the servlet container on which the servlet is running.

The form of the returned string is servername/versionnumber. For example, the JavaServer Web Development Kit may return the string JavaServer Web Dev Kit/1.0.

The servlet container may return other optional information after the primary string in parentheses, for example, JavaServer Web Dev Kit/1.0 (JDK 1.1.6; Windows NT 4.0 x86).

Returns:

a String containing at least the servlet container name and version number

getInitParameter

public java.lang.String getInitParameter(java.lang.String name)

Returns a String containing the value of the named context-wide initialization parameter, or null if the parameter does not exist.

This method can make available configuration information useful to an entire \webmaster's email address or the name of a system that holds critical data.

Parameters:

name - a String containing the name of the parameter whose value is requested

Returns:

a String containing at least the servlet container name and version number See Also:

ServletConfig.getInitParameter(java.lang.String)

getInitParameterNames

public java.util.Enumeration getInitParameterNames()

Returns the names of the context's initialization parameters as an Enumeration of String objects, or an empty Enumeration if the context has no initialization parameters. Returns:

an Enumeration of String objects containing the names of the context's initialization parameters See Also:

ServletConfig.getInitParameter(java.lang.String)

getAttribute

public 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. An attribute allows a servlet container to give the servlet additional information not already provided by this interface. See your server documentation for information about its attributes. A list of supported attributes can be retrieved using

getAttributeNames.

The attribute is returned as a java.lang.Object or some subclass. Attribute names should follow the same convention as package names. The Java Servlet API specification reserves names matching java.*, javax.*, and sun.*.

Parameters:

name - a String specifying the name of the attribute

Returns:

an Object containing the value of the attribute, or null if no attribute exists matching the given name See Also:

getAttributeNames()

getAttributeNames

public java.util.Enumeration getAttributeNames()

Returns an Enumeration containing the attribute names available within this servlet context. Use the getAttribute(java.lang.String) method with an attribute

name to get the value of an attribute. Returns:

an Enumeration of attribute names See Also:

getAttribute(java.lang.String)

setAttribute

public void setAttribute(java.lang.String name, java.lang.Object object)

Binds an object to a given attribute name in this servlet context. If the name specified is already used for an attribute, this method will replace the attribute with the new to the new attribute.

If listeners are configured on the ServletContext the container notifies them accordingly.

If a null value is passed, the effect is the same as calling removeAttribute().

Attribute names should follow the same convention as package names. The Java Servlet API specification reserves names matching java.*, javax.*, and sun.*.

Parameters:

name - a String specifying the name of the attribute object - an Object representing the attribute to be bound

removeAttribute

public void removeAttribute(java.lang.String name)

Removes the attribute with the given name from the servlet context. After removal, subsequent calls to getAttribute(java.lang.String) to retrieve the attribute's value will return null.

If listeners are configured on the ServletContext the container notifies them accordingly.

Parameters:

name - a String specifying the name of the attribute to be removed

getServletContextName

public java.lang.String getServletContextName()

Returns the name of this web application correponding to this ServletContext as specified in the deployment descriptor for this web application by the display-name element. Returns:

The name of the web application or null if no name has been declared in the deployment descriptor. Since: Servlet 2.3

================================= javax.servlet

Interface ServletConfig Method Detail getServletName

public java.lang.String getServletName()

Returns the name of this servlet instance. The name may be provided via server administration, assigned in the web application deployment descriptor, or for an unregistered (and thus unnamed) servlet instance it will be the servlet's class name. Returns:

the name of the servlet instance

getServletContext

public ServletContext getServletContext()

Returns a reference to the ServletContext in which the caller is executing.

Returns:

a ServletContext object, used by the caller to interact with its servlet container See Also:

ServletContext

getInitParameter

public java.lang.String getInitParameter(java.lang.String name)

Returns a String containing the value of the named initialization parameter, or null if

the parameter does not exist. Parameters:

name - a String specifying the name of the initialization parameter

Returns:

a String containing the value of the initialization parameter

getInitParameterNames

public java.util.Enumeration 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. Returns:

an Enumeration of String objects containing the names of the servlet's initialization parameters

本文来源:https://www.bwwdw.com/article/m1rp.html

Top