Introduction to the Web Access name and the servlet family of the servlet

Source: Internet
Author: User


Web Access name of the servlet

The Web access name of a servlet can be more than one, and in addition to the XML configuration, it can also be configured using annotations, now the main configuration is to use annotations, which is the Servlet3.0 feature, the annotations are configured to be lighter and simpler than web. XML, But do not rule out some old projects still use the configuration of Web. Xml.

Here's a look at using the * wildcard on annotations:

    1. /admin/* This configuration means that the name of the access must start with admin/, but the subsequent string can be arbitrary, * as a match for any string, the following uses the actual code example to illustrate this usage:

650) this.width=650; "style=" width:553px; "src=" http://note.youdao.com/yws/public/resource/ b53ac070a11631cd6ed43a2c4306f000/xmlnote/office82450646faf54314a701b6861be21e26/4405 "alt=" 4405 "/>


Operation Result:

650) this.width=650; "style=" width:553px; "src=" http://note.youdao.com/yws/public/resource/ b53ac070a11631cd6ed43a2c4306f000/xmlnote/office39628d36a7f148e784f2911abbae00b1/4406 "alt=" 4406 "/>


Console:

650) this.width=650; "style=" width:380px; "src=" http://note.youdao.com/yws/public/resource/ b53ac070a11631cd6ed43a2c4306f000/xmlnote/officec7e0c935b6944150b9851be5f10bbcb0/4407 "alt=" 4407 "/>


    1. *.action means that the name suffix of the access must be an action, and the prefix can be any string:

Operation Result:

650) this.width=650; "style=" width:553px; "src=" http://note.youdao.com/yws/public/resource/ b53ac070a11631cd6ed43a2c4306f000/xmlnote/office67f242303c5c4c7fa573f31a388c92b8/4408 "alt=" 4408 "/>


Console:

650) this.width=650; "style=" width:373px; "src=" http://note.youdao.com/yws/public/resource/ b53ac070a11631cd6ed43a2c4306f000/xmlnote/office95941f584a894eb9932838c2d42eb2c9/4409 "alt=" 4409 "/>



Servlet family

The highest parent class in the Servlet family derivation system is the Servlet interface, which defines the basic methods that the implementation class must override. Then there is the Genericservlet class, an abstract class, a generic servlet that implements the Servlet interface, which is equivalent to adding a branch to the Servlet family's derivation system, and then to the HttpServlet class with the protocol. This is also an abstract class that inherits from the Genericservlet class, but this class does not define an abstract method, which is a servlet with an HTTP protocol that is specifically developed for HTTP.

Today's Web Access is almost always an HTTP protocol or an HTTPS protocol, so why not define the protocol on the parent class? If this is done, it violates the six principles of the design pattern, the highest parent class is written dead with what protocol, then later derived from other protocols or need to use other protocols, it is not able to expand, only the parent class can be modified? This is absolutely not allowed in the program design, one of the six principles of the model is the open-closed principle: To modify the closed-open to the extension.

And the principle of single responsibility does not allow a class to contain too many different functions, because it creates unnecessary high coupling, and a class only does one major thing. So the engineers who develop the servlet family are smarter, use a top interface to define the basic methods that all servlet classes must have, and then let the subclasses do it, and the different protocols only need to add different subclasses, no need to modify the parent class, and the subclasses do not depend on each other. This is the Dimitri rule, which abstracts the dependencies of all classes onto an interface parent class, which is interface-oriented programming.

The concept of interface-oriented is like repairing a computer, one of the computer parts is broken, the direct replacement can be, do not need to replace other accessories. For example: the memory bar is broken, I will change the root memory bar, I do not need to change the hard disk and do not need to change the motherboard. Replacing an accessory completely does not affect the normal use of other accessories, because they are not dependent on each other, just as subclasses do not depend on each other, they all rely on only one interface, as long as the interface is appropriate to install it can be used (extension subclass). Subclasses do not rely on the coupling of each other, must not will not affect each other, as if you repair the computer seems to be very simple, basically change an accessory (after all, there are anti-hang interface). But let you go to repair the radio is not so simple, the radio accessories are basically integrated in a circuit board, all diodes, resistors, single-chip computer or something, in addition to the special repair of the general people will not repair, this is because of the high coupling. If the sub-class is like a circuit board as well, think about how troublesome it is to modify it once it has a problem.

So why not like the parts of the computer, the complexity of the sub-class packaging, so that they are only dependent on a parent interface, the problem only need to modify or replace a subclass, and when it is necessary to increase the functionality of the sub-class directly, do not need to make other changes, Just like my computer to the sound quality of a little bit on a separate sound card, want to speed up a bit faster I can add a network card, which is why to interface programming, why the development of the servlet family of engineers can be flexible, extensibility to do so well, this is because of the application of interface-oriented programming.


The above used a lot of nonsense to explain why interface-oriented programming, and interface-oriented programming benefits, next introduce the servlet interface, Genericservlet, HttpServlet class, the Main method (not all):

1. Servlet interface

There are three main methods in the Servlet interface, namely:

Init (ServletConfig) initialization method, which is called after the servlet instantiates the object.

Service (ServletRequest, Servletresponse) services, methods for receiving client requests

Destroy () Destruction method


Servlet Interface Source code:

650) this.width=650; "style=" width:553px; "src=" http://note.youdao.com/yws/public/resource/ b53ac070a11631cd6ed43a2c4306f000/xmlnote/office8d891df4ebb549a8a425ca664ba6c8f1/4410 "alt=" 4410 "/>


2. Genericservlet Abstract class

There are four main methods, namely:

Init (ServletConfig) initialization method, which is called when the same initialization occurs

Init () initialization method

Service (ServletRequest, Servletresponse) Services method

Destroy () Destruction method


There is an init () in Genericservlet, which is used to rewrite the user, and by overriding this method we can do something at initialization. In fact, this method will be called by the Init (ServletConfig) method, so in order to do something during initialization, look at the source code will know:

650) this.width=650; "style=" width:553px; "src=" http://note.youdao.com/yws/public/resource/ b53ac070a11631cd6ed43a2c4306f000/xmlnote/officee23bf270dd9849f7b13d991bdacf4b74/4411 "alt=" 4411 "/>


3. HttpServlet Abstract class

HttpServlet methods are more, the main is the following methods:

Service (ServletRequest, servletresponse) This is the overridden method of the parent class, Here the ServletRequest and Servletresponse parameter objects are cast to the HttpServletRequest and HttpServletResponse objects of the HTTP protocol. The following service (HttpServletRequest, HttpServletResponse) method is called after the conversion is complete.


Service (HttpServletRequest, HttpServletResponse)

This is HttpServlet service method, is specific to the HTTP protocol service method, this method will be called by service (ServletRequest, Servletresponse), The HttpServletRequest and HttpServletResponse parameter objects of this method are passed by this non-protocol service method.


Do ... Series methods, this series of methods are called by the Service method with the HTTP protocol, and their parameter objects are passed in by this service method:

Doget (HttpServletRequest, HttpServletResponse)

When a browser is accessed through a connection such as the URL of the address bar or a hyperlink, the Doget method is called by default, and the commit access in the form is an optional invocation.


DoPost (HttpServletRequest, HttpServletResponse)

When a browser is accessed through a form submission, you can choose to call the Dopost method.


The following five methods are called, which need to be defined in the HTTP request header text:

Dohead (HttpServletRequest, HttpServletResponse)

DoPut (HttpServletRequest, HttpServletResponse)

DoDelete (HttpServletRequest, HttpServletResponse)

Dooptions (HttpServletRequest, HttpServletResponse)

Dotrace (HttpServletRequest, HttpServletResponse)


From the characteristics of these methods, we can see that the HttpServlet will invoke the method of the Do series, which is determined by the request method defined in the browser, we may view the service (HttpServletRequest, HttpServletResponse) The source of the method, see how the Do series method is called:

650) this.width=650; "style=" width:553px; "src=" http://note.youdao.com/yws/public/resource/ b53ac070a11631cd6ed43a2c4306f000/xmlnote/office899c5bfa94484b438609988006b06019/4412 "alt=" 4412 "/>

650) this.width=650; "style=" width:553px; "src=" http://note.youdao.com/yws/public/resource/ b53ac070a11631cd6ed43a2c4306f000/xmlnote/office1912ee38041f4352b0021546f8715966/4413 "alt=" 4413 "/>


From observing the derivation of the servlet classes, you know that if you want to receive any type of browser request, you just need to rewrite the service method.

Here's a flowchart to look at the servlet's access process:

650) this.width=650; "style=" width:553px; "src=" http://note.youdao.com/yws/public/resource/ b53ac070a11631cd6ed43a2c4306f000/xmlnote/office6ca7169341154a1c97713290d77bd949/4414 "alt=" 4414 "/>


Servlet Family Mind Mapping:

650) this.width=650; "style=" width:553px; "src=" http://note.youdao.com/yws/public/resource/ b53ac070a11631cd6ed43a2c4306f000/xmlnote/office7e3700a5faf34249a4b47057dd6930d7/4416 "alt=" 4416 "/>











This article is from the "zero" blog, make sure to keep this source http://zero01.blog.51cto.com/12831981/1978953

Introduction to the Web Access name and the servlet family of the servlet

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.