Java Servlet API Description Document (2.1A) (2)
Summary
Continued Java Servlet API Description Document (2.1A) (1) (2002-08-29 13:27:34)
By Wing, Source:
LinuxAid
This section contains a detailed description of all classes and interfaces for the Java Servlet API. This note is similar to the Javadoc API, but this document provides more information. The API contains two packages, twelve interfaces and nine classes. Package: Javax.Servlet includes interfaces: RequestDispatcher; servlet; servletconfig; servletContext; servletRequest; servletReadModel. The class: genericServlet; servletinputstream; servletoutputstream; servletException; unavailableException. 1. RequestDispatcher interface: Definition:
Public interface requestDispatcher; defines an object, receives the request from the client, and then send it to the server's available resources (such as servlet, CGI, HTML files, JSP files). The Servlet engine creates a Request Dispatcher object that is used to encapsulate server resources defined by a particular URL. This interface is dedicated to encapsulated servlet, but a servlet engine can create a Request Dispatcher object to encapsulate any type of resource. The Request Dispatcher object is built by the servlet engine, not built by servlet developers. Method 1, Forward
Public Void Forward (ServletRequest Request, ServletReponse Response)
Throws servletexception, ioException;
It is used to transfer requests from this servlet to other server resources. This method can be used when a servlet is initially processed in response and requires other objects to respond to this. When the Request object is passed to the target object, the requested URL path and other path parameters are adjusted to reflect the target URL path to the target object. If a servletoutputstream object or PrintWriter object has been returned by the response, this method will not be able to use, otherwise this method will throw an IllegalStateException. 2, Include
Public Void Include (ServletRequest Request, ServletResponse Response)
Throws ServleTexception, IOException
Used to include the content of the response sent to other server resources. In essence, this method reflects the content of the server. Request the object to pass to the target object, reflect the request URL path and path information of the call request. This response object can only call this ServletOutputStream object and PrintWriter object. A servlet calling include the header field if this servlet calls the method (such as cookie) that must be set, this method will not guarantee normal use. As a servlet developer, you must properly solve those methods that may directly store the header field. For example, even if you use a session tracking, in order to ensure the normal work of the session, you must start your session II and Servlet interface outside a call of the include INCLUDE. Defining the interface of the public interface servlet defines a servlet: a Java class that inherits this feature on the web server. Method 1, init
Public void init (servletConfig config) THROWS servleTexception; Servlet Engine After the servlet is instantiated, the Init method is accurately called before the service is placed. Before calling the service method, the init method must be successfully exited. If the init method throws a servletException, you can't place this servlet, if the init method is not completed within the timeout range, we can assume that this servlet does not have functions, and cannot be placed in the service. 2, Service
Public void service (servletRequest Request, ServletResponse Response)
Throws servletexception, ioException;
The Servlet engine calls this method to allow the servlet response request. This method cannot be called until the servlet is not successfully initialized. Before servlet is initialized, the servlet engine can block unresolved requests. After a servlet object is uninstalled until a new serverT is initialized, the servlet engine cannot call this method 3, Destroy
Public void destroy (); When a servlet is removed from the service, the servlet engine calls this method. The DESTROY method cannot be called when all threads are not exited or not all threads that are not allowed by the engine. 4, GetServletConfig
Public servletconfig getServletConfig (); Returns a servletconfig object, as a servlet developer, you should store the ServletConfig object via the init method so that this method can return this object. For your convenience, GenericServlet has done this when performing this interface. 5, GetServletInfo
Public String getServletInfo (); Allow servlets to provide information about its own servlet runtime. The returned string should be a plain text format without any flag (such as HTML, XML, etc.). Third, the servletconfig interface definition
Public interface servletconfig This interface defines an object. With this object, the servlet engine configures a servlet and allows the servlet to get a description of the ServletContext interface about it. Each servletconfig object corresponds to a unique servlet. Method 1, GetInitParameter
Public String GetInitParameter (String Name); This method returns a String that contains the initialization parameters specified by the servlet. If this parameter does not exist, return null values. 2, GetinitParameterNameSpublic EnuMeration GetItParameterNames (); This method returns a list String object that includes all initialization parameter names of the servlet. If the servlet does not initialize the parameters, getInitParameterNames returns an empty list. 3, GetServletContext
Public servletContext (); returns the servletContext object of this servlet. Fourth, the servletContext interface definition
Public interface servletContext defines a servlet environment object, through which the servlet engine provides environmental information to servlet. An environment object of a servlet must be at least one or one with the host it resides. In a servlet engine that handles multiple virtual hosts (for example, host header fields of HTTP1.1), each virtual host must be considered a separate environment. In addition, the servlet engine can also create an environment object corresponding to a set of servlets. Method 1, GetAttribute
Public Object GetAttribute (String Name); Returns the attribute object specified in the Servlet environment object. Returns null values if the property object does not exist. This method allows access to additional information that has not been provided in other methods of this servlet engine. 2, GetaTtributeNames
Public Enumeration GetAttributeNames (); Returns a list of attribute names available in a servlet environment object. 3, getContext
Public servletContext getContext (String Uripath); Returns a servlet environment object, including servlets and resources of a specific URI path, returns an null value if the path does not exist. The URI path format is /DIR/DIR/FileName.ext. For security, if you access a restricted servlet's environment object through this method, an null value is returned. 4, getmajorversion
Public int getmajorversion (); Returns the main version of the servlet API supported by the Servlet engine. For example, for version 2.1, this method returns an integer 2. 5, getminorversion
Public int getminorversion (); returns the secondary version of the servlet API supported by the servlet engine. For example, for version 2.1, this method returns an integer 2. 6, getMimeType
Public String getMimetype (String file); Returns the MIME type of the specified file, if this MIME type is unknown, return an null value. The MIME type is determined by the configuration of the servlet engine. 7, GetRealPath
Public String GetRealPath (String Path); a format of a specified virtual path in accordance with the URL path format is: /DIR/DIR/FileName.ext. With this method, you can return String with a true path corresponding to the virtual path in accordance with the format. The format of this real path should be suitable for running this Servlet engine (including its corresponding path parser). No matter what reason, if this process cannot be executed from the virtual path to the actual path, the method will return an null value. 8, getResourceCepublic URL GetResource (String Uripath); Returns a URL object that reflects the resources known to the servlet environment objects located at a given URL address (format: /DIR/DIR/FileName.ext). Whether UrlstreamHandlers is not necessary for access to a given environment, the servlet engine must be executed. If a given path is not known, the method returns an null value. This method is not exactly the same as the GetResource method of java.lang.class. The GetResource method of java.lang.class is looking for resources by loading class. This method allows the server to generate environment variables to any servlet for any resource, without having to rely on loading classes, specific areas, and more. 9, GetResourceAsStream
Public InputStream GetResourceSstream (String Uripath); Returns an InputStream object that references the contents of the servlet environment object of the specified URL. If the servlet environment variable is not found, the null value will be returned, and the URL path should have this format: /DIR/DIR/FileName.ext. This method is a convenient way to get URL objects through the getResource method. Note that when you use this method, Meta-Information (eg, content length, content type) will be lost. 10, GetRequestDispatcher
Public RequestDispatcher getRequestDispatcher (String Uripath); if this specified path can find an active resource (such as a servlet, JSP page, CGI, etc.) returns a specific URL's RequestDispatcher object, otherwise, return an null value, servlet engine Responsible for encapsulating the target path with an REQUEST Dispatcher object. This Request Dispatcher object can be used to fully request. 11, GetServerInfo
Public string getServerInfo (); returns a String object, which includes at least the name and version number of the Servlet engine. 12, log
Public void log (String MSG);
Public void log (String Msg, Throwable T);
Public void log (Exception Exception, String MSG); // This usage will be canceled
Write the specified information to a log file of a servlet environment object. The log file written is specified by the servlet engine, but it is usually an event log. When this method is called by an exception, a stack tracking is included. 13, SetAttribute
Public Void SetAttribute (String Name, Object O); gives a name that you specify in the SERVLET environment object. 14, RemoveAttribute
Public Void Removettribute (String Name); Delete an attribute from the specified servlet environment object. Note: The following methods will be canceled 15, GetServletPublic servlet getServlet (String name) throws servletexception; for a servlet used to return a specified name, if you have not found, you return an null value. If this servlet can return, this means it has been initialized and can have been accepted. This is a dangerous approach. When calling this method, you may not know the status of the servlet, which may result in a problem with the server status. This method that allows a servlet to access other servlets is also the same. Now this method returns an null value, in order to keep and previously compatible compatibility, this method is now not canceled. In the later API version, this method will be canceled. 16, GetServletNames
Public Enumeration GetServletNames (); initially used to return a list of String objects, which represents all known servlet object names in this servlet environment. This list always contains this servlet itself. This is also a dangerous approach based on the same reason as the previous method. Now this method returns an empty list. In order to maintain and previously versions, this method is now not canceled. In the later API version, this method will be canceled. 17, GetServlets
Public Enumeration GetServlets (); initially used to return a list of all known servlet objects in this Servelet environment. This list always contains this servlet itself. This is also a dangerous approach based on the same reasons for the GetServlet method. Now this method returns an empty list. In order to maintain and previously versions, this method is now not canceled. In the later API version, this method will be canceled. 5. ServletRequest interface definition
Public interface servletRequest Defines an object generated by a servlet engine. Through this object, the servlet can obtain the data requested by the client. This object provides all the data including the parameters by reading the data of the request body, and all the data of the input stream. Method 1, GetAttribute
Public Object GetAttribute (String Name); Returns the value of the specified attribute in the request, if this property does not exist, return an null value. This method allows access to some request information that does not provide other methods in this interface and other servlets placed within this request object. 2, GetaTtributeNames
Public Enumeration GetAttributeNames (); Returns a list of all attribute names contained in this request. 3, getcharacterencoding
Public string getcharacterencoding (); Returns the character encoding type of the input content in the request, if the character encoding type is not defined, returns a null value. 4, getContentLength
Public int getContentLength (); the length of request content, returns -1 if the length is unknown. 5, getContentType
Public String getContentType (); Returns the MIME type of request data, if the type is not known to return null values. 6, GetInputStream
Public servletinputStream getInputStream () throws oException; returns an input stream to read binary data from the request body. If you have obtained the result to read through the getReader method, this method will throw an ILLEGALSTATEEXCEPTION. 7, getParameterPublic String getParameter (String name); Returns the value of the specified parameter with a string, if this parameter does not have a return null value. For example, in an HTTP Servlet, this method returns a value of the parameter generated by the specified query representation or a parameter value in a submitted form. If a parameter name corresponds to a few parameter values, this method can only return the first value in the array returned by the getParameterVALUES method. Therefore, if this parameter is (or possibly) multiple values, you can only use the getParameterVALUES method. 8, getParameternames
Public Enumeration getParameterNames (); returns a list of String objects of all parameter names, if no parameters are input, the method returns an null value. 9, getParameterValues
Public string [] getParameterValues; Returns the value of the specified parameter through an array of String objects, if this parameter does not exist, the method returns an null value. 10, GetProtocol
Public String getProtocol (); Returns the protocol used to request, its form is the protocol / home version number. Supreme version number. For example, for an HTTP 1.0 request, the method returns http / 1.0. 11, GetReader
Public BufferedReader getReader () THROWS IOException; This method returns a buffered reader to read the entity of the requested body, and its encoding method is in accordance with the encoding method of requesting data. If the input stream of this request has been called by GetInputStream, this method will throw an IllegalStateException. 12, GetRemoteAddr
Public String getRemoteAddr (); returns the IP address of the sending requestor. 13, GetRemotehost
Public String getRemotehost (); Returns the host name of the sending requestor. This method will return to the IP address if the engine can't or if the engine is not resolved (in order to improve performance). Getscheme
Public string getscheme (); Returns the mode of the URL used to request. For example, for an HTTP request, this mode is http. 15, GetServerName
Public String getServerName (); Returns the host name of the receiving server. 16, GetServerport
Public int GetServerPort (); Returns the port number of the received request. 17, SetaTRibute
Public void setttribute (String Name, Object Object); This method adds an attribute to the request, which can be used by other objects that can access this request object (eg, a nested servlet). Note: The following methods will be canceled 18, getRealPath
Public String getRealPath (String path); Returns the true path corresponding to the virtual path, if this process cannot be performed because of some reason, the method will return an null value. This method is repeated with the getRealPath method in the servletContext interface. In version 2.1, the ServletContext interface will clarify the mapping of all paths that a servlet can use. The result of this method will be exactly the same as the result of the getRealPath method in servletContext. 6. The servletResponse interface defines the public interface servletResponse defines an object generated by a servlet engine. Through this object, servlet responds to the client's request. This response should be a MIME entity, which may be an HTML page, image data, or other MIME format. Method 1, getCharacterencoding
Public String getcharacterencoding (); Returns the character encoding of the MIME entity. This character encoding can be a specified type or a type that is most matching with the client encoded by the client reflected by the request header field. In the HTTP protocol, this information is transmitted to the servlet engine via accept-charset. For more information on character encoding and MIME, see RFC 2047.2, GetOutputStream Public ServletOutputStream GetputStream () THROWS IOException; Returns a record binary response data output stream. If this response object has called GetWrit, IllegalStateException will be thrown. 3, GetWriter
Public PrintWriter getWriter throws oException; This method returns a Princewriter object to record formatted responses. If you want to reflect the character encoding used, you must modify the MIME type of the response. The Content type of the response must be set before calling this method. If this encoding type is not provided, a unsupportedEncodINGException will throw a getOutputStream if this response object has called GetOutputStream. 4, SetContentLength
Public void setContentLength; Set the length of the content of the response, this method overrides the previously setup length setting. In order to ensure the successful setting of the content length, this method must be called before the response is submitted to the output stream. 5, setContentType
Public void setContentType (String Type); This method is used to set the Content type of the response. This type may be implicitly modified in some cases, and additional situations mention here may be set to MIME characters when the server finds necessary. This method must be called before the response is submitted to the output stream to ensure successfully set the response to the output stream. 7. SingleThreadModel interface definition
Public Interface SingleThreadModel; This is an air interface that specifies how the system handles calls to the same servlet. If a servlet is specified by this interface, then two threads will not be executed simultaneously in the service method in this servlet. The servlet can achieve this guarantee by maintaining a separate servlet instance pool or by only one thread in the Servlet's service. Eight, GenericServlet class
Public Abstract Class GenericServlet Implements Servlet, ServletConfig, Serializable
The presence of this class makes it easier to write servlets. It provides a simple solution that performs a method for the Servlet lifecycle and describes the servletconfig object and ServletContext objects when initialization. Method 1, DESTROY
Public void destroy (); DESTROY method does not do anything else. 2, GetInitParameter
Public String GetInitParameter (String Name); this is a simple way, which will call the same name of the servletconfig object. 3, GetInitParameterNames
Public Enumeration GetInitParameterNames (); this is a simple way, which will call the same name of the servletConfig object. 4, GetServletConfig
Public servletconfig getServletConfig (); Returns a description of the servletconfig object generated by this type of init method. 5, GetServletContext
Public servletContext (); this is a simple way, which will call the same name of the servletconfig object. 6, GetServletInfo
Public string getServletInfo (); return a String that reflects the servlet version. 7, INIT
Public void init () throws servletexception;
Public void init (servletconfig config) THROWS servletexception;
The init (servletconfig config) method is a simple way to initialize the life cycle of this servlet. The init () method is used to expand your genericServlet class. When you use this method, you don't need to store the config object, nor does it need to call Super.init (config). The init (servletconfig config) method stores the config object and then calls init (). If you overload this method, you must call Super.init (config) so that other methods of the GenericServlet class work. 8, log
Public void log (String MSG);
Public void log (String Msg, Throwable Cause);
Write the servlet class name and a given information through the servlet content object. 9, Service
Public Abstract Void Service (ServletRequest Request, ServletRESPONSE
Response) Throws ServleTexception, IOException;
This is an abstract way. When you expand this class, in order to perform a network request, you have to execute it. Nine, servletinputStream class definition
Public Abstract Class ServletInputStream Extends InputStream This class defines a stream of request information to read the client. This is an abstract class provided by a servlet engine. A servlet obtains an instructions for a servletinputStream object by using the ServletRequest interface. Subclasses of this class must provide a way to read information about information from the InputStream interface. Method 1, ReadlinePublic int in (Byte [] B, int off, int LEN) throws ioException; starts the byte of the specified length in the specified array from the specified offset from the input stream. If the content of all requests of the line has been read, this read process will end. If you have encountered a new line, the first character of the new line will also be read into the array. Ten, servletOutputStream class definition
Public Abstract Class ServletOutputStream Extends OutputStream This is an abstract class that is used by the servlet engine. The servlet obtains an instructions for the object of one of this type by using the use of the ServletResponse interface. With this output stream, data can be returned to the client. Subclasses of this class must provide a method of writing to the OutputStream interface to the information. In this interface, when a refresh or closing method is called. All data buffers will be sent to the client, that is, the response is submitted. Note that when you close this type of object, you don't have to turn off the hidden Socket stream. Method 1, Print
Public void print (string s) throws oException;
Public Void Print (Boolean B) THROWS IOEXCEPTION;
Public void print (char C) throws oException;
Public Void Print (INT I).
Public Void Print (long L) THROWS IOEXCEPTION;
Public void print (float f) throws oException;
Public Void Print (Double D) THROWS IOEXCEPTION;
Output variables to output streams 2, Println
Public voidprintln () throws oException;
Public void println (string s) throws oException;
Public void println (Boolean B) THROWS IOEXCEPTION;
Public void Println (Char C) Throws oException;
Public Void Println (INT I) Throws oException;
Public Void Println (Long L) THROWS IOEXCEPTION;
Public void println (float f) throws oException;
Public void println (double d) throws oException;
Output variables into the output stream, and add a return line 11, servletexception class definition
Public Class ServletException Extends Exception This exception thrown when servlet encounters problems. Constructor
Public servletexception ();
Public servleTexception (String Message);
Public servletexception (String Message, Throwable Cause);
Public servletexception (throwable cause); constructs a new servleTException, if this constructor includes a throwable parameter, this Throwable object will be used as a reason why it may throw this exception. Method 1, getrootcause
Public throwable getrootcause (); if the reason why this exception is configured, this method will return this, otherwise it returns an null value. 12. UnavailableException class definition
Public Class UnavailableException Extends ServletException will throw this exception regardless of a servlet or temporarily invalid. The servlet will record this exception and the corresponding measures to be taken by the Servlet Engine. The temporary invalid means that the servlet cannot handle the request due to a temporary issue at a certain time. For example, a service (possibly a database) in another different application layer cannot be used. This issue may correct or need other corrective actions. Permanent invalid means that this servlet will not be able to handle the client's request unless the administrator takes measures. For example, this servlet configuration information is lost or the state of servlet is destroyed. The Servlet engine can safely handle these two exceptions, but the normal handling of temporary invalidation allows the servlet engine to be more robust. Special, at this time, the request for servlet is just blocked (or being extended) for a while, which is obviously more scientific than the request before the service is restarted. Constructor
Public UNAVAILABLEXCEPTION (SERVLET Servlet, String Message);
Public UnavailableException (int SECONDS, servlet servlet,
String message;
Constructs a new exception containing the specified description information. If this constructor has a parameter for the number of seconds, it will give the servlet to process the estimated time can be resrelected. If this parameter is not included, this means that this servlet is permanent. Method 1, GetServlet
Public servlet getServlet (); Returns the report invalid servlet. This is used by the servlet engine to identify the affected servlet. 2, getunavailaseconds
Public int getUnavailableseconds (); Returns a servlet expective invalid time, if this servlet is permanent, return -1. 3, ISPERMANENT
Public Boolean ispermanent (); if this servlet is permanently invalid, return to the Boolean TRUE, indicating some management actions to make this servlet available.