Java web; servlet (1)

Source: Internet
Author: User

A Java Web application consists of a set of Servlets, HTML pages, classes, and other resources that can be bound;

It can be run in a servlet container that implements the servlet specification provided by various vendors.

The servlet container provides the runtime environment for Java Web Applications, which manages the life cycle of Servlets and JSPs and manages their shared data.

Currently the most popular servlet containers are:

The built-in servlet container is also available in the TOMCAT;RESIN;J2EE server (such as WebLogic).

Web structure:

Both the classes and LIB directories in the –web-inf directory can hold Java class files, and when the servlet container runs, the ClassLoader for the WEB application will first load the classes directory, followed by the classes in the Lib directory. If a class of the same name exists under both directories, it will be the class in the classes directory; –web-inf is a special directory (all letters are capitalized). This directory is not part of the context path that the Web application can access, which is not visible to the client. However, the contents of this directory are visible to the servlet code.

The servlet container is responsible for the communication between the servlet and the client, and the method of invoking the servlet, where the servlet communicates with the client in a "request/response" pattern.

Procedure for a servlet container to respond to a customer request:

The ①servlet engine checks to see if an instance object of the servlet has been loaded and created. If it is, execute step ④ directly, otherwise, step ②. ② loads and creates an instance object of the servlet: the constructor that invokes the servlet ③ invokes the init () method of the Servlet instance object. ④ creates a ServletRequest object that encapsulates the request and a Servletresponse object that represents the response message, and then invokes the servlet's service () method and passes the request and response objects in as parameters. Before the ⑤web application is stopped or restarted, the servlet engine uninstalls the servlet and calls the servlet's Destroy () method before uninstalling. the servlet's registration and operation:The servlet program must be run through the servlet container, and the storage directory has special requirements that need to be stored in the <web application directory >\web-inf\classes\ directory. The servlet program must register and map its access path in the Web application's. xml file before it can be loaded by the servlet engine and accessed by the outside world. • A <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. details of the servlet mappings:• 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 "/*".

<servlet-mapping>

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

<url-pattern>*.do</url-pattern>

</servlet-mapping>

<servlet-mapping>

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

<url-pattern>/action/*</url-pattern>

</servlet-mapping>

ServletConfig Interface:servlet In some cases, you might need to access the servlet container or access external resources with a servlet container, so the Serlvet engine needs to pass the object that represents the Servlet container to the servlet. In addition, information such as the friendly name and initialization parameters set for a servlet in the Web. xml file also needs to be passed to the object that the servlet servlet engine will represent the servlet container (ServletContext) and the configuration parameter information of the servlet are encapsulated in an object called ServletConfig and passed to the Servlet instance object when it is initialized. The ServletConfig interface is used to define methods that the ServletConfig object needs to provide externally so that these methods can be called in the servlet program to obtain information. The servlet engine invokes the init (ServletConfig config) method of the instance object of the servlet to pass the ServletConfig object to the servlet. The Servlet.getservletconfig () method must return a reference to the ServletConfig object passed in by the Init (ServletConfig config) method. ServletContext Interface:The servlet engine creates a corresponding ServletContext object for each Web application, and ServletContext objects are included in the ServletConfig object. Call the Servletconfig.getservletcontext method to return a reference to the ServletContext object. • Because all servlets in a Web application share the same ServletContext object, the ServletContext object is called a Application object (Web Application object). • Function: ü Get initialization parameters of Web application ü Record log üapplication domain-wide Properties ü Access resource file ü Get virtual path mapped local path üweb application access Üservletcontext other methods get the initialization parameters for the Web application:• The advantage of setting initialization parameters for Web applications is that you can change some parameter information without modifying the servlet source program. The Servletcontext.getinitparameternames method is used to return a enumeration collection object that contains all the initialization parameter names of the Web application, and the Servletcontext.getinitparameter method is used to return a reference The initialization parameter value for the name. • Add <context-param> child elements to the root element <web-app> of the Web. xml file as follows:

<context-param>

<param-name>companyName</param-name>

<param-value>lampbrother</param-value>

</context-param>

Getrealpath (String Path) method: Used to return the local file system path mapped by a virtual path

Java web; servlet (1)

Related Article

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.