Common servlet interfaces and Methods

Source: Internet
Author: User

1. javax. servlet. servlet Interface
The servlet abstraction set is the javax. servlet. servlet interface, which specifies the set of methods that must be recognized and managed by the servlet engine by the servlet class.
The basic goal of the servlet interface is to provide the lifetime Methods Init (), service (), and destroy.
Methods In the servlet Interface
Void Init (servletconfit config) throws servletexception is performed by the servlet engine after the servlet is loaded and before the service is implemented? Secondary call. If Init () causes an overflow of unavailableexception, servle will exit the service.
Servletconfig getservletconfig () returns the servletconfig object passed to the servlet Init () method.
Void Service (servletrequest request, servletresponse response) throws servletexception. ioexception processes the request described in the request object and returns the request result using the response object.
String getservletinfo () returns a string describing the Servlet
Void destory () is called by the servlet engine when the servlet is to be detached.
Ii. javax. servlet. genericservlet class (protocol-independent Version)
Genericservlet is a protocol-independent servlet. It is a servlet that does not provide services with requests. Instead, it simply starts the background thread from the init () method and (). It can be used to simulate the port monitoring process of the operating system.
The servlet API provides a direct implementation of the servlet interface, called genericservlet. This class provides the default implementation of methods in all interfaces except the Service () method. This means that a basic servlet can be compiled by simply extending genericservlte.
In addition to the servlet interface, genericservlet also implements the servletconfig interface to process initialization parameters and servlet context, and provides methods for passing authorization to the servletconfig object in the init () method.
Methods In the genericservlet class
Void destory () is a registration entry for "destory ".
String getinitparameter (string name) returns the value of the initialization parameter with the specified name. Use config. getinitparameter (name) in progress.
Enumeration getinitparameternames () returns this serv] for all et encoded initialization parameters? Enumerated value. Call config. getinitparameternames () to obtain the list. If no initialization parameter is provided? Null enumerated value (but not null)
Servletconfig getservletconfig () returns the servletconfig object passed to the init () method.
Servletcontext getservletcontext () returns the servletcontext referenced in the config object
String getservletinfo () returns an empty string
Void Init (servletconfig config) throws servletexception saves the config object in an instance variable. Compile the registration entry that makes up the word "init", and then call the method Init ()
Void Init () throws servletexception can be skipped to process servlet initialization. after the config object is saved, the end of Init (servletconfig config) is automatically called. servlet authors often forget to call super. init (config)
Void log (string MSG) is used to compile the servlet registration entry. Therefore, the log () method of the servlet context is called. The servlet name is added to the beginning of the message text.
Void log (string MSG, throwable T) is used to compile a stack track for entry and Servlet registration. This method is also a copy of the corresponding method in servletcontext.
Abstract void Service (request, response) throws servletexception. ioexception is called by the servlet engine to provide services for the request described by the request object. This is the only abstract method in genericservlet. Therefore, it is the only method that must be covered by the quilt class.
String getservletname () returns the servlet name specified in the Web application release Descriptor (Web. XML.
Iii. javax. servlet. http. httpservlet class (HTTP Version)
Although servlet APIs allow extension to other protocols, all servlets are implemented in the Web environment. Only a few servlets directly expand genericservlet. More common for servlets is to extend their HTTP subclass httpservlet.
Httpservlet class implements Service () by calling the method specified to the HTTP request method, that is, for delete, Head, get, options, post, put, and trace, dodelete () is called respectively (), dohead (), doget (), dooptions (), dopost (), doput (), and dotrace () methods, put the request and response objects into their HTTP specified subclass.


Methods In the httpservlet class
Void doget (httpservletrequest request, httpservletresponse response) throws servletexception. ioexception is called by the servlet engine to process an http get request. The input parameters, HTTP header, and input stream can be obtained from the output stream of the request object, response header, and response object.
Void dopost (httpservletrequest request, httpservletresponse response) throws servletexception. ioexception is called by the servlet engine to process an http post request. The input parameters, HTTP header, and input stream can be obtained from the output stream of the request object, response header, and response object.
Void doput (httpservletrequest request, httpservletresponse response) throws servletexception. ioexception is called by the servlet engine to process an http put request. In this method, the request URI specifies the location of the file to be loaded.
Void dodelete (httpservletrequest request, httpservletresponse response) throws servletexception. ioexception is called by the servlet engine to process an HTTP Delete request. The request URI indicates that the resource is deleted.
Void dooptions (httpservletrequest request, httpservletresponse response) throws servletexception. ioexception is called by the servlet engine to process an http options request. Returns an allow response header indicating the HTTP method supported by the servlet. A servlet does not need to override this method, because the httpservlet method has implemented the functions required by the specifications.
Void dotrace (httpservletrequest request, httpservletresponse response) throws servletexception. ioexception is called by the servlet engine to process an http trace request. The request header is fed back to the response mark. A servlet does not need to override this method, because the httpservlet method has implemented the functions required by the HTTP specification.
Void Service (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception Service (request, response) calls an immediate method with the specified HTTP request and response. This method actually directs the request to doget (), dopost (), and so on. This method should not be overwritten.
Void Service (request, response) throws servletexception, ioexception puts the request and response object into its specified HTTP subclass, and calls the specified HTTP service () method.
4. javax. servlet. servletrequest class (protocol-independent Version)
The servletrequest interface encapsulates the details of client requests. It has nothing to do with the Protocol and has a sub-interface that specifies HTTP.
Servletrequest mainly deals:
1. Find the Host Name and IP address of the client.
2. Retrieve Request Parameters
3. Get and set attributes
4. Get input and output streams

Methods In the servletrequest class
Object getattribute (string name) returns the request attribute with the specified name. If it does not exist, null is returned. Attributes can be set by the servlet engine or explicitly added using setattribute.
Enumeration getattributename () returns the enumeration of all attribute names in the request. If no attribute exists, an empty enumeration is returned.
String getcharacteencoding () returns the character encoding used by the request.
Int getcontentlength () specifies the length of the input stream. If it is unknown,-1 is returned.
Servletinputstream getinputstream () throws ioexception returns the request-related (Binary) input stream. You can call one of the getinputstream () or getreader () methods.
String getparameter (string name) returns the specified input parameter. If no parameter exists, null is returned.
Enumeration getparametername () returns an enumeration that may be null for all parameter names in the request.
String [] getparametervalues (string name) returns an array of values for the specified input parameter name. If the value does not exist, null is returned. It is useful when parameters have multiple values.
String get protocol () returns the name and version of the protocol used in the request.
String getscheme () returns the substring of the request URI, but does not contain the content before the first colon.
String getservername () returns the Host Name of the server that processes the request.
String getserverport () returns the port number that the receiving host is listening on.
Bufferedreader getreader () throws ioexception returns a character interpreter for request-related input data. This method and getinputstream () can only be called separately and cannot be used at the same time.
String getremoteaddr () returns the numeric IP address of the client host.
String getremotehost () returns the client host name if it is known.
Void setattribute (string name, object OBJ) saves the reference of the specified object in the request with the specified name.
Void removeattribute (string name): deletes a specified attribute from the request.
If locale getlocale () is known, return the first scene of the client or null.
If enumeration getlocales () is known, an enumeration of the first field of the client is returned; otherwise, the first field of the server is returned.
Boolean issecure () returns true if the request uses an https-secured tunnel.
Requestdispatcher getrequestdispatcher (string name) returns the requsetdispatcher object of the specified source name.
V. javax. servlet. http. httpservletrequest interface (HTTP Version)
Httpservletrequest class mainly deals:
1. Read and Write HTTP header labels
2. Obtain and set cookies
3. Obtain path information
4. Identify the http session.

Methods In the httpservletrequest Interface
String getauthtype () if the servlet is protected by an authentication scheme, for example, HTTP basic authentication, the scheme name is returned.
String getcontextpath () returns the prefix of the URL of the specified servlet context (Web application.
Cookie [] getcookies () returns an array of request-related cookies.
Long getdateheader (string name) converts the output to a simplified version of getheader () suitable for building the long value of the date object.
String getheader (string name) returns the specified HTTP header identifier. If the name is provided by the request, the name is case-insensitive.
Enumeration getheadernames () returns the weights of all HTTP header names given in the request.
Enumeration getheaders (string name) returns the enumerated values of all HTTP header names of the specified type in the request. It is very useful for header labels with multiple values.
Int getintheader (string name) converts the output to a simplified version of getheader () of the int value.
String getmethod () returns the HTTP request method (such as get and post)
String getpathinfo () returns any additional path information specified in the URL.
String getpathtranslated () returns any additional path information specified in the URL. the quilt is converted into an actual path.
String getquerystring () returns the query string, that is, in the URL? The following section.
String getremoteuser () if the user passes the authentication, the remote user name is returned; otherwise, the value is null.
String getrequestedsessionid () returns the client session ID
String getrequesturi () returns a part of the URL, starting with "/", including the context, but not any query string.
String getservletpath () returns the substring after the request URI Context
Httpsession getsession () calls the simplified version of getsession (true.
Httpsession getsession (Boolean create) returns the current HTTP session. If it does not exist, a new session is created. The create parameter is true.
Principal getprincipal () if the user passes the authentication, the Java. Security. principal object representing the current user is returned; otherwise, the value is null.

Boolean isrequestedsessionidfromcookie () If the requested session ID is provided by a cookie object, true is returned; otherwise, false is returned.
Boolean isrequestedsessionidfromurl () If the requested session ID is decoded in the request URL, true is returned; otherwise, false is returned.
Boolean isrequestedsessionidvalid () returns true if the session ID returned by the client is still valid.
Boolean isuserinrole (string role) returns true if the user has been authenticated to be related to the specified role. If the user is not authenticated or fails to pass the authentication, false is returned.
6. javax. servlet. servletresponse interface (protocol-independent Version)
The servletresponse object uploads the result generated by a servlet to the client sending the request. The servletresponse operation is mainly used as a package container for the output stream, its content type and length. It is created by the servlet engine.

Methods In the servletresponse Interface
Void flushbuffer () throws ioexception sends the output content cached to the client. Because HTTP requires header labels to be sent before the content, call this method to send status lines and Response Header labels to confirm the request.
Int getbuffersize () returns the cache size used by the response. If the cache is invalid, 0 is returned.
String getcharacterencoding () returns the name decoded by a character in the response. ISO-8859-1 unless explicitly set
Locale getlocale () returns the field used for response. Unless modified using setlocale (), the default server field is used.
Outputstream getoutputstream () throws ioexception returns the stream used to write the returned binary output to the client. Either method or getwrite () method can only call one of them.
Writer getwriter () throws ioexception returns a character writer that writes the returned text output to the client. This method and getoutputstream () can only call one of them.
Boolean iscommitted () returns true if the status and response header are sent back to the client. It does not work if the response is confirmed.
Void reset () clears the output cache and any response header. If the response is confirmed, the event illegalstateexception is thrown.
Void setbuffersize (INT nbytes) sets the minimum cache size of the response. The actual cache size can be larger, which can be obtained by calling getbuffersize. If the output has been written, illegalstateexception is generated.
Void setcontentlength (INT length) sets the length of the content body.
Void setcontenttype (string type) sets the content type. Set the Content-Type header in HTTP servlet.
Void setlocale (locale) is used to set the response site. In HTTP servlet, the Content-Type header value will be affected.
VII. javax. servlet. http. httpservletresponse interface (HTTP Version)
Httpservletresponse adds a method to indicate the status code, status information, and response header. It is also responsible for decoding the http session ID written to a web page in the URL.

Methods In the httpservletresponse Interface
Void addcookie adds a set-Cookie header to the response.
Void adddateheader (string name, long date) uses the specified date value to add a method with the specified name (or replace all the Response Header labels.
Void setheader (string name, string value) sets a response header with the specified name and value.
Void addintheader (string name, int value) uses the specified integer value to add the response header with the specified name (or replace all header labels with this name ).
Boolean containsheader (string name) returns true if the response contains the header mark of this name.
String encoderedirecturl (string URL) if the client does not know to accept cookid, the session ID is added to the URL. The first method only calls the URLs used in sendredirect. Other encoded URLs should be passed to encodeurl ()
String encodeurl (string URL)
Void senderror (INT status) sets the response status code to a specified value (optional status information ). Httpservleetresponse defines a complete set of Integer constants to indicate valid State values.
Void senderror (INT status, string MSG)
Void setstatus (INT status) specifies the response status code. Only applicable to the response that does not generate an error, while the error response uses senderror ().
8. javax. servlet. servletcontext Interface
A servlet context is an interface provided by the servlet engine to serve web applications. The servlet context has a unique directory mapped to the file system by name (which belongs to the Web application name.
A servlet can reference the servlet context through the getservletcontext () method of the servletconfig object. If the servlet directly or indirectly calls the subclass genericservlet, The getservletcontext () method can be used.
In a web application, servlet can be obtained using the servlet context:
1. Save and retrieve attributes during the call, and share these attributes with other servlets.
2. Read the file content and other static Resources in the Web application.
3. Sending Requests to each other.
4. Record errors and information messages.

Methods In the servletcontext Interface
Object getattribute (string name) returns the object with the specified name in the servlet context, or binds an object with the specified name. From the standard perspective of Web applications, such objects are global objects because they can be accessed by the same servlet at another time. Or any other servlet access in the context.
Void setattribute (string name, object OBJ) sets the object with the specified name in the servlet context.
Enumeration getattributenames () returns an enumeration of all attribute names stored in the servlet context.
Servletcontext getcontext (string uripath) returns the servlet context mapped to another URL. In the same server, the URL must start.
String getinitparameter (string name) returns the initialization parameter value of the specified context range. This method is different from the name of the servletconfig method. The latter is only applicable to the encoded specified servlet. This method is applied to all parameters in the context.
Enumeration getinitparameternames () returns (may be blank) the enumerated value of the name of the initialization parameter value for the specified context range.
Int getmajorversion () returns the maximum and minimum versions supported by Servlet APIs in this context.
Int getminorversion ()
String getmimetype (string filename) returns the MIME type of the specified file name. Typically, it is based on the file extension rather than the content of the file itself (it does not have to exist ). If the MIME type is unknown, null is returned.
Requestdispatcher getnamedispatcher (string name) returns the requestdispatcher of the servlet or JSP with the specified name or path. If requestdispatch cannot be created, null is returned. If a path is specified, it must start with "/" and be relative to the top of the servlet context.
Requestdispatcher getnamedispatcher (string path)
String getrealpath (string path) specifies a URI and returns the absolute path of the URI in the file system. If the ing is not allowed, null is returned.
URL getresource (string path) returns the URL corresponding to the specified absolute path of the servlet context or the input stream that reads the URL. If the resource does not exist, null is returned.
Inputstream getresourceasstream (string path)
String getserverinfo () returns the name and version number of the servlet engine.
Void log (string message)
Void log (string message, throwable t) writes a message to the servlet registration. If the throwable parameter is provided, the stack track is included.
Void removeattribute (string name) deletes the specified attribute from the servlet context.
9. javax. servlet. http. httpsession Interface
Httpsession is similar to the hash table interface. It provides the setattribute () and getattribute () Methods to store and retrieve objects. Httpsession provides a session ID keyword. A client that participates in session behavior stores and returns it in the same session request. The servlet engine searches for appropriate session objects and makes them available to the current request.

Methods In the httpsession Interface
Object getattribute (string name) saves an object in a session as the specified name, and returns or deletes the previously saved name object. Void setattribute (string name, object value)
Void removeattribute (string name)
Enumeration getattributename () returns the enumerated values of all attribute names bound to the current session.
Long getcreationtime () returns a long integer that represents the session creation and last access date and time. This integer is used in the Java. util. Date () constructor.
Long getlastaccessedtime ()
String GETID () returns the session ID, a unique keyword set by the servlet engine.
If ing getmaxinactiveinterval () does not interact with the client, set and return the maximum number of seconds for session survival.
Void setmasinactiveinterval (INT seconds)
Void invalidate () causes the session to be terminated and any objects are released.
Boolean isnew () returns true if the client is not added to the session. When a session is created for the first time and the session ID is passed into the client, but the client does not perform the second request containing the session ID, true is returned.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.