Some details of 3--servlet's experience.

Source: Internet
Author: User
Tags tomcat tomcat server

1. Using the servlet API

The Servlet API defines a complete set of interfaces and classes, which makes it easy for developers to develop a servlet, an inheritance hierarchy of interfaces and classes:

Servlet Interface Sun Company defines two default implementation classes, namely: Genericservlet, HttpServlet.

2.Servlet interface Implementation Class

HttpServlet refers to a servlet capable of handling HTTP requests, which adds some and HTTP protocol processing to the original servlet interface, which is more powerful than the servlet interface. As a result, developers should typically inherit this class when writing a servlet, rather than implementing the Servlet interface directly.

HttpServlet when implementing the Servlet interface, the service method is covered, and the code in the body of the method automatically determines how the user is requested, such as a GET request, and calls HttpServlet's Doget method, such as a POST request, The Dopost method is called. As a result, developers usually need to overwrite the Doget or Dopost method when writing a servlet, rather than overwrite the service method.

3. Details of the servlet 1

• Because the client accesses resources in the Web server through a URL address, the servlet program must map the servlet program to a URL address if it wants to be accessed by the outside world , and this work is done using the <servlet. xml file > elements and <servlet-mapping> elements are complete.

the <servlet> element is used to register a servlet, which contains two primary child elements:<servlet-name> and <servlet-class>, Used to set the registration name of the servlet and the full class name of the servlet, respectively.

• An <servlet-mapping> element is used to map an external access path to a registered servlet that contains two child elements:<servlet-name> and <url-pattern> Used to specify the name of the servlet's registry and the external access path of the servlet, respectively. For example:

<web-app>

<servlet>

<servlet-name>AnyName</servlet-name>

<servlet-class>HelloServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>AnyName</servlet-name>

<url-pattern>/demo/hello.html</url-pattern>

</servlet-mapping>

</web-app>

• The same servlet can be mapped to multiple URLs , that is, the set value of the <servlet-name> child elements of multiple <servlet-mapping> elements can be the registered name of the same servlet.

* Wildcard characters can also be used in URLs mapped to by Servlets , but only in two fixed formats: one format is "*. Extension" and the other is preceded by a forward slash (/) and ends with "/*".

*. extension

<servlet-mapping>

<servlet-name>

Anyname

</servlet-name>

<url-pattern>

*.do

</url-pattern>

</servlet-mapping>

a forward slash (/) begins with a "/*" Ending

<servlet-mapping>

<servlet-name>

Anyname

</servlet-name>

<url-pattern>

/action/*

</url-pattern>

</servlet-mapping>

4. For some of the following mapping relationships:

Servlet1 Mapping to/abc/*

Servlet2 Map to/*

Servlet3 Mapping to/ABC

SERVLET4 Mapping to *.do

Questions:

• When the request URL is "/abc/a.html", "/abc/*" and "/*" match, which Servlet responds

the servlet engine calls Servlet1.

• When the request URL is "/ABC", both "/abc/*" and "/ABC" match, which Servlet responds

the servlet engine calls Servlet3.

• When the request URL is "/abc/a.do", both "/abc/*" and "*.do" match, which Servlet responds

the servlet engine calls Servlet1.

• When the request URL is "/a.do", "/*" and "*.do" match, which Servlet responds

the servlet engine calls Servlet2.

• When the request URL is "/xxx/yyy/a.do", "/*" and "*.do" match, which Servlet responds

the servlet engine calls Servlet2.

5. Servlit Details 2

a servlet is a Java class that is called by another Java program (Servlet engine) and cannot run independently , and it is run entirely by the servlet engine to control and dispatch.

• Multiple servlet requests for the client, typically, the server creates only one Servlet instance object, that is, once the Servlet instance object is created, it resides in memory and serves subsequent requests until the Web container exits. The servlet instance object will not be destroyed.

• The Servlet's Init method is called only once throughout the servlet's life cycle . Each access request to a servlet causes the servlet engine to invoke the Servlet's service method once. For each access request, the Servlet engine creates a new HttpServletRequest request object and a new HttpServletResponse response object. The two objects are then passed as arguments to the service () method of the servlet that it invokes, and the service method calls the Doxxx method separately, depending on the request.

If a <load-on-startup> element is configured in the <servlet> element, the Web application will mount and create an instance object of the servlet when it is started, and the Init () method that invokes the Servlet instance object.

Example:

<servlet>

<servlet-name>invoker</servlet-name>

<servlet-class>

Org.apache.catalina.servlets.InvokerServlet

</servlet-class>

<load-on-startup>2</load-on-startup>

</servlet>

Purpose: Write a initservlet for the Web application, which is configured to mount at boot time, creating the necessary database tables and data for the entire Web application.

• If a servlet's mapping path is just a forward slash (/), the servlet becomes the default servlet for the current Web application.

• The URL of a matching <servlet-mapping> element cannot be found in the Web. xml file, and their access requests are given to the default servlet processing , i.e., The default servlet is used to handle access requests that are not processed by all other servlets.

• Installation directory in <tomcat >\conf\ Web. xml file, a servlet named Org.apache.catalina.servlets.DefaultServlet is registered, and the servlet is set to the default servlet.

• When accessing a static HTML file and picture in a Tomcat server, you are actually accessing the default servlet.

6. Precautions

There can be a lot of servlet instances under the same Web project, and a servlet class will start a servlet instance, so the init () method invoked in the class or the Destory () method will access the mapped URL of the class when it starts access in IE.

When testing the Inti () method and the Destory () method of the servlet with MyEclipse, to start or pause the Tomcat service, you cannot close the console directly, so that the JDK is stopped.

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.