Exceptions Thrown java.io.IOException printboolean value Method public void printboolean value throws java.io.IOException This version of the print method prints the passed in boole
Trang 1Classes
Classes for the javax.servlet package are GenericServlet, ServletInputStream, and
ServletOutputStream Their methods are described in the following sections
GenericServlet Class
The GenericServlet class was created to provide a basic foundation of new servlets It provides default life cycle methods and default implementations of the ServletConfig's methods
GenericServlet() Method
public GenericServlet()
The GenericServlet() method is an empty default constructor GenericServlet() has no
parameters, returns no value, and throws no exceptions
destroy() Method
public void destroy()
The destroy() method is executed when the servlet is removed from the running service It performs any cleanup of resources that were allocated in the init() method destroy() has no parameters, returns no value, and throws no exceptions
getInitParameter() Method
public java.lang.String getInitParameter(
java.lang.String name)
The getInitParameter() method returns a String containing the value of the initialization parameter keyed by the passed in name getInitParameter() throws no exceptions
Parameters
java.lang.String
Returns
java.lang.String
getInitParameterNames() Method
public java.util.Enumeration getInitParameterNames()
The getInitParameterNames() method returns an Enumeration containing all of the names for each initialization parameter getInitParameterNames() has no parameters and throws no
exceptions
Returns
java.util.Enumeration
getServletConfig() Method
public ServletConfig getServletConfig()
The getServletConfig() method returns a ServletConfig object containing any startup
configuration information for this servlet getServletConfig() has no parameters and throws no exceptions
Returns
ServletConfig
getServletContext() Method
public ServletContext getServletContext()
The getServletContext() method returns a ServletContext object containing information about the servlet's network service getServletContext() has no parameters and throws no exceptions
Returns
ServletContext
Trang 2getServletInfo() Method
public java.lang.String getServletInfo()
The getServletInfo() method returns a String containing servlet-specific information about the implementing servlet getServletInfo() has no parameters and throws no exceptions
Returns
java.lang.String
init(ServletConfig config) Method
public void init(ServletConfig config)
throws ServletException
The init() method marks the beginning of a servlet's life It is called only when the servlet is first loaded, and it must execute successfully before the servlet can service requests The init() method should contain all initialization code for the servlet It returns no value
Parameters
ServletConfig (encapsulates the servlet's startup configuration and initialization parameters)
Exceptions Thrown
ServletException
init() Method
public void init()
throws ServletException
This parameterless implementation of the init() method is provided only for convenience It prevents a derived servlet from having to store the ServletConfig object init() has no parameters and returns
no value
Exceptions Thrown
ServletException
log(java.lang.String message) Method
public void log(java.lang.String message)
The log() method takes the passed in message and the name of the servlet and writes them to a log file The location of the log is server specific log() returns no value and throws no exceptions
Parameters
java.lang.String
log(java.lang.String message, java.lang.Throwable t) Method
public void log(java.lang.String message,
java.lang.Throwable t)
This log() method takes the passed in message and Throwable object and logs the message with a stack trace from the Throwable object log() returns no value and throws no exceptions
Parameters
java.lang.String
java.lang.Throwable
service() Method
public void service(ServletRequest request,
ServletResponse response)
throws ServletException,
java.io.IOException
Trang 3The service() method defines the servlet's entry point for servicing requests It can be executed only after the servlet's init() method has executed successfully The service() method is the life cycle method executed for every incoming request It returns no value
Parameters
ServletRequest
ServletResponse
Exceptions Thrown
ServletException
java.io.IOException
ServletInputStream Class
The ServletInputStream is an abstract class defined for servlet writers to get data from the client It is meant to be implemented by a network services writer This class has two methods
ServletInputStream() Method
protected ServletInputStream()
The ServletInputStream() method is the empty default constructor It has no parameters, returns no value, and throws no exceptions
readLine() Method
public void readLine(byte[] b,
int off,
int len)
throws java.io.IOException
The readLine() method reads the len of bytes into the passed in byte array b, starting at position off
If the character '\n' is encountered, then no more bytes are read in readLine() returns no value
Parameters
byte[]
int
int
Exceptions Thrown
java.io.IOException
ServletOutputStream Class
The ServletOutputStream class is used to write responses back to the client It is an abstract class that is implemented by the network services implementor To access the ServletOutputStream, you must call the ServletResponse's getOutputStream() method The class has several methods
ServletOutputStream() Method
public ServletOutputStream()
throws java.io.IOException
The ServletOutputStream() method is the empty default constructor It has no parameters and returns no value
Exceptions Thrown
java.io.IOException
print(boolean value) Method
public void print(boolean value)
throws java.io.IOException
This version of the print() method prints the passed in boolean value to the output stream
Trang 4Parameters
boolean
Note The print() method always throws a java.io.IOException exception For all print values, the
print() method returns no value
print(char value) Method
public void print(char value)
throws java.io.IOException
This version of the print() method prints the passed in char value to the output stream
Parameters
char
print(double value) Method
public void print(double value)
throws java.io.IOException
This version of the print() method prints the passed in double value to the output stream
Parameters
double
print(float value) Method
public void print(float value)
throws java.io.IOException
This version of the print() method prints the passed in float value to the output stream
Parameters
float
print(int value) Method
public void print(int value)
throws java.io.IOException
This version of the print() method prints the passed in int value to the output stream
Parameters
int
print(long value) Method
public void print(long value)
throws java.io.IOException
This version of the print() method prints the passed in long value to the output stream
Parameters
long
print(java.lang.String value) Method
public void print(java.lang.String value)
throws java.io.IOException
This version of the print() method prints the passed in String value to the output stream
Parameters
java.lang.String
Trang 5println() Method
public void println()
throws java.io.IOException
This version of the println() method prints CRLF to the output stream and has no parameters
Note The println() method always throws a java.io.IOException exception For all print values, the
println() method returns no value
println(java.lang.String value) Method
public void println(java.lang.String value)
throws java.io.IOException
This version of the println() method prints the passed in String value to the output stream, followed
by a CRLF
Parameters
java.lang.String
println(boolean value) Method
public void println(boolean value)
throws java.io.IOException
This version of the println() method prints the passed in boolean value to the output stream, followed
by a CRLF
Parameters
boolean
println(char value) Method
public void println(char value)
throws java.io.IOException
This version of the println() method prints the passed in char value to the output stream, followed by
a CRLF
Parameters
char
println(int value) Method
public void println(int value)
throws java.io.IOException
This version of the println() method prints the passed in int value to the output stream, followed by a CRLF
Parameters
int
println(long value) Method
public void println(long value)
throws java.io.IOException
This version of the println() method prints the passed in long value to the output stream, followed by
a CRLF
Parameters
long
println(float value) Method
public void println(float value)
Trang 6throws java.io.IOException
This version of the println() method prints the passed in float value to the output stream, followed by
a CRLF
Parameters
float
println(double value) Method
public void println(double value)
throws java.io.IOException
This version of the println() method prints the passed in double value to the output stream, followed
by a CRLF
Parameters
double
Classes
Classes for the javax.servlet package are GenericServlet, ServletInputStream, and
ServletOutputStream Their methods are described in the following sections
GenericServlet Class
The GenericServlet class was created to provide a basic foundation of new servlets It provides default life cycle methods and default implementations of the ServletConfig's methods
GenericServlet() Method
public GenericServlet()
The GenericServlet() method is an empty default constructor GenericServlet() has no
parameters, returns no value, and throws no exceptions
destroy() Method
public void destroy()
The destroy() method is executed when the servlet is removed from the running service It performs any cleanup of resources that were allocated in the init() method destroy() has no parameters, returns no value, and throws no exceptions
getInitParameter() Method
public java.lang.String getInitParameter(
java.lang.String name)
The getInitParameter() method returns a String containing the value of the initialization parameter keyed by the passed in name getInitParameter() throws no exceptions
Parameters
java.lang.String
Returns
java.lang.String
getInitParameterNames() Method
public java.util.Enumeration getInitParameterNames()
The getInitParameterNames() method returns an Enumeration containing all of the names for each initialization parameter getInitParameterNames() has no parameters and throws no
exceptions
Returns
java.util.Enumeration
Trang 7getServletConfig() Method
public ServletConfig getServletConfig()
The getServletConfig() method returns a ServletConfig object containing any startup
configuration information for this servlet getServletConfig() has no parameters and throws no exceptions
Returns
ServletConfig
getServletContext() Method
public ServletContext getServletContext()
The getServletContext() method returns a ServletContext object containing information about the servlet's network service getServletContext() has no parameters and throws no exceptions
Returns
ServletContext
getServletInfo() Method
public java.lang.String getServletInfo()
The getServletInfo() method returns a String containing servlet-specific information about the implementing servlet getServletInfo() has no parameters and throws no exceptions
Returns
java.lang.String
init(ServletConfig config) Method
public void init(ServletConfig config)
throws ServletException
The init() method marks the beginning of a servlet's life It is called only when the servlet is first loaded, and it must execute successfully before the servlet can service requests The init() method should contain all initialization code for the servlet It returns no value
Parameters
ServletConfig (encapsulates the servlet's startup configuration and initialization parameters)
Exceptions Thrown
ServletException
init() Method
public void init()
throws ServletException
This parameterless implementation of the init() method is provided only for convenience It prevents a derived servlet from having to store the ServletConfig object init() has no parameters and returns
no value
Exceptions Thrown
ServletException
log(java.lang.String message) Method
public void log(java.lang.String message)
The log() method takes the passed in message and the name of the servlet and writes them to a log file The location of the log is server specific log() returns no value and throws no exceptions
Parameters
java.lang.String
Trang 8log(java.lang.String message, java.lang.Throwable t) Method
public void log(java.lang.String message,
java.lang.Throwable t)
This log() method takes the passed in message and Throwable object and logs the message with a stack trace from the Throwable object log() returns no value and throws no exceptions
Parameters
java.lang.String
java.lang.Throwable
service() Method
public void service(ServletRequest request,
ServletResponse response)
throws ServletException,
java.io.IOException
The service() method defines the servlet's entry point for servicing requests It can be executed only after the servlet's init() method has executed successfully The service() method is the life cycle method executed for every incoming request It returns no value
Parameters
ServletRequest
ServletResponse
Exceptions Thrown
ServletException
java.io.IOException
ServletInputStream Class
The ServletInputStream is an abstract class defined for servlet writers to get data from the client It is meant to be implemented by a network services writer This class has two methods
ServletInputStream() Method
protected ServletInputStream()
The ServletInputStream() method is the empty default constructor It has no parameters, returns no value, and throws no exceptions
readLine() Method
public void readLine(byte[] b,
int off,
int len)
throws java.io.IOException
The readLine() method reads the len of bytes into the passed in byte array b, starting at position off
If the character '\n' is encountered, then no more bytes are read in readLine() returns no value
Parameters
byte[]
int
int
Exceptions Thrown
java.io.IOException
Trang 9ServletOutputStream Class
The ServletOutputStream class is used to write responses back to the client It is an abstract class that is implemented by the network services implementor To access the ServletOutputStream, you must call the ServletResponse's getOutputStream() method The class has several methods
ServletOutputStream() Method
public ServletOutputStream()
throws java.io.IOException
The ServletOutputStream() method is the empty default constructor It has no parameters and returns no value
Exceptions Thrown
java.io.IOException
print(boolean value) Method
public void print(boolean value)
throws java.io.IOException
This version of the print() method prints the passed in boolean value to the output stream
Parameters
boolean
Note The print() method always throws a java.io.IOException exception For all print values, the
print() method returns no value
print(char value) Method
public void print(char value)
throws java.io.IOException
This version of the print() method prints the passed in char value to the output stream
Parameters
char
print(double value) Method
public void print(double value)
throws java.io.IOException
This version of the print() method prints the passed in double value to the output stream
Parameters
double
print(float value) Method
public void print(float value)
throws java.io.IOException
This version of the print() method prints the passed in float value to the output stream
Parameters
float
print(int value) Method
public void print(int value)
throws java.io.IOException
This version of the print() method prints the passed in int value to the output stream
Parameters
int
Trang 10print(long value) Method
public void print(long value)
throws java.io.IOException
This version of the print() method prints the passed in long value to the output stream
Parameters
long
print(java.lang.String value) Method
public void print(java.lang.String value)
throws java.io.IOException
This version of the print() method prints the passed in String value to the output stream
Parameters
java.lang.String
println() Method
public void println()
throws java.io.IOException
This version of the println() method prints CRLF to the output stream and has no parameters
Note The println() method always throws a java.io.IOException exception For all print values, the
println() method returns no value
println(java.lang.String value) Method
public void println(java.lang.String value)
throws java.io.IOException
This version of the println() method prints the passed in String value to the output stream, followed
by a CRLF
Parameters
java.lang.String
println(boolean value) Method
public void println(boolean value)
throws java.io.IOException
This version of the println() method prints the passed in boolean value to the output stream, followed
by a CRLF
Parameters
boolean
println(char value) Method
public void println(char value)
throws java.io.IOException
This version of the println() method prints the passed in char value to the output stream, followed by
a CRLF
Parameters
char
println(int value) Method
public void println(int value)
throws java.io.IOException