Java Development Study Note 5: servlet programming API

Source: Internet
Author: User

I. Basic servlet API
Java EE APIs for servlet mainly include javax. servlet and javax. servlet. HTTP. The former provides basic Servlet classes and interfaces that can be used by web containers, and the latter mainly includes Servlet classes and interfaces related to HTTP. Servlet programming mainly inherits the functions required by these classes as needed.

The javax. servlet package and javax. servlet. http package classes and interfaces are divided into the following categories:
1. servlet basic class
Javax. servlet. servlet
Javax. servlet. http. httpservlet
Javax. servlet. singlethreadmodel
Javax. servlet. genericservlet
These are abstract classes and interfaces that servlet programs need to inherit directly or indirectly. The singlethreadmodel interface ensures that the servlet processes only one customer request at a time.

2. Web requests and responses
Javax. servlet. servletrequest
Javax. servlet. servletresponse
Javax. servlet. servletinputstream
Javax. servlet. servletoutputstream
Javax. servlet. httpservletrequest
Javax. servlet. httpservletresponse

3. Contact the Web Container
Javax. servlet. servletconfig
The servletconfig interface enables the Web container to contact the servlet during servlet initialization (generally when the web server starts and loads the web application. Servlet can use this interface to obtain initial variables and Servlet names.

4. Contact the Web Application
Javax. servlet. servletcontext
This interface is used to connect Servlet and web applications and use Servlet and web programs to transmit data at runtime. Servlet data can also be shared through this interface. Servletcontext is included in servletconfig, which is different from servletconfig: servletcontext has more methods, mainly used during servlet running, servletconfig is mainly obtained from the Web container at servlet startup.

5. Interaction with other Web Resources
Javax. servlet. http. requestdispatcher
Requestdispather supports Servlet and JSP, and calls between other servlets or web resources. In this way, when the servlet needs to introduce other servlet, JSP, or HTML content, it can control the logical relationship between them more flexibly.

6. error exception
Javax. servlet. servletexception
Javax. servlet. unavailableexception

7. Other ancillary classes
Javax. servlet. http. httputils
Javax. servlet. http. Cookie
The former provides some servlet help functions, and the latter corresponds to HTTP cookies.

Ii. Usage of main Servlet classes and interfaces
1. javax. servlet. servlet Interface
This interface defines the basic methods for servlet initialization, service, and service termination. These methods are related to the servlet lifecycle. Javax. servlet. servlet will be implemented by httpservlet and genericservlet. Generally, programmers indirectly use javax. servlet. servlet methods by inheriting httpservlet, such as Init (), service (), and destroy ().
2. javax. servlet. genericservlet class
This class defines a general servlet class. Its usage has nothing to do with the HTTP protocol. It is mainly used for programmers to develop servlet programs for other web protocols themselves. Genericservlet implements the servlet and servletconfig interfaces above. It provides some common methods including servlet lifecycle methods. The servlet that inherits this class must specify its service () method. Its Init () and destroy () methods are basically the same as those of javax. servlet. servlet. In addition, it adds methods such as getinitparameter.
3. javax. servlet. http. httpservlet class
Httpservlet is the most common servlet class. Programmers need to inherit this abstract class and fill in its doxxx method. In addition, pay attention to the thread relationship of each method during programming to avoid servlet errors when multiple Web requests arrive. Its service () method generally does not need to be replaced. It will call methods such as doxxx corresponding to the user request. In a servlet, the most commonly used doget and dopost methods are used, and there is no need to rewrite the dooptions and dotrace methods.
4. javax. servlet. singlethreadmodel Interface
The Web Container ensures that the servlet implementing the singletheadmodel interface runs in single-threaded mode, that is, its service () method does not use multithreading. Web containers can be implemented in two ways. One is to create and manage multiple servlet instances. Each instance processes one request at a time, and the other is to process the requests by a servlet in sequence.
5. javax. Servet. servletconfig Interface
The servletconfig interface allows the Web container to pass some settings information to the servlet during servlet initialization. The setting information is defined outside servlet, for example, configuring the initialization variable specified in Web. XML in the description file in the Web container. Configuration on Servet can bring great flexibility.
6. javax. servlet. servletrequest interface and javax. servlet. servletresponse Interface
These two interfaces define the request from the client and the Response sent to the client. The Web Container establishes the servetrequest object and the servletresponse object based on the customer request and passes the Service () method to the Servlet as a parameter.
You can obtain request parameters (& var1 = value1. ..), request attributes (attributes), and input streams from servletrequest. In addition, it can obtain web protocol information (such as getremoteaddr (), getremotehost (), and getservetname ).
When the Web Container transmits response data to the customer through the servletresponse interface, the data format can mainly include two types of response and text. If the servlet needs to send binary data, the servlet must use the servletoutputstream object obtained by getoutputstream (). If you need to send text data, use getwriter () to get the printerwriter object. Servlet can use setcontenttype () to set the character set of the response data (Note: This setting should be obtained before the getwriter method obtains the printerwriter object ).
7. javax. servlet. servletinputstream interface and javax. servlet. servletoutputstream Interface
Setvletinputstream is used to obtain the data input stream from servletrequest; servletoutputstream is used to write data streams to servletresponse.
8. javax. servlet. http. httpservletrequest, javax. servlet. http. httpservletreponse Interface
These two interfaces are inherited from servletrequest and servletresponse, which provide multiple methods to process HTTP requests and responses. In servlet programming, these two interfaces are passed as parameters to methods such as doget and dopost. You can obtain and set the HTTP header information from these two methods.
9. javax. servlet. http. httpsession Interface
Httpsession provides a session management mechanism. It can be used to identify the association between a user and a series of Web requests. At the same time, httpsession can be used to record certain user information. In Web containers, common methods for implementing sessions include cookie or URL rewriting. The httpsession programming interface represents the session created between the HTTP client and the web server. You can use this interface to query and manage session information.

Related Article

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.