Introduction to Web containers and Web applications

Source: Internet
Author: User

Web applications are server applications. the most basic requirements for its development are as follows: Programming Model and API, supported by the server during runtime, and supported by the implementation (deployment. implementation refers to the process of installing applications on the server. this process also includes configuring application components, such as specifying initialization parameters and specifying any databases.

The Web container is actually a Java runtime. It provides an implementation of the Java Servlet API and simplifies the JSP webpage. the Web container is responsible for initializing, calling, and managing the survival periods of Java Servlet and JavaServer pages. examples of Web containers include tomcat, resin, WebLogic, websphere, and JBoss. Without them, web applications cannot be connected to real network services.

We know that the source code of the servlet API is some interfaces, while the JSP web page is compiled into a servlet before it is put into operation. after the JSP page is updated, the Web Container decides to re-compile and update the servlet. you can understand that these interfaces do not have any implementation code, and the implementation of these interfaces is done by web containers. just as we press the remote control of the TV, and the actual TV playing is completed by the operation interface of the remote control requesting the realization of the TV function, rather than the remote control, it only serves as an abstraction and function abstraction.

A container can run multiple Web applications at the same time. They are generally differentiated and accessed through different Uris. each web application generally contains the following parts: JSP, Servlet, static resources, and implementation descriptor. for tomcat, each $ atat_home/webapps/sub-directory (if this directory contains a/WEB-INF/web. XML file) is a separate web application. web containers ensure that the data between them does not conflict with each other, that is, the session, application, and other server variables of each application have their own memory space, which will not affect each other and are separated.

Deployment descriptors is an integral part of J2EE Web applications (that is, it is the smallest part and an indispensable part ). they help manage web application configurations after application implementation. for Web containers, the implementation descriptor is called Web. XML file, stored in the/WEB-INF directory of the Web application. java Servlet technology provides a document definition type (DTD) for the Implementation descriptor, which can be found from the Web http://java.sun.com/dtd/web-app_2_3.dtd.

The implementation descriptor has multiple purposes:
Provide initialization parameters for servlet and Web applications, which reduces the initialization value of hard-coded code in our web applications. for example, a common tag can provide a parameter for the servlet. This parameter can be loaded in the init () method. struts actionservlet is also through this way to find the location of their required configuration file struts-config.xml, so as to load and analyze it, to initialize the Struts framework used by various frombean, action, forward and so on.
Servlet/jsp definitions can be defined for each servlet in a web application or pre-compiled JSP webpage, including the servlet/jsp name, Servlet/jsp class, and an optional description.
Servlet/jsp ing web containers use this information to map incoming requests to Servlet and JSP web pages.
MIME type because each web application can contain multiple content types, we can specify the MIME type for each type in the implementation descriptor.
Security we can use the implementation descriptor to manage the access control of applications. for example, you can specify whether or not your web application needs to log on. If you need to, what logon page should you use and what role the user will assume.
The implementation descriptor can also be customized with other elements, including welcome pages, error pages, session configuration, and Web application structure.
Web applications have four parts:
A public directory
A WEB-INF/Web. xml file
A WEB-INF/classes directory
A WEB-INF/lib directory
The public directory stores all resources that can be accessed by users, including. html,. jsp,. GIF,. jpg,. CSS,. JS,. SWF, and so on.
The WEB-INF directory is a dedicated area where the container cannot provide content to users. files in this directory are only used by containers and contain resources that should not be directly downloaded by customers. For example: servlet (these components include application logic and possible access to other resources, such as databases), any other files that the servlet can directly access in Web applications, resources running or used on the server (such as Java class files and jar files for servlet), temporary files generated by your application, implementation descriptors, and any other configuration files. these resources are dedicated, so they can only be accessed by their own web applications and containers. in particular, JSP/servlet program files can also access files under this directory through servletcontext. For example, JSP files can be accessed through application. getrealpath ("/WEB-INF/web. XML ") access the path to the implementation descriptor file. web containers require that a directory in your application must be written by the WEB-INF. if your web application does not contain this directory, it will not work. the WEB-INF contains the implementation descriptor, a classes directory, a lib directory, and other content. the classes directory is used to store compiled Servlet and other program classes, such as JavaBean. if a program has a packaged JAR file (for example, a third-party API is packaged into a jar file, such as the Struts framework class library struts. jar, MySQL database JDBC driver file mm. mySQL. jar), so they can be copied to the lib directory (if you decompress these packages, copy them to the classes directory ). the Web Container uses these two directories to find the servlet and other related classes. That is to say, the class loader of the container will automatically view the classes directory and the jar files under the lib directory. this means you do not need to explicitly add these classes and jar files to classpath. the Web Container automatically adds the files in these two directories to the Web application class path.
Take the default installed root directory of Tomcat as an example. It contains the following files:

1 root web application root directory
│/Index. jsp default JSP welcome page
│/Jakarta-banner.gif resource file
│/Tomcat-power.gif resource file
│/Atat.gif resource file

└ ── 1web-inf web application implementation configuration information directory
/Web. XML Web application implementation Descriptor
What are the minimum file requirements for a Web application? The answer is to need a WEB-INF directory and web. xml. Under this directory, the minimum content of this web. XML is:

Public "-// Sun Microsystems, Inc. // DTD web application 2.3 // en"
Http://java.sun.com/dtd/web-app_2_3.dtd>

<Web-app>
Web-app>

Let's take a look at the content of web. XML in the Tomcat sample directory:

Public "-// Sun Microsystems, Inc. // DTD web application 2.3 // en"
Http://java.sun.com/dtd/web-app_2_3.dtd>

<Web-app>
<Display-Name> welcome to tomcatdisplay-Name>
<Description>
Welcome to Tomcat
Description>
Web-app>

The first line of the implementation descriptor is an XML declaration, indicating the XML version number and the encoding used:

These statements follow the document type declaration, specifying the URI to be used by the application's DTD:
Public "-// Sun Microsystems, Inc. // DTD web application 2.3 // en"
Http://java.sun.com/dtd/web-app_2_3.dtd>
In Web applications, DTD is part of J2EE technical specifications for web applications and the standard for all our web applications. Note that the version number here is 2.3. the web-app tag defines a web application. Only the display-Name and description information of the Web application are defined here ), if you use a visual web application implementation descriptor editor, you can see this information. other features, such as URL ing, Servlet definition, and error pages, are added and configured as sub-tags under this tag.

