Java Servlet API Description Document (2.1A) (2)

xiaoxiao2021-03-06  43

Observation of API / R

This part 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.

Software package: javax.servlet

The interface included: RequestDispatcher; servlet; servletconfig; servletContext; servletRequest; servletReadModel.

The class: genericServlet; servletinputstream; servletoutputstream; servletException; unavailableException.

First, the RequestDispatcher interface:

definition:

Public interface requestdispatcher;

Define an object, receive a request from the client, and 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 began outside a servlet that calls include Include.

Second, the servlet interface.

Definition / R

Public interface servlet

This interface defines a servlet: a Java class that inherits this feature on the web server.

method

1, INIT

Public void init (servletconfig config) THROWS servletexception;

After the servlet is instantiated in the servlet, the Servlet is exactly called the init method 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 itself to the host's servlet. The returned string should be a plain text format without any flag (such as HTML, XML, etc.).

Third, servletconfig interface

Definition / R

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, GetinitParameterNames

Public Enumeration GetInitParameterNames ();

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 GetServletContext ();

Returns the servletContext object of this servlet.

Fourth, servletContext interface

Definition / R

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, which includes servlets and resources of a specific URI path. If the path does not exist, an null value is returned. 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, an null value is returned. 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, getResource

Public url getResource (String uripath);

Returns a URL object that reflects the resources known to the servlet environment objects of 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.), return a specific URL RequestDispatcher object, otherwise, return an null value, the servlet engine is responsible for encapsulating the target with a Request Dispatcher object path. This Request Dispatcher object can be used to fully request.

11, GetServerInfo

Public String getServerInfo ();

Returns a String object that 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 use will be canceled / r

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 SetaTribute (String Name, Object O);

Give a name that you specify in the SERVLET environment object.

14, RemoveAttribute

Public void Removettribute (String Name);

Remove an attribute from the specified servlet environment object.

Note: The following methods will be canceled / r

15, GetServlet

Public servlet getServlet (String name) Throws servletexception;

Originally used to return a servlet that specifies the name, if you have not found a 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, the method will be canceled.

16, GetServletNames

Public Enumeration GetServletNames ();

Originally 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, the method will be canceled.

17, GetServlets

Public Enumeration GetServlets ();

Originally 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, the method will be canceled.

V. ServletRequest interface

Definition / R

Public Interface ServletRequest

Define objects 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, and return null values ​​if the character encoding type is not defined.

4, getContentLength

Public int getContentLENGTH ();

Request the length of the content, returns -1 if the length is unknown.

5, getContentType

Public String getContentType ();

Returns the MIME type of the request data, if the type is unknown 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, getParameter

Public 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, the 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 oException;

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 getRemoteAddddr ();

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 / r

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.

Sixth, servletResponse interface

Definition / R

Public Interface ServletResponse

Define objects generated by a servlet engine, through this object, servlet respond 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 getOutputStream () THROWS IOException; returns an output stream of record binary response data.

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 response entities. 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; INT LENGTH

Set the length of the content of the response, this method covers the previously set of content length.

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.

Seven, SingleThreadModel interface

Definition / R

Public interface SingLethreadModel;

This is a void, which 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 / R

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

Here the 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 GetServletContext ();

This is a simple way, which will call the same name of the servletconfig object. 6, GetServletInfo

Public String getServletInfo ();

Returns 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 / R

Definition / R

Public Abstract Class ServletInputStream Extends InputStream

This class defines the input stream for reading the request information of 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, Readline

Public int ready (Byte [] B, int off, int LEN)..

From the specified offset from the input stream, the byte of the specified length is read into the specified array. 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 / R

Definition / R

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 IOException; Public Void Print (INT I) Throws oException;

Public Void Print (long L) THROWS IOEXCEPTION;

Public void print (float f) throws oException;

Public Void Print (Double D) THROWS IOEXCEPTION;

Output variables into the output stream

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 carriage return / R

Eleven, ServletException class / R

Definition / R

Public Class ServleTexception Extends Exception

An exception thrown when the servlet encounters problems.

Constructor

Public servletexception ();

Public servleTexception (String Message);

Public servletexception (String Message, Throwable Cause);

Public servletexception (throwable cause);

Construct 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 ();

This method will return this reason if it is configured, this method will return this, otherwise it returns an null value.

Twelve, UnavailableException class / R

Definition / R

Public Class UnavailableException Extends ServletException

Whether a servlet is permanently or temporarily invalid, this exception will be thrown. 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 UnavailableException (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 the timeless time of the servlet, 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.

转载请注明原文地址:https://www.9cbs.com/read-61166.html

New Post(0)