Java EE Review Program

Source: Internet
Author: User

For this piece, it is only the use of WEB development technology, and many of the technology is outdated or better replaced by the private, so focus on the most basic Servlet API, session mechanism.

Servlet API

Top-level interfaces: Servlets, ServletConfig, ServletContext

Most commonly used classes: HttpServlet

Servlet interface

Broadly speaking, a class implements a servlet interface so it is called a servlet, meaning that the interface defines the methods that all servlets must implement .

Method at a Glance:

    • Init (ServletConfig)
    • Service (ServletRequest, Servletresponse)
    • Destroy ()
    • Getservletconfig ()
    • Getservletinfo ()

According to the official API explanation, the simple translation came to this point:

A servlet is a small JAVA program that runs on the server side, and the servlet accepts and responds to requests from the client (usually the HTTP protocol);

In order to implement this interface, you can inherit javax.servlet.GenericServlet or inherit javax.servlet.http.HttpServlet .

This interface defines the methods that initialize, process requests, and delete servlets, called life cycle methods, that are called in the following order:

    • The Servlet is constructed to invoke the init() method class.
    • All requests are submitted to service method processing
    • Use destroy method to destroy resources after processing, wait for GC recycle

In addition to the life cycle approach, the interface provides a Getservletconfig method that the Servlet obtains with some boot information.

ServletConfig interface

The ServletConfig object is constructed by the container.

When the servlet is initialized, the object passes some starting arguments to the servlet (in the form of the Init () method parameter)

Simple to understand: the parameters () configured in Web. XML are encapsulated in the ServletConfig <init-param>

Method at a Glance:

    • Getinitparameter (String name)
    • Getinitparameternames ()
    • Getservletcontext ()
    • Getservletname ()

The servlet and ServletConfig are one by one counterparts, and a servlet follows a Config object

ServletContext interface

The servlet context encapsulates some of the methods that communicate with the container where the servlet resides.

Should be very familiar with this, is one of the four most commonly used storage domains, is the largest piece of the range.

ServletContext corresponds to a container ; a servletcontext can correspond to multiple servlets.

It defines a number of commonly used methods, such as obtaining the absolute path of Getrealpath, storing the Get/set attrbiute of the data.

To get the ServletContext centralized way:

    1. Servletconfig.getservletcontext ();
    2. Httpsession.getservletcontext ();
    3. Filterconfig.getservletcontext ();
    4. Servletcontextevent.getservletcontext ();

PS: Since the servletconfig can be obtained, then its implementation of the class is also possible ~

Generciservlet Abstract class

It implements the previously said Servlet and ServletConfig interface, but after all it is an abstract class, only one method is not implemented, so there are eight methods can be used, and there are some of his own methods.

Here are a few important points to say:

    • Init (ServletConfig)
      The default implementation can be summed up as two sentences: {this.config = config;init();} , you can see that this class must have a servletconfig type of property, in Init to save the Config reference to facilitate the use of subsequent methods.
      Then a direct call to an empty parameter of the Init method, this method is Generciservlet own definition, the default NULL implementation, if you have their own initialization logic, rewrite the null parameter method, and no need to call the super.init(config) method.

    • Abstract Service (ServletRequest, Servletresponse)
      Yes, the only one abstract method, the only one that is not implemented.
      The server receives the request to call the service to handle, how the processing should also be implemented by the developer, note that the parameter is ServletRequest, generally we need to strongly turn into httpservletrequest, generally is to handle the Http request.

Then there are two ways to print the log, and that's all.

HttpServlet class

It inherits from the Genericservlet and implements the service method that it has not implemented, and from the name it can be seen that the HTTP request is specifically handled, and the Servlet written by the developer is generally inherited from this class.

And then it implements the method of doing two things:

    1. Turn Servletrequest/servletresponse strong into Httpservletrequest/httpservletresponse
    2. Call your own defined service (HttpServletRequest, HttpServletResponse) method (this method is protected)

So what exactly does the service method that it defines itself do?

Simply by request.getMethod() acquiring the request and then calling the Doxxx method according to the request, developers will generally rewrite their doget and doPost.

Ps:protected-Modified methods can be overridden, but permissions cannot be smaller.

API Summary

From the perspective of the creator, caller, constructor:

API Name implemented by called by Constructor (new)
Servlet Developers Container Container
ServletConfig Container Developers Container
ServletContext Container Developers Container
Request Container Developers Container
Response Container Developers Container
RequestDispatcher Container Developers Container
Cookies Container Developers Developers
HttpSession Container Developers Container
Filter Developers Container Container
Listener Developers Container Container
The process of a request

Server with Tomcat as an example, the process of a request can probably be divided into the following steps:

    1. Server Startup phase
      TOMCAT detects its own configuration file conf directory at boot time and fails to start if there is a problem with the configuration.
      Tomcat checks all the Web. xml files under the WebApps project, if there is a problem with the file, then tomcat error, start normal (Error project cannot access, other works can be used normally).

    2. Enter the address in the browser's address bar, return

    3. The browser packages the address information into a standard HTTP request string

    4. If the domain name is entered, then the DNS is resolved and then sent over the network to the server address

    5. The server accepts the request and resolves and packages the request information into the Httpservletrequest/response object

    6. web.xmllook for the specified url-pattern , according to Servlet-name find Servlet-class

    7. The container detects if there is an object in memory for that servlet, if any, uses the original object, and if not, constructs the Servlet object using reflection

    8. The "Object Not found" container calls Init ()--Method initialization

    9. Container Call Service (ServletRequest)--Doxx

    10. We rewrite the Doxxx method to complete the business logic

    11. The container converts the response object into a standard HTTP response string, which is then sent back to the browser via the network

    12. The browser accepts standard response information, parses, displays

Of course, each of the above steps can actually be subdivided, but here is not divided into too thin.

Java EE Review Program

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.