Finally, what are the purposes of these content in practice? Use tomcat to talk about this problem.
First, for the issue of class paths, the developed Java class files can be published to Web applications in two ways, can be copied directly to the WEB-INF/classes (note to keep the directory structure of the package unchanged), if there is no classes directory, you can create this directory. you can also package it into a jar file (the JAR file format is zip file. You can create a zip file containing class files through WinZip, WinRAR, and other programs, and then change its file extension. jar), and then put the JAR file under the WEB-INF/lib directory. note that modification to the file content in these two directories may take effect after the Web server is restarted, that is, tomcat. if the application uses a configuration file, such as the typical struts configuration file application. properties, can only be placed under the WEB-INF/classes directory.
Second, if you want to configure multiple web applications, you can create a minimum contain WEB-INF/web under the % atat_home %/webapps/directory. XML directory, assuming the directory name is webapp1, then its access URI is http: // hostname: Port/webapp1, you can add the WEB-INF/classes directory and WEB-INF/lib directory. you can create multiple such Web applications. For example, to run two struts applications, you can create two directories with the same content but different root directory names as two separate web applications. after creating a new web application, you may need to restart the Tomcat server.

For more information about Web applications, war files, Web implementation descriptors, and Web application tags, see <J2EE Programming Guide (1.3 or 1.4)> (The Chinese version is published by the Electronic Industry Publishing House, with 1143 pages and a price of 106.00 RMB. The English document is titled professional Java Server programming J2EE 1.3 edition and published by wrox Publishing House) and Chapter 2 (Web implementation, verification, and packaging). You can also learn about it through online resources.

Bytes ---------------------------------------------------------------------------------------------

The container is the environment where your JSP page, Servlet, and other resources are stored. Just like ASP's IIS. The main function is to create a request object for the HTTP request sent from the client. Then, based on the requested JSP or servlet, the result is sent to the client in the HTTP format using the reponse object. Ask your browser to explain the strings in HTML or other formats (or binary ).
Of course, this is only the most basic operation. There are many other operations, such as thread management ~~~

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.