Servlet Learning Note _1

Source: Internet
Author: User

I. Dynamic pages and static pages
Dynamic pages & static pages: If a browser accesses a page of a Web server at different times under different conditions, the content of the page that the browser obtains changes, then the page is called a dynamic page. The difference between a dynamic page and a static page is that the access request to the page The Web page content that the server can return to the client under different conditions is the same. When you want a Web browser to access a Web page, you can query for real-time information about dynamic changes, and you must use dynamic pages.
Dynamic pages and Dynamic HTML pages: Although dynamic HTML pages can also change the content of the page, this result is only the result of the browser execution, not the server's program execution, The result of the change in the source code of the browser page. This is what it says. Dynamic Web pages are Web pages that change the content of a Web server's pages that are returned to the browser at different times. You can see it by looking at the source code.

Two. Engine and Dynamic Web program

The engine interacts with the application interface (API) and the Dynamic Web program. The engine accepts the request from the server, and is responsible for transferring the request to the Dynamic Web program and returning the Dynamic Web program to the server. The specific workflow is as follows:

Three. Getting Started with servlet development
The 1.servletapi:servlet engine communicates with the servlet program through SERVLETAPI, and in fact, the servlet program is a Java class called Servletapi that runs on the Web server side.
Wildcard characters for 2.Servlet paths: In the <url-pattern> tab, you can use wildcards, there are two fixed formats, one format is *. Extension, cannot have a directory separator in front of *, another format starts with/, and ends with/*. For example:/action /*. When the request is accepted, the most specific matching principle will be used to match the mapping, where *. The extension has the lowest priority. Priority is a specific match (for example,/ABC) >/* match (for example,/abc/*) >*. Extension matches (for example, *.ABC) > The default servlet.

The 3.Servlet program cannot be set as the default open document for Web pages, for example, add a line to XML:

You will still not be able to jump to Bservlet to perform the results when you visit the homepage.

4. Basic concepts of Class Loaders:
ClassLoader--(byte-code data for classes that can be read by a Java Virtual machine)-->java Virtual machine--(Converts the class's bytecode data into executable binary data)--memory
Bootstrap class Loader: Loads a class in a class (Rt.jar) in a core package. (The kernel of the Java Virtual machine is to be loaded without the turnstile's ClassLoader)
Extclassloader: Classes that are responsible for loading in Jre/lib/ext
Appclassloader: The startup execution class that is responsible for loading the application. For example, Java Test. Load Test class with Appclassloader
Class loading takes a delegate mechanism, that is, when a class is loaded, it is delegated to the current ClassLoader's parent loader, upward, and then loaded from the root ClassLoader until the current class loader, if it still fails to load the class's
The classnotfoundexception exception is reported. So in this case, if a class is called in the writing of a class, and the class needs to be loaded with the subclass loader of the current class, the called class will not be loaded. Because the load task of the parent ClassLoader is not delegated to the child ClassLoader.
Tomcat's classloader mechanism: (Note that Catalinaclassloader and Sharedclassloader are assigned to the Commonclassloader example in Tomcat7, and the code is as follows:

Private voidinitclassloaders () {Try{Commonloader= Createclassloader ("Common",NULL );if(Commonloader = =NULL) {Commonloader= This. GetClass (). getClassLoader ();} Catalinaloader= Createclassloader ("Server", Commonloader); Sharedloader= Createclassloader ("Shared", Commonloader);} Catch(Throwable t) {handlethrowable (t); log. Error ("Class Loader creation Threwexception", T); System. Exit (1);}}

The Tomcat class loader loading mechanism such as: (appclassloader main load Catalina/conf,webappclassloader loaded is the/web-inf/classes directory under the class file, Common loading Catalina_home/lib)

Four. servlet features and operating procedures
1.servlet Execution Process:

2.servlet running process.
1> When an access request to a servlet is accepted, the engine first checks to see if the instance object (initialization) of the servlet has been mounted and created. If a Servlet object has already been created, Then create a HttpServletRequest object that encapsulates the HTTP request and a HttpServletResponse object that represents the HTTP corresponding message, then invoke the Servlet's service method and pass the parameters in.
2> If you do not create a Servlet object, create and load a Servlet object
3> Invoke the Init method of the Servlet object to complete some of the necessary initialization operations.
4> Also in newly created objects, the pass-through parameters ServletRequest and servletresponse.httpservletrequest encapsulate the various contents of the request message. The initial httpservletresponse has no content, but it can call various methods to generate the corresponding parts later. When the Servlet service method finishes executing, the Web server can read the corresponding message from the corresponding object
5> Before the application is stopped or restarted, the servlet engine unloads its servlet. Before uninstalling the servlet, the Destory method of the Servlet object is called to complete some necessary cleanup operations.
Precautions :
1> the 2nd and 3rd steps are completed at the time of the first visit to the servlet.
2> In the servlet's entire lifecycle, its Init method is only called once, and the service method, each time a request is made against the servlet, will be called. Each time the HttpServletRequest and HttpResponse objects are repackaged, the servlet can get the required data from the HttpServletRequest object. And the method that invokes the corresponding object writes the corresponding content to the buffer of the servlet engine. The Web server is then sent to the client.
3> If you modify a servlet that has already been loaded, the client still accesses the original servlet unless the server is restarted, because the servlet is only loaded once during the server's run. Even if the servlet is modified on the hard disk, However, the old servlet is still used on the server.
The 3.servletapi:servlet engine is responsible for creating the Servletrequest,servletresponse object, and how does it interact with the servlet? In fact, according to the basic principle of object-oriented, only need to provide the corresponding API ( Standard), and then the servlet engine is responsible for creating the specific implementation class object. In the service method, a reference to the corresponding interface is accepted, and the servlet engine and servlet interaction can be implemented.

Servlet Learning Note _1

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.