Top Description Fields Constructors Methods
jakarta.servlet

public abstract Class GenericServlet

extends Object
implements Servlet, ServletConfig, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, jakarta.servlet.ServletConfig, jakarta.servlet.Servlet
Known Direct Subclasses
jakarta.servlet.http.HttpServlet
Imports
java.io.IOException, java.util.Enumeration

Defines a generic, protocol-independent servlet. To write an HTTP servlet for use on the Web, extend jakarta.servlet.http.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.

Field Summary

Modifier and TypeField and Description
private transient ServletConfig
private static final long

Constructor Summary

AccessConstructor and Description
public
GenericServlet()

Does nothing.

Method Summary

Modifier and TypeMethod and Description
public void
destroy()

Implements jakarta.servlet.Servlet.destroy.

Called by the servlet container to indicate to a servlet that the servlet is being taken out of service.

public String

Returns:

String a String containing the value of the initialization parameter
getInitParameter
(String
a String specifying the name of the initialization parameter
name
)

Implements jakarta.servlet.ServletConfig.getInitParameter.

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

public Enumeration<String>

Returns:

Enumeration an enumeration of String objects containing the names of the servlet's initialization parameters
getInitParameterNames
()

Implements jakarta.servlet.ServletConfig.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.

public ServletConfig

Returns:

ServletConfig the ServletConfig object that initialized this servlet
getServletConfig
()

Implements jakarta.servlet.Servlet.getServletConfig.

Returns this servlet's ServletConfig object.

public ServletContext

Returns:

ServletContext the ServletContext object passed to this servlet by the init method
getServletContext
()

Implements jakarta.servlet.ServletConfig.getServletContext.

Returns a reference to the ServletContext in which this servlet is running.

public String

Returns:

String information about this servlet, by default an empty string
getServletInfo
()

Implements jakarta.servlet.Servlet.getServletInfo.

Returns information about the servlet, such as author, version, and copyright.

public String

Returns:

the name of this servlet instance
getServletName
()

Implements jakarta.servlet.ServletConfig.getServletName.

Returns the name of this servlet instance.

public void
init(ServletConfig
the ServletConfig object that contains configuration information for this servlet
config
)

Implements jakarta.servlet.Servlet.init.

Called by the servlet container to indicate to a servlet that the servlet is being placed into service.

public void
init()

A convenience method which can be overridden so that there's no need to call super.init(config).

public void
log(String
a String specifying the message to be written to the log file
message
)

Writes the specified message to a servlet log file, prepended by the servlet's name.

public void
log(String
a String that describes the error or exception
message
,
Throwable
the java.lang.Throwable error or exception
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.

public abstract void
service(ServletRequest
the ServletRequest object that contains the client's request
req
,
ServletResponse
the ServletResponse object that will contain the servlet's response
res
)

Redeclares jakarta.servlet.Servlet.service.

Called by the servlet container to allow the servlet to respond to a request.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

configback to summary
private transient ServletConfig config
serialVersionUIDback to summary
private static final long serialVersionUID

Constructor Detail

GenericServletback to summary
public GenericServlet()

Does nothing. All of the servlet initialization is done by one of the init methods.

Method Detail

destroyback to summary
public void destroy()

Implements jakarta.servlet.Servlet.destroy.

Called by the servlet container to indicate to a servlet that the servlet is being taken out of service. See Servlet#destroy.

Annotations
@Override
getInitParameterback to summary
public String getInitParameter(String name)

Implements jakarta.servlet.ServletConfig.getInitParameter.

Returns a String containing the value of the named initialization parameter, or null if the parameter does not exist. See ServletConfig#getInitParameter.

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

Parameters
name:String

a String specifying the name of the initialization parameter

Returns:String

String a String containing the value of the initialization parameter

Annotations
@Override
getInitParameterNamesback to summary
public Enumeration<String> getInitParameterNames()

Implements jakarta.servlet.ServletConfig.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.

Returns:Enumeration<String>

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

Annotations
@Override
getServletConfigback to summary
public ServletConfig getServletConfig()

Implements jakarta.servlet.Servlet.getServletConfig.

Returns this servlet's ServletConfig object.

Returns:ServletConfig

ServletConfig the ServletConfig object that initialized this servlet

Annotations
@Override
getServletContextback to summary
public ServletContext getServletContext()

Implements jakarta.servlet.ServletConfig.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.

Returns:ServletContext

ServletContext the ServletContext object passed to this servlet by the init method

Annotations
@Override
getServletInfoback to summary
public String getServletInfo()

Implements jakarta.servlet.Servlet.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.

Returns:String

String information about this servlet, by default an empty string

Annotations
@Override
getServletNameback to summary
public String getServletName()

Implements jakarta.servlet.ServletConfig.getServletName.

Returns the name of this servlet instance. See ServletConfig#getServletName.

Returns:String

the name of this servlet instance

Annotations
@Override
initback to summary
public void init(ServletConfig config) throws ServletException

Implements jakarta.servlet.Servlet.init.

Called by the servlet container to indicate to a servlet that the servlet is being placed into service. See Servlet#init.

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

Parameters
config:ServletConfig

the ServletConfig object that contains configuration information for this servlet

Annotations
@Override
Exceptions
ServletException:
if an exception occurs that interrupts the servlet's normal operation
See Also
UnavailableException
initback to summary
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.

Exceptions
ServletException:
if an exception occurs that interrupts the servlet's normal operation
logback to summary
public void log(String message)

Writes the specified message to a servlet log file, prepended by the servlet's name. See ServletContext#log(String).

Parameters
message:String

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

logback to summary
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:String

a String that describes the error or exception

t:Throwable

the java.lang.Throwable error or exception

serviceback to summary
public abstract void service(ServletRequest req, ServletResponse res) throws ServletException, IOException

Redeclares jakarta.servlet.Servlet.service.

Called by the servlet container to allow the servlet to respond to a request. See Servlet#service.

This method is declared abstract so subclasses, such as HttpServlet, must override it.

Parameters
req:ServletRequest

the ServletRequest object that contains the client's request

res:ServletResponse

the ServletResponse object that will contain the servlet's response

Annotations
@Override
Exceptions
ServletException:
if an exception occurs that interferes with the servlet's normal operation occurred
IOException:
if an input or output exception occurs