GenericServlet (Java EE 5)

Java EE API


javax.servlet Class GenericServlet

java.lang.Object
  extended by javax.servlet.GenericServlet
All Implemented Interfaces:
Serializable, Servlet, ServletConfig
Direct Known Subclasses:
HttpServlet

public abstract class GenericServlet
extends Object
implements Servlet, ServletConfig, Serializable

Implements: Servlet, ServletConfig, java.io.Serializable
Extended by: HttpServlet

定义一般的、与协议无关的 servlet。要编写用于 Web 上的 HTTP servlet,请改为扩展 javax.servlet.http.HttpServlet

GenericServlet 实现 ServletServletConfig 接口。servlet 可以直接扩展 GenericServlet,尽管扩展特定于协议的子类(比如 HttpServlet)更为常见。

GenericServlet 使编写 servlet 变得更容易。它提供生命周期方法 initdestroy 的简单版本,以及 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
 
Method Summary
 void
 String
 Enumeration
 ServletConfig
 ServletContext
 String
 String
 void
 void
 void
 void
abstract  void
 
Methods inherited from class java.lang.Object
 

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().

Specified by:
destroy in interface Servlet

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, or null if the parameter does not exist. See ServletConfig.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 interface ServletConfig
Parameters:
name - a String 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 of String objects, or an empty Enumeration if the servlet has no initialization parameters. See ServletConfig.getInitParameterNames().

This method is supplied for convenience. It gets the parameter names from the servlet's ServletConfig object.

Specified by:
getInitParameterNames in interface ServletConfig
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 interface Servlet
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. See ServletConfig.getServletContext().

This method is supplied for convenience. It gets the context from the servlet's ServletConfig object.

Specified by:
getServletContext in interface ServletConfig
Returns:
ServletContext the ServletContext object passed to this servlet by the init 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 interface Servlet
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 对象
ThrowsServletException: 如果发生中断 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, call super.init(config).

Specified by:
init in interface Servlet
Parameters:
config - the ServletConfig 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 对象。

ThrowsServletException: 如果发生中断 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 by GenericServlet.init(ServletConfig config). The ServletConfig object can still be retrieved via getServletConfig().

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 - a String 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. See ServletContext.log(String, Throwable).

Parameters:
message - a String that describes the error or exception
t - the java.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 对象
ThrowsServletException: 如果发生妨碍 servlet 正常操作的异常
Throwsjava.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.

Specified by:
service in interface Servlet
Parameters:
req - the ServletRequest object that contains the client's request
res - the ServletResponse object that will contain the servlet's response
Throws:
ServletException - if an exception occurs that interferes with the servlet's normal operation occurred
IOException - 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 interface ServletConfig
Returns:
the name of this servlet instance


Submit a bug or feature

Copyright 2007 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms.

一看就知道只有菜鸟才干这么无知的事啦。

PS : 未经我党受权你也可自由散发此文档。 如有任何错误请自行修正;若因此而造成任何损失请直接找人民主席,请勿与本人联系。谢谢!