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 that make it easy for developers to develop a servlet, the interface, and the inheritance hierarchy of classes:

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

2.Servlet interface Implementation Class

HttpServlet refers to the servlet that handles HTTP requests, adding some HTTP protocol processing methods 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 and avoid directly implementing the Servlet interface.

HttpServlet when implementing the Servlet interface, the service method is overridden, and the code inside the method automatically judges the user's request, such as a GET request, and calls the HttpServlet Doget method, such as a POST request, The Dopost method is invoked. As a result, when writing a servlet, developers usually only need to override the Doget or Dopost methods, rather than overwrite the service method.

3. servlet Details 1

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

<servlet> element is used to register the servlet, which contains two main 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.

• A <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 registration name of the servlet and the external access path of the servlet. 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 <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 that the servlet maps to , but there are only two fixed formats: one format is "*. Extension" and the other is preceded by a forward slash (/) and ends with a "/*".

*. Extended name

<servlet-mapping>

<servlet-name>

Anyname

</servlet-name>

<url-pattern>

*.do

</url-pattern>

</servlet-mapping>

start with a slash (/) and End with "/*"

<servlet-mapping>

<servlet-name>

Anyname

</servlet-name>

<url-pattern>

/action/*

</url-pattern>

</servlet-mapping>

4. For some of the following mapping relationships:

Servlet1 Map to/abc/*

Servlet2 Map to/*

Servlet3 Map to/abc

Servlet4 Map to *.do

question:

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

the servlet engine will invoke Servlet1.

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

the servlet engine will invoke Servlet3.

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

the servlet engine will invoke Servlet1.

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

the servlet engine will invoke Servlet2.

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

the servlet engine will invoke Servlet2.

5. Servlit Details 2

a servlet is a Java class that is invoked by other Java programs (servlet engines), which cannot run independently , and is run entirely by the servlet engine to control and schedule.

• Multiple servlet requests for clients, typically, the server creates only one Servlet instance object, which means that once the Servlet instance object is created, it resides in memory, serving subsequent requests until the Web container exits. The servlet instance object is not destroyed.

within the entire lifecycle of the servlet, the servlet's Init method is invoked only once . Each access request to a servlet causes the servlet engine to invoke a Servlet's service method. 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 loads and creates the instance object of the servlet when it starts, 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 Web applications, this servlet is configured to load at startup, 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 is not found in the Web.xml file, and their access requests are processed by the default servlet , i.e. The default servlet is used to handle access requests that are not processed by all other servlet.

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

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

6. Matters needing attention

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

When you test 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 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.