Common servlet Interfaces

Source: Internet
Author: User

Common servlet Interfaces
Public interface Servlet
This interface is an interface that must be directly or indirectly implemented by all servlets. It defines the following methods:
Init (servletconfig config) is used to initialize Servlet
Destory () destroy Servlet
Getservletinfo () obtains Servlet Information.
Getservletconfig () obtains servlet configuration information.
Service (servletrequest req, servletresponse res) run the applicationProgramLogical entry point.

Public abstract class genericservlet implements servlet, servletconfig, java. Io. serializableGenericservlet provides the basic implementation of servlet interfaces. It is an abstract class, and its service () method is an abstract method. The derived class of genericservlet must directly or indirectly implement this method.

Public abstract class httpservlet extends genericservlet implements java. Io. serializable Httpservlet is a servlet class for Web servers that use HTTP protocol. The httpservlet class provides HTTP functions by executing the servlet interface.
The subclass of httpservlet must implement one of the following methods.
Doget: If the servlet supports http get requests, it is used for http get requests.
Dopost: If the servlet supports http post requests, it is used for http post requests.
Doput: If the servlet supports http put requests, it is used for http put requests.
Dodelete: If the servlet supports HTTP Delete requests, it is used for HTTP Delete requests.
Init and destory manage resources occupied by servlet. To manage resources held in the servlet lifecycle, You can reload these two methods.
Getservletinfo obtains Servlet Information.

Javax. servlet. servletconfig
This interface represents the servlet configuration. The main methods are as follows:
Getinitparameter (string name) returns the initialization parameter of a specific name.
Example program:

< Servlet >
< Servlet-name > Helloworldservlet </ Servlet-name >
< Servlet-class > Com. jspdev. ch7.helloworldservlet </ Servlet-class >
< Init-Param >
< Param-name > User </ Param-name >
< Param-Value > BN </ Param-Value >
</ Init-Param >
  </ Servlet >

Use the following in the customer program:

String getparam = Getibitparameter ( " User " );

You can obtain bn.

Getinitparameternames () returns the names of all initialization parameters.
Getservletcontext () returns the reference of the servlet context object.

Servlet exceptions
Public class servletexception extends exception
It contains several constructor methods and a method to obtain the cause of the exception. The method is as follows:
Getrootcause () returns the cause of this servletexception.

Public class unavailableexception extends servletexception
This exception will be recruited when the servlet or filter is temporarily or never used.

Request and response
Httpservletrequest
Statement:Public interface httpservletrequest
The most common method for this interface is to obtain the parameters in the request. The following describes common built-in JSP objects.
This built-in object is an instance of httpservletrequest.
Getcookies () obtain the cookie sent by the client. Returns an array containing all the current cookies in the request. If the request does not contain cookies, an empty array is returned.
Getsession () returns the session associated with the client. If no, It is null.
Getsession (Boolean create) is similar to the previous method. However, if no session is assigned to the client, a new session is created and returned.
Getparameter (string name) gets the value of the specified parameter in the request. If no value exists, null is returned.
Getparametervalues (string name) returns the parameter value named name in the request. This value is often submitted by the checkbox or select control. The obtained value is a string array!

Public interface httpservletresponse extends servletresponse
indicates the HTTP Response to the client,
addcookie (cookie) adds a cookie to the response.
encodeurl (string URL) uses the URL and sessionid to override the URL.
sendredirect (string lication) sends the response to another page or servlet for processing
setcontenttype (string type) sets the MIME type of the response
setcharacterencoding (string charset) set the character encoding of the response

session tracking
httpsession
Public interface htt%esion
the following methods are commonly used
getcreatetime () to return the session creation time. The minimum unit is 1‰ seconds.
GETID () returns a unique identifier. Each session ID is different.
getlastaccessedtime () returns the last request time sent by the client related to the current session object.
getmaxinactiveinterval () returns the final time. A negative value indicates that the session will never expire. Its value is the survival time of the session
getvalue (string name) to return the object on the specified session. If no data exists, null is returned.
getvaluenames () returns the names of all data bound to the session in an array.
the invalidate (); Public void invalidate () method terminates this session. All data bound to this session will be cleared.
putvalue (string name, object Value) binds the specified object to the session with the given name. if it already exists, it will overwrite it. In this case, the valuebound method of the httpsessionbindinglistener interface is called.
removevalue (string name) unbinds
setmaxinactiveinterval (INT interval) sets a number of seconds, which indicates that when the client does not send a request, the maximum length of time the session is maintained by the servlet engine.

Servlet context
Servletcontext
Public interface servletcontext
The Session object is used on the server to maintain the State related to a single customer. When the web application of multiple users maintains a state, the servlet environment (context) is used ). The following are common methods:
Getattribute (string name) obtains the attribute named name in servletcontext.
Getcontext (string uripath) returns the servlet context of the given uripath application,
For example, servletcontext test = getcontext ("/test ");
Removeattribute (string name) deletes an attribute named name.
Setattribute (string name, object OBJ) sets an attribute in servletcontext

Servlet collaboration
Servlet collaboration isRequestdispatcherInterface, which can forward a request to another servlet.
Public interface requestdispatcher
There are two main methods:
Forward (servletrequest, servletresponse response) forwards the request to another resource on the server
Include (servletrequest, servletresponse response) include another resource on the server to the response

filter
Public interface filter
is an interface that must be implemented by the filter, including the following methods.
Init (filterconfig) initializes the service method of the filter
dofilter (servletrequest, servletresponse response, filterchain chain.
destory () releases the resources occupied by the filter.

filterchain
Public interface filterchain
it is the filter chain of Code , this interface is used to transfer a filtering task between different filters.
method:
dofilter (servletrequest, servletresponse response)
This method is used to call the next filter. If there is no next resource, call the target resource
filterconfig
Public interfacr filterconfig
indicates the filter configuration. Like servlet, filter also has some configuration information. Method:
getfiltername () returns the Filter Name.
getinitparameter (string name) obtains the initialization logarithm named name.
getservletcontext () returns the servlet context object of the filter.
getinitparameternames () obtains the names of all the initialization parameters in the filter configuration.

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.