|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
javax.servlet Class GenericServlet
java.lang.Object javax.servlet.GenericServlet
- All Implemented Interfaces:
- Serializable, Servlet, ServletConfig
- Direct Known Subclasses:
- HttpServlet
public abstract class GenericServlet
- extends Object
- implements Servlet, ServletConfig, Serializable
定义一般的、与协议无关的 servlet。要编写用于 Web 上的 HTTP servlet,请改为扩展
javax.servlet.http.HttpServlet
。
GenericServlet
实现 Servlet
和 ServletConfig
接口。servlet 可以直接扩展 GenericServlet
,尽管扩展特定于协议的子类(比如 HttpServlet
)更为常见。
GenericServlet
使编写 servlet 变得更容易。它提供生命周期方法 init
和 destroy
的简单版本,以及 ServletConfig
接口中的方法的简单版本。GenericServlet
还实现 log
方法,在 ServletContext
接口中对此进行了声明。
要编写一般的 servlet,只需重写抽象 service
方法即可。
Defines a generic, protocol-independent
servlet. To write an HTTP servlet for use on the
Web, extend HttpServlet
instead.
GenericServlet
implements the Servlet
and ServletConfig
interfaces. GenericServlet
may be directly extended by a servlet, although it's more common to extend
a protocol-specific subclass such as HttpServlet
.
GenericServlet
makes writing servlets
easier. It provides simple versions of the lifecycle methods
init
and destroy
and of the methods
in the ServletConfig
interface. GenericServlet
also implements the log
method, declared in the
ServletContext
interface.
To write a generic servlet, you need only
override the abstract service
method.
- Author:
- Various
- See Also:
- Serialized Form
Constructor Summary | |
---|---|
GenericServlet()
Does nothing. |
Method Summary | |
---|---|
void |
destroy()
Called by the servlet container to indicate to a servlet that the servlet is being taken out of service. |
String |
getInitParameter(String name)
Returns a String containing the value of the named
initialization parameter, or null if the parameter does
not exist. |
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. |
ServletConfig |
getServletConfig()
Returns this servlet's ServletConfig object. |
ServletContext |
getServletContext()
Returns a reference to the ServletContext in which this servlet
is running. |
String |
getServletInfo()
Returns information about the servlet, such as author, version, and copyright. |
String |
getServletName()
Returns the name of this servlet instance. |
void |
init()
A convenience method which can be overridden so that there's no need to call super.init(config) . |
void |
init(ServletConfig config)
Called by the servlet container to indicate to a servlet that the servlet is being placed into service. |
void |
log(String msg)
Writes the specified message to a servlet log file, prepended by the servlet's name. |
void |
log(String message,
Throwable t)
Writes an explanatory message and a stack trace for a given Throwable exception
to the servlet log file, prepended by the servlet's name. |
abstract void |
service(ServletRequest req,
ServletResponse res)
Called by the servlet container to allow the servlet to respond to a request. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public
GenericServlet()
不执行任何操作。所有 servlet 初始化都由 init
方法中的一个方法来完成。
英文文档:
GenericServlet
public GenericServlet()
- Does nothing. All of the servlet initialization
is done by one of the
init
methods.
Method Detail |
---|
public void
destroy()
由 servlet 容器调用,指示将从服务中取出的 servlet。请参见 Servlet#destroy
。
英文文档:
destroy
public void destroy()
- Called by the servlet container to indicate to a servlet that the
servlet is being taken out of service. See
Servlet.destroy()
.
public String
getInitParameter(String name)
返回包含指定初始化参数的值的 String
,如果参数不存在,则返回 null
。请参见 ServletConfig#getInitParameter
。
提供此方法是为了方便使用。该方法从 servlet 的 ServletConfig
对象获取指定参数的值。
name |
指定初始化参数名称的 String |
return |
String 包含初始化参数值的 String |
getInitParameter
public String getInitParameter(String name)
- Returns a
String
containing the value of the named initialization parameter, ornull
if the parameter does not exist. SeeServletConfig.getInitParameter(java.lang.String)
.This method is supplied for convenience. It gets the value of the named parameter from the servlet's
ServletConfig
object. - Specified by:
getInitParameter
in interfaceServletConfig
- Parameters:
name
- aString
specifying the name of the initialization parameter- Returns:
- String a
String
containing the value of the initialization parameter
public java.util.Enumeration<E>
getInitParameterNames()
以 String
对象的 Enumeration
的形式返回 servlet 的初始化参数的名称,如果 servlet 没有初始化参数,则返回一个空的 Enumeration
。请参见 ServletConfig#getInitParameterNames
。
提供此方法是为了方便使用。该方法从 servlet 的 ServletConfig
对象获取参数名称。
return |
Enumeration 包含 servlet 初始化参数名称的 String 对象的枚举 |
getInitParameterNames
public Enumeration getInitParameterNames()
- Returns the names of the servlet's initialization parameters
as an
Enumeration
ofString
objects, or an emptyEnumeration
if the servlet has no initialization parameters. SeeServletConfig.getInitParameterNames()
.This method is supplied for convenience. It gets the parameter names from the servlet's
ServletConfig
object. - Specified by:
getInitParameterNames
in interfaceServletConfig
- Returns:
- Enumeration an enumeration of
String
objects containing the names of the servlet's initialization parameters
public ServletConfig
getServletConfig()
返回此 servlet 的 ServletConfig
对象。
return |
ServletConfig 初始化此 servlet 的 ServletConfig 对象 |
getServletConfig
public ServletConfig getServletConfig()
- Returns this servlet's
ServletConfig
object. - Specified by:
getServletConfig
in interfaceServlet
- Returns:
- ServletConfig the
ServletConfig
object that initialized this servlet - See Also:
Servlet.init(javax.servlet.ServletConfig)
public ServletContext
getServletContext()
返回对此 servlet 在其中运行的 ServletContext
的引用。请参见 ServletConfig#getServletContext
。
提供此方法是为了方便使用。该方法从 servlet 的 ServletConfig
对象获取上下文。
return |
ServletContext 通过 init 方法传递给此 servlet 的 ServletContext 对象 |
getServletContext
public ServletContext getServletContext()
- Returns a reference to the
ServletContext
in which this servlet is running. SeeServletConfig.getServletContext()
.This method is supplied for convenience. It gets the context from the servlet's
ServletConfig
object. - Specified by:
getServletContext
in interfaceServletConfig
- Returns:
- ServletContext the
ServletContext
object passed to this servlet by theinit
method - See Also:
ServletContext
public String
getServletInfo()
返回有关 servlet 的信息,比如作者、版本和版权。默认情况下,此方法返回一个空字符串。重写此方法使其返回有意义的值。请参见 Servlet#getServletInfo
。
return | String 有关此 servlet 的信息,默认情况下是一个空字符串 |
getServletInfo
public String getServletInfo()
- Returns information about the servlet, such as
author, version, and copyright.
By default, this method returns an empty string. Override this method
to have it return a meaningful value. See
Servlet.getServletInfo()
. - Specified by:
getServletInfo
in interfaceServlet
- Returns:
- String information about this servlet, by default an empty string
public void
init(ServletConfig config) throws ServletException
由 servlet 容器调用,指示将放入服务中的 servlet。请参见 Servlet#init
。
此实现存储它从 servlet 容器中获得的 ServletConfig
对象,以便将来使用。在重写方法的这种形式时,调用 super.init(config)
。
config |
包含此 servlet 的配置信息的 ServletConfig 对象 |
Throws | ServletException: 如果发生中断 servlet 正常操作的异常 |
See also | javax.servlet.UnavailableException |
init
public void init(ServletConfig config) throws ServletException
- Called by the servlet container to indicate to a servlet that the
servlet is being placed into service. See
Servlet.init(javax.servlet.ServletConfig)
.This implementation stores the
ServletConfig
object it receives from the servlet container for later use. When overriding this form of the method, callsuper.init(config)
. - Parameters:
config
- theServletConfig
object that contains configutation information for this servlet- Throws:
ServletException
- if an exception occurs that interrupts the servlet's normal operation- See Also:
UnavailableException
public void
init() throws ServletException
可重写的便捷方法,这样就不必调用 super.init(config)
。
不用重写 #init(ServletConfig)
,只需重写此方法即可,它由 GenericServlet.init(ServletConfig config)
调用。仍然可以通过 #getServletConfig
获取 ServletConfig
对象。
Throws | ServletException: 如果发生中断 servlet 正常操作的异常 |
init
public void init() throws ServletException
- A convenience method which can be overridden so that there's no need
to call
super.init(config)
.Instead of overriding
init(ServletConfig)
, simply override this method and it will be called byGenericServlet.init(ServletConfig config)
. TheServletConfig
object can still be retrieved viagetServletConfig()
. - Throws:
ServletException
- if an exception occurs that interrupts the servlet's normal operation
public void
log(String msg)
将前置有 servlet 名称的指定消息写入 servlet 日志文件。请参见 log(String)
。
msg |
指定要写入日志文件的消息的 String |
log
public void log(String msg)
- Writes the specified message to a servlet log file, prepended by the
servlet's name. See
ServletContext.log(String)
. - Parameters:
msg
- aString
specifying the message to be written to the log file
public void
log(String message, Throwable t)
将前置有 servlet 名称的给定 Throwable
异常的解释性消息和堆栈跟踪写入 servlet 日志文件。请参见 log(String, Throwable)
。
message |
描述错误或异常的 String |
t |
java.lang.Throwable 错误或异常 |
log
public void log(String message, Throwable t)
- Writes an explanatory message and a stack trace
for a given
Throwable
exception to the servlet log file, prepended by the servlet's name. SeeServletContext.log(String, Throwable)
. - Parameters:
message
- aString
that describes the error or exceptiont
- thejava.lang.Throwable
error or exception
abstract public void
service(ServletRequest req, ServletResponse res) throws ServletException, java.io.IOException
由 servlet 容器调用,以允许 servlet 响应某个请求。请参见 Servlet#service
。
此方法已声明为抽象方法,因此子类(比如 HttpServlet
)必须重写它。
req |
包含客户端请求的 ServletRequest 对象 |
res |
将包含 servlet 的响应的 ServletResponse 对象 |
Throws | ServletException: 如果发生妨碍 servlet 正常操作的异常 |
Throws | java.io.IOException: 如果发生输入或输出异常 |
service
public abstract void service(ServletRequest req, ServletResponse res) throws ServletException, IOException
- Called by the servlet container to allow the servlet to respond to
a request. See
Servlet.service(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
.This method is declared abstract so subclasses, such as
HttpServlet
, must override it. - Parameters:
req
- theServletRequest
object that contains the client's requestres
- theServletResponse
object that will contain the servlet's response- Throws:
ServletException
- if an exception occurs that interferes with the servlet's normal operation occurredIOException
- if an input or output exception occurs
public String
getServletName()
返回此 servlet 实例的名称。请参见 ServletConfig#getServletName
。
return | 此 servlet 实例的名称 |
getServletName
public String getServletName()
- Returns the name of this servlet instance.
See
ServletConfig.getServletName()
. - Specified by:
getServletName
in interfaceServletConfig
- Returns:
- the name of this servlet instance
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
Submit a bug or feature
Copyright 2007 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms.
PS : 未经我党受权你也可自由散发此文档。 如有任何错误请自行修正;若因此而造成任何损失请直接找人民主席,请勿与本人联系。谢谢!