servletactioncontext.getservletactioncontextcontext是什么对象

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
您的访问请求被拒绝 403 Forbidden - ITeye技术社区
您的访问请求被拒绝
亲爱的会员,您的IP地址所在网段被ITeye拒绝服务,这可能是以下两种情况导致:
一、您所在的网段内有网络爬虫大量抓取ITeye网页,为保证其他人流畅的访问ITeye,该网段被ITeye拒绝
二、您通过某个代理服务器访问ITeye网站,该代理服务器被网络爬虫利用,大量抓取ITeye网页
请您点击按钮解除封锁&&&&您需要以后才能回答,未注册用户请先。随笔 - 275
评论 - 1756ServletContext (Java EE 5 SDK)
Java EE 5 SDK
javax.servlet
Interface ServletContext
public interface 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.
"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.)
In the case of a web
application marked "distributed" in its deployment descriptor, there will
be one context instance for each virtual machine.
In this situation, the
context cannot be used as a location to share global information (because
the information won't be truly global).
Use an external resource like
a database instead.
The ServletContext object is contained within
object, which the Web server provides the
servlet when the servlet is initialized.
See Also:,
&&&&&&&&&&Returns the servlet container attribute with the given name,
or null if there is no attribute by that name.
&&&&&&&&&&Returns an Enumeration containing the
attribute names available
within this servlet context.
(&uripath)
&&&&&&&&&&Returns a ServletContext object that
corresponds to a specified URL on the server.
&&&&&&&&&&Returns the context path of the web application.
&&&&&&&&&&Returns a String containing the value of the named
context-wide initialization parameter, or null if the
parameter does not exist.
&&&&&&&&&&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 the major version of the Java Servlet API that this
servlet container supports.
&&&&&&&&&&Returns the MIME type of the specified file, or null if
the MIME type is not known.
&&&&&&&&&&Returns the minor version of the Servlet API that this
servlet container supports.
&&&&&&&&&&Returns a
object that acts
as a wrapper for the named servlet.
&&&&&&&&&&Returns a String containing the real path
for a given virtual path.
&&&&&&&&&&Returns a
object that acts
as a wrapper for the resource located at the given path.
&&&&&&&&&&Returns a URL to the resource that is mapped to a specified
&&&&&&&&&&Returns the resource located at the named path as
an InputStream object.
&&&&&&&&&&Returns a directory-like listing of all the paths to resources within the web application whose longest sub-path
matches the supplied path argument.
&&&&&&&&&&Returns the name and version of the servlet container on which
the servlet is running.
&&&&&&&&&&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.
&&&&&&&&&&Returns the name of this web application corresponding to this ServletContext as specified in the deployment
descriptor for this web application by the display-name element.
&&&&&&&&&&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.
&&&&&&&&&&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.
(&exception,
&&&&&&&&&&Deprecated.&As of Java Servlet API 2.1, use
This method was originally defined to write an
exception's stack trace and an explanatory error message
to the servlet log file.
&&&&&&&&&&Writes the specified message to a servlet log file, usually
an event log.
(&message,
&throwable)
&&&&&&&&&&Writes an explanatory message and a stack trace
for a given Throwable exception
to the servlet log file.
&&&&&&&&&&Removes the attribute with the given name from
the servlet context.
&&&&&&&&&&Binds an object to a given attribute name in this servlet context.
getContextPath
getContextPath()
Returns the context path of the web application.
The context path is the portion of the request URI that is used
to select the context of the request. The context path always comes
first in a request URI. The path starts with a "/" character but does
not end with a "/" character. For servlets in the default (root)
context, this method returns "".
It is possible that a servlet container may match a context by
more than one context path. In such cases the
will return the actual context path used by the request and it may
differ from the path returned by this method.
The context path returned by this method should be considered as the
prime or preferred context path of the application.
Returns:The context path of the web application, or "" for the
default (root) contextSince:
Servlet 2.5
getContext
getContext(&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
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:
getMajorVersion
int getMajorVersion()
Returns the major version of the Java Servlet API that this
servlet container supports. All implementations that comply
with Version 2.5 must have this method
return the integer 2.
getMinorVersion
int getMinorVersion()
Returns the minor version of the Servlet API that this
servlet container supports. All implementations that comply
with Version 2.5 must have this method
return the integer 5.
getMimeType
getMimeType(&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 "text/html" and "image/gif".
Parameters:file - a String specifying the name
Returns:a String specifying the file's MIME type
getResourcePaths
getResourcePaths(&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("/") returns {"/welcome.html", "/catalog/", "/customer/", "/WEB-INF/"}
getResourcePaths("/catalog/") returns {"/catalog/index.html", "/catalog/products.html", "/catalog/offers/"}.
Parameters:path - 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
getResource(&path)
Returns a URL to the resource that is mapped to a specified
path. The path must begin with a "/" and is interpreted
as relative to the current context root.
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
- if the pathname is not given in
the correct form
getResourceAsStream
getResourceAsStream(&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:path - 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
getRequestDispatcher(&path)
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 "/" and is interpreted as relative
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, or null if
the ServletContext cannot return
a RequestDispatcherSee Also:,
getNamedDispatcher
getNamedDispatcher(&name)
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
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,
or null if the ServletContext
cannot return a RequestDispatcherSee Also:,
getServlet
getServlet(&name)
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
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
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.
void log(&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
void log(&exception,
Deprecated.&As of Java Servlet API 2.1, use
This method was originally defined to write an
exception's stack trace and an explanatory error message
to the servlet log file.
void log(&message,
&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 exceptionthrowable - the Throwable error
or exception
getRealPath
getRealPath(&path)
Returns a String containing the real path
for a given virtual path. For example, the path "/index.html"
returns the absolute file path on the server's filesystem would be
served by a request for "http://host/contextPath/index.html",
where contextPath is the context path 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
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
getInitParameter(&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 "web application".
For example, it can provide a
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 numberSee Also:
getInitParameterNames
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 parametersSee Also:
getAttribute
getAttribute(&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
nameSee Also:
getAttributeNames
getAttributeNames()
Returns an Enumeration containing the
attribute names available
within this servlet context. Use the
method with an attribute name
to get the value of an attribute.
Returns:an Enumeration of attribute
namesSee Also:
setAttribute
void setAttribute(&name,
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
Parameters:name - a String specifying the name
of the attributeobject - an Object representing the
attribute to be bound
removeAttribute
void removeAttribute(&name)
Removes the attribute with the given name from
the servlet context. After removal, subsequent calls to
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
getServletContextName()
Returns the name of this web application corresponding 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
Java EE 5 SDK
Copyright 2007 Sun Microsystems, Inc. All rights reserved. Use is subject to
Scripting on this page tracks web page traffic,
but does not change the content in any way.下次自动登录
现在的位置:
& 综合 & 正文
servletContext对象
ServletContext对象
1.Web容器在启动时,他会为每个应用都创建一个对应的对象,所以就代表当前的应用。用于数据共享
2,获取对象
方式一对象中维护类对象的引用,所以可以通过方法获取对象;
Servletcontext contest=this.GetServletConfig().getServletContext();
方式二:调用的方法得到其对象
ServletContext context
=this.getServletContext();
3.ServletContext的生命周期:在服务器启动时,会分别为每一个应用创建对象,对象会在的对应应用被删除时和服务器停止时销毁。
4.ServletContext对象的常用方法:详见文档:
(java.lang.String name)
(java.lang.String name)(java.lang.String name)(java.lang.String name, java.lang.Object object)(java.lang.String name)(java.lang.String path)
5,的功能:
①:由于一个应用中所有的共享同一个对象,所以对个通过对象实现数据共享;对象通常也被称之为context域对象
例:在一个中的数据
String data ="abde";
SevletContext context = this.getServletContext();
context.setAttribute("data",data);
可以在中得到
SevletContext context = this.getServletContext();
String value = context.getAttribute("data");
注意:域 代表什么呢:这是一个容器,
2.说明了这个容器的作用范围,也就是应用程序范围
②:获取应用的初始化参数。
在整个的配置文件中,可以使用一个或多个标签为整个
配置一些初始化信息;
&context-param&
&param-name&data&/param-name&
&param-value&xxxxx&/param-value&
&/context-param&
&/web-app&
在中获取初始化信息
//得到指定的初始化参数。
String value this.getServletContext().get("data");
//获取所有的初始化信息
Enumeration e =this.getServletContext().getInitParameterNames();
while(e.hasMoreElements()){
String name = (String) e.nextElement();
//获取初始化信息
String v = this.getServletConfig().getInitParameter(name);
System.out.println(v);
③:实现的转发
因为不适合数据输出,需要把数据转发给输出
例:中的数据转发的中输出
data="abcde";
//把数据带给,要通过域
//得到转发对象
RequestDispatcher rd =
this.getServletContext().("1.jsp");
//用方法转发
rd.forWard(request,response);
//在中获取数据,并输出;
④:利用对象读取资源文件
注意:开发中的配置文件有两种类型,.xml和.properties方法;当配置 信息之间有关系时,就用类型,没有关系时就用类型。
例:在中读取资源文件
//db.properties文件内容
Url=jdbc:mysql://localhost:3306/test
方式一:的getResourceAsStream方法读取
//在中读取
ServletContext context = this.getServletContext();
//ServletContext 的getResourceAsStream("文件的相对路径方法是
将配置文件作为输入
InputStream in=
context.getResourceAsStream("/WEB-INF/CLASSES/db.properties");
//用Properties 对象的方法获取输入流中的键值对。
Properties prop=new Properties();
Prop.load(in);
//读取键对应的值。
String value1 = prop.getProperties("url");
String value2 = prop.getProperties("user");
注意:在读取资源文件时不可以用传统的流读取。最好用
对象读取。
方式二:的方法读取
通过的方法是得到资源的绝对路径,再用传统的流读取资源文件。并且还可以得到当前的资源名称
//在中读取
ServletContext context = this.getServletContext();
//得到绝对路径
String path =
context.getRealPath("/WEB-INF/CLASSES/db.properties");
//用得到当前文件名
String fileName = String.substring(path.lastIndexOf("\\")+1);
//用普通流读取
FileInputStream in = new FileInputStream(path);
//用Properties 对象的方法获取输入流中的键值对。
Properties prop=new Properties();
Prop.load(in);
//读取键对应的值。
String value1 = prop.getProperties("url");
String value2 = prop.getProperties("user");
⑤:在应用中的普通文件怎样读取资源文件
在实际开发中,读取配置文件的操作不会在中进行(如连接数据库);以为要分层开发,所以会创建一个专门连接数据库的类。 在类中获取配置文件就不可以使用对象获取了。就只能通过类加载器去读
例:实际开发中的
public class UserDao {
//创建公用的对象
private static Properties prop = new Properties();
//得到类加载器
//注意:同一个应用下类和文件使用是同一个类加载器
ClassLoader loader = UserDao.class.getClassLoader();
//getResourceAsStream方法,读取资源文件并用流返回
InputStream in =loader.getResourceAsStream("db.properties");
//将流读入对象中
prop.load(in);
} catch (Exception e) {
throw new ExceptionInInitializerError(e);
public void update(){
System.out.println(prop.getProperty("url"));
public void add(){
System.out.println(prop.getProperty("url"));
public void select(){
System.out.println(prop.getProperty("url"));
然后在Servlet中创建UserDao对象调用其增删改查的方法;
public class ServletDemo3 extends HttpServlet {
public void doGet
(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
UserDao dao=new UserDao();
dao.update();
public void doPost
(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
注意:1.使用类加载器,加载配置文件时,配置文件不能太大,因为是加载到内存中过大会导致内存溢出
<span style="color:#.当在配置文件被更改后,上述数的例的造作就读不到,更新后的配置文件。
就需要通过类加载的方式先得到资源文件的位置,再通过传统的方式读取资源文件,这样就可以读到更新后的数据
//得到类加载器
ClassLoader loader = UserDao.class.getClassLoader();
//通过getResource(资源名称)方法的的URL
Java.net.URL url=loader.getResource("db.properties");
//得到文件路径
String path = url.getPath();
FileInputStream in =new FileInputStream(path);
Properties prop = =new Properties();
prop.load(in);
System.out.println(prop.getProperty("url"));
&&&&推荐文章:
【上篇】【下篇】}

我要回帖

更多关于 servletcontext 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信