Dynamic Website Development 02: Java Web Overview

Article directory

1. XML basics

(1) Overview of XML

Goal: Understand the concept of XML and be able to know what HTML is used for

1、XML

  • XML is the abbreviation of EXtensible Markup Language, which is a markup language similar to HTML, called extensible markup language. XML is used to provide a data description format, suitable for data exchange between different applications, and this exchange is not premised on a predefined set of data structures, which enhances scalability.
  • In real life, there are certain hierarchical relationships between many things. For example, China has many provinces, and each province has many cities. The hierarchical relationships between China and the provinces and cities under its jurisdiction can be represented by a tree structure. Figure description.

2. Comparison between XML and HTML

(1) HTML is used to display data, and XML is used to transmit and store data.
(2) HTML tags are not case-sensitive, while XML tags are strictly case-sensitive.
(3) HTML can have multiple root elements, while well-formed XML has and can only have one root element.
(4) In HTML, spaces are automatically filtered, but in XML, spaces are not automatically filtered.
(5) Tags in HTML are predefined tags, while tags in XML can be defined as needed and are extensible.

(2) XML syntax

  • Objective: Master the syntax of XML, including document declaration, element declaration, attribute definition and comments

1. Declaration of XML document

  • Starting from XML 1.1, a complete XML document must contain a declaration of the XML document, and the declaration must be located on the first line of the document.
    Syntax format of XML document declaration:<?xml version="version" encoding="value" standalone="value"?>
Attributes illustrate
version Used to specify the version number that follows the XML specification. The version attribute must be included in the XML declaration, and this attribute must be placed before other attributes in the XML declaration.
encoding Used to specify the encoding set used by XML documents.
standalone Used to specify whether the XML document is nested with an external document. The value is yes or no. If the attribute value is set to yes, it means that it is an independent XML document and has no connection with the external file; if the attribute value is set to no, it means that the XML document is not independent.

2. Definition of XML elements

  • The main content in XML documents is composed of elements. Elements are arranged in a tree-like hierarchical structure. One element can be nested within another element. There is only one top-level element in an XML document, which is called the document element or root element. Elements generally consist of a start tag, attributes, element content, and an end tag.
<售价 单位="">100</售价>

3. Definition of XML attributes

  • In an XML document, attributes can be defined for elements. Attributes are further descriptions and descriptions of elements. In an element, you can customize multiple attributes. The attributes are attached to the element, and each attribute has its own name and value.
<售价 单位="">100</售价>
  • It should be noted that in XML documents, the naming convention of attributes is the same as that of elements, and attribute values ​​must use double quotes(“”) or single quotes(‘’) is raised, otherwise it is considered an error.

4. Definition of XML comments

The comment is for ease of reading and understanding. If you want to insert some additional information into the XML document, such as the author's name, address or phone number, etc., this information is an explanation of the document structure or document content and does not belong to the XML document. content, so the XML parser will not process the comment content. Comments in XML documents end with the string “<!--”开始,以字符串“-->”.

<!--注释信息-->

5. XML file example

  • breakfast menu
<?xml version="1.0" encoding="UTF-8"?>
<breakfast>
 	<food>
		<name>Belgian Waffles</name>
		<price>$5.95</price>
		<description>two of our famous Belgian Waffles with plenty of real maple syrup</description>
		<calories>800</calories>
	</food>
	<food>
		<name>Strawberry Belgian Waffles</name>
		<price>$4.50</price>
		<description>light Belgian waffles covered with strawberries and whipped cream</description>
		<calories>300</calories>
	</food>
 	<food>
		<name>Berry-Berry Belgian Waffles</name>
		<price>$8.95</price>
		<description>light Belgian waffles covered with an assortment of fresh berries and whipped cream</description>
		<calories>900</calories>
	</food>
   <food>
		<name>French Toast</name>
		<price>$4.50</price>
		<description>thick slices made from our homemade sourdough bread</description>
		<calories>550</calories>
	</food>
 	<food>
		<name>Homestyle Breakfast</name>
		<price>$6.95</price>
		<description>two eggs, bacon or sausage, toast, and our ever-popular hash browns</description>
		<calories>950</calories>
	</food>
</breakfast>
  • Spring projectpom.xmlConfiguration file
<?xml version="1.0" encoding="UTF-8"?>

(3) DTD constraints

  • Goal: Master the introduction of DTD constraints and the syntax of DTD constraints

1. What are XML constraints?

Tags in XML documents can be defined at will. There are two selling prices for the same book. It is difficult to distinguish which is the original price and which is the membership price based only on the tag name. To this end, in the XML document, a set of rules is defined to constrain the content in the document. This set of rules is called XML constraints. When constraining XML documents, certain grammatical rules must also be followed, and these grammatical rules form the XML constraint language.

2. What are DTD constraints?

DTD constraint is an early XML constraint pattern language, and files created according to its syntax are called DTD files. In a DTD file, it can contain the definition of elements, the definition of relationships between elements, the definition of element attributes, and the definition of entities and symbols.

  • Briefly understand DTD constraints through a case and createbook.xmlfile
<?xml version="1.1" encoding="UTF-8"?>
<书架>
 	<>
 		<书名>徒然草</书名>
 		<作者>吉田兼好</作者>
 		<售价>34.00元</售价>
 	</>
 	<>
 		<书名>精通Spring框架</书名>
 		<作者>魏赫布</作者>
 		<售价>49.00元</售价>
 	</>
</书架>

3. Explanation of DTD constraint documents

  • (1)<!ELEMENT …> statement defines an element, where "bookshelf" is the name of the element, “(书+)” indicates that there are one or more bookshelf elements For an element named "book", the character "+" indicates that the element it modifies must appear one or more times.
  • (2) "Book" is the name of the element, “(书名,作者,售价)” means that the element book contains three sub-elements: book title, author, and selling price, and these sub-elements must appear in order.
  • (3) "Book title", "Author" and "Price Price" are all element names. “(#PCDATA)” means that the content nested in the element is an ordinary text string.

4. Introduction of DTD

There are two ways to introduce external DTD files into XML documents

第1种方式: <!DOCTYPE 根元素名称 SYSTEM  "外部DTD文件的URI">
第2种方式: <!DOCTYPE 根元素名称 PUBLIC "DTD名称" "外部DTD文件的URI">
  • The first method is used to reference a local DTD file; the second method is used to reference a public DTD file, where the "URI of the external DTD file" refers to the location where the DTD file is stored locally. For the first method, it It can be a relative path to the XML document or an absolute path. For the second method, it is an absolute URL address on the Internet.
(1) Introduce local DTD files
  • Modify the filebook.xml and introduce the local DTD file into the XML documentbook.dtd
<?xml version="1.1" encoding="UTF-8"?>
<!DOCTYPE 书架 SYSTEM "book.dtd">
<书架>
 	<>
 		<书名>徒然草</书名>
 		<作者>吉田兼好</作者>
 		<售价>34.00元</售价>
 	</>
 	<>
 		<书名>精通Spring框架</书名>
 		<作者>魏赫布</作者>
 		<售价>49.00元</售价>
 	</>
</书架>
(2) Introduce public DTD files
  • To introduce a public DTD file, you need to use the PUBLIC attribute in the DOCTYPE declaration statement
<!DOCTYPE web-app PUBLIC 
	"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
	 "http://java.sun.com/dtd/web-app_2_3.dtd">
  • where"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" is the DTD name, which is used to describe the standards that the DTD complies with, the name of the owner, and the file describing the DTD. Although the DTD name looks complicated, This is entirely a matter for the DTD file publisher to consider. The XML file writer only needs to copy the DTD identification name defined in advance by the DTD file publisher.
(3) Use embedded methods to implement DTD constraints on XML

DTD's constraints on XML documents can be implemented in an embedded manner in addition to external introduction.
Embed the complete syntax format of DTD definition statement directly in XML

<?xml version="1.1"  encoding="UTF-8"  standalone="yes"?>
<!DOCTYPE 根元素名 [
	   DTD定义语句
	   ……
]>
  • Modifybook.xml and embedfile directly inbook.xmldocumentbook.dtd
<?xml version="1.1" encoding="UTF-8"?>
<!DOCTYPE 书架 [
	<!ELEMENT 书架 (书+)>
	<!ELEMENT  (书名,作者,售价)>
	<!ELEMENT 书名 (#PCDATA)>
	<!ELEMENT 作者 (#PCDATA)>
	<!ELEMENT 售价 (#PCDATA)>
]>
<书架>
 	<>
 		<书名>徒然草</书名>
 		<作者>吉田兼好</作者>
 		<售价>34.00元</售价>
 	</>
 	<>
 		<书名>精通Spring框架</书名>
 		<作者>魏赫布</作者>
 		<售价>49.00元</售价>
 	</>
</书架>

5. Syntax of DTD constraints

  • When writing XML documents, you need to master XML syntax. In the same way, when writing a DTD document, you also need to follow the syntax of the DTD. The structure of a DTD generally consists of element type definition, attribute definition, entity definition, token definition, etc. A typical DTD document type definition will pre-define the element structure, attribute type, entity reference, etc. of the XML document to be created in the future.
(1) Element definition
  • Elements are the basic components of XML documents. In the DTD definition, each <!ELEMENT...> statement is used to define an element
  • Basic syntax format:<!ELEMENT 元素名称 元素内容>
  • In the definition syntax format of the above element, it includes "element name" and "element content". Among them, “元素名称” is an element in the constrained XML document, and “元素内容” is a declaration of the content contained in the element, which includes data type and symbol.
(2) Element content
element illustrate
#PCDATA Indicates that the content nested in the element is an ordinary text string, where the keyword PCDATA is the abbreviation of Parsed Character Data.
child element The description element contains other elements. Usually a pair of parentheses () are used to enclose a group of sub-elements to be nested within the element.
mixed content Presentation elements can contain both character data and child elements. Mixed content must be defined zero or more times.
EMPTY Indicates that the element contains neither character data nor sub-elements, and is an empty element. If the element itself has a clear meaning in the document, you can use the keyword EMPTY in the DTD to indicate an empty element.
ANY Indicates that the element can contain any character data and sub-elements.

(3) Symbols contained in element content

  • When defining an element, the element content can contain some symbols, and different symbols have different functions.
symbol effect
question mark[?] Indicates that the object can appear 0 or 1 times
Asterisk[*] Indicates that the object can appear 0 or more times
plus sign[+] Indicates that the object can appear once or multiple times
vertical line[ ]
comma[,] Indicates that objects must appear in the specified order
brackets[()] Used to group elements

(4) Attribute definition

  • In a DTD document, while defining an element, you can also define attributes for the element.
    Basic syntax format of DTD attribute definition
<!ATTLIST 元素名
      属性名1 属性类型 设置说明
      属性名2 属性类型 设置说明
     ......
> 
  • In the syntax format of the above attribute definition, “元素名” is the name of the element to which the attribute belongs, “属性名” is the name of the attribute, “属性类型” is used to specify which type the attribute belongs to, “设置说明” is used to indicate whether the attribute must appear.

(5) Attribute setting instructions

Setup instructions meaning
#REQUIRED This attribute of the representation element is necessary. For example, when defining the DTD of contact information, we hope that each contact has a contact number attribute. In this case, you can use REQUIRED when declaring the attribute.
#IMPLIED Indicates that the element may or may not contain this attribute. For example, when defining the information of a book, it is found that the page number attribute of the book is insignificant to the reader. In this case, you can use IMPLIED when declaring the attribute.
#FIXED Represents a fixed default value of an attribute that cannot be set to other values ​​in the XML document. When using the #FIXED keyword, you also need to provide a default value for this property. When the attribute is not defined in the XML document, its value will be automatically set to the default value defined in the DTD.
default value Like FIXED, if the element does not contain this attribute, the attribute will automatically be set to the default value defined in the DTD. The difference is that the value of this attribute can be changed. If this attribute is set in the XML file, the new attribute value will override the default value defined in the DTD.

(6)Attribute type

  • CDATA is the most commonly used attribute type, indicating that the attribute type is character data, which is the same as #PCDATA in the element content description. Of course, special characters that appear in attribute setting values ​​also need to be represented by their escape character sequences. For example, use "&" to represent the character "&", use "<" to represent the character "<", etc.
    Enumerated (enumeration type), when declaring attributes, you can limit the value of the attribute to be selected from a list. This type of attribute belongs to Enumerated (enumeration type)
  • An attribute of type ID is used to uniquely identify an element in an XML document. Attribute values ​​of type ID must comply with the rules defined by XML names. An element can only have one ID type attribute, and the ID type attribute must be set to #IMPLIED or #REQUIRED. Because each value of an ID type attribute is used to identify a specific element, it makes no sense to provide default values, especially fixed default values, for ID type attributes.
  • In addition to the several attribute types described, there are also several attribute types in DTD constraints: IDREF, IDREFS, NMTOKEN, NMTOKENS, NOTATION, ENTITY and ENTITYS. Due to limited space, I will not list them all here.

(4) Schema constraints

  • Goal: Master the namespace of Schema constraints, how to introduce Schema documents, and Schema syntax
1. What are Schema constraints?

Like DTD, XML Schema is also a pattern language used to define and describe the structure and content of XML documents. Its emergence overcomes the limitations of DTD.

2. Advantages of Schema constraints

(1) DTD uses a non-XML syntax format and lacks a comprehensive description of the document structure, elements, data types, etc. XML Schema uses XML syntax format, and it is also an XML document itself. Therefore, XML Schema syntax format is better to understand than DTD.
(2) XML has very high legality requirements. The description of XML by XML DTD is often used as a basis for verifying the legality of XML. However, the legality of XML DTD itself is lacking. A better verification mechanism must be handled independently. XML Schema is different. It has the same legality verification mechanism as XML.
(3) XML Schema supports namespaces very well, while DTD barely supports namespaces.
(4) The data types supported by DTD are very limited.
(5) The ability of DTD to define constraints is very limited, and it is unable to make more detailed semantic restrictions on XML instance documents.

3. Documentation of Schema constraints
  • XML Schema is much more powerful than DTD, but the corresponding syntax is also much more complicated than DTD.
    Look at a simple Schema document
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
     <xs:element name="root"  type="xs:string"/>
</xs:schema>
  • Schema document, with xsd as the suffix name and xs:schema as the root element, represents the beginning of the schema definition.
4. Name space
  • An XML document can introduce multiple constraint documents. However, since the elements or attributes in the constraint documents are all customized, it is very likely that elements or attributes with the same name representing different meanings will appear in the XML document, resulting in name conflicts. For this purpose, in XML documents, a namespace is provided, which can uniquely identify an element or attribute. This is like taking a taxi to go to Xiaoying. Since there are two places in Beijing called Xiaoying, in order to avoid the driver taking the wrong route, we will say "Go to the Xiaoying in the Asian Games Village" or "Go to the Xiaoying in Qinghe". At this time, the Asian Games Village or Qinghe is equivalent to a name space.
5. Format of declaring namespace
  • The declaration of a namespace is to specify a temporary name for the namespace of a certain schema document in an XML document. It is declared through a series of reserved attributes. The name of such attributes must be "xmlns" or "xmlns:". prefix. Like any other XML attribute, it can be given directly or by default.
  • Syntax format of namespace declaration: <元素名 xmlns:prefixname="URI">
  • Note: The element name refers to the element on which the namespace is declared. The namespace declared on this element applies to the element and attributes where it is declared, as well as all elements and their attributes nested in the element. xmlns:prefixname refers to the attribute name of the element, and its corresponding value is a URI reference used to identify the name of the namespace. It should be noted that if there are two URIs and the characters they consist of are exactly the same, they can be considered to identify the same namespace.
  • Createbook2.xml and learn the use of namespaces inbook2.xmldocuments.
<?xml version="1.1" encoding="UTF-8"?>
<lzy:书架 xmlns:lzy="http://www.lzy.org/xmlbook/schema">
 	<lzy:>
 		<lzy:书名>徒然草</lzy:书名>
 		<lzy:作者>吉田兼好</lzy:作者>
 		<lzy:售价>34.00元</lzy:售价>
 	</lzy:>
 	<lzy:>
 		<lzy:书名>精通Spring框架</lzy:书名>
 		<lzy:作者>魏赫布</lzy:作者>
 		<lzy:售价>49.00元</lzy:售价>
 	</lzy:>
</lzy:书架>

6. Introduce Schema document

Introduction method illustrate
Importing XML Schema documents using namespaces When using a namespace to introduce an XML Schema document, you need to declare the namespace document through the attribute xsi:schemaLocation. The xsi:schemaLocation attribute is in the standard namespace "http://www.w3.org/2001/XMLSchema-instance" Defined, this attribute contains two URIs separated by whitespace. Among them, the first URI is the name of the namespace, and the second URI is the location of the document.
Directly specified through the xsi:noNamespaceSchemaLocation attribute Directly specified through the xsi:noNamespaceSchemaLocation attribute. The noNamespaceSchemaLocation attribute is also defined in the standard namespace "http://www.w3.org/2001/XMLSchema-instance", which is used to define the location of the specified document.

7. Schema syntax

(1) Element definition

Schema, like DTD, can define elements in XML documents.
In the Schema document, the syntax format of element definition:<xs:element name="名称" type="类型"/>

  • element is used to declare an element, the name refers to the name of the element, and the type refers to the data type of the element.
  • There are many built-in data types in XML Schema. The most commonly used types are as follows:
type illustrate
xs:string Represents string type
xs:decimal Represents decimal type
xs:integer Represents an integer type
xs:boolean Represents Boolean type
xs:date Represents date type
xs:time Indicates time type
  • XML sample code using the element's definition
<lastname>Smith</lastname>
<age>28</age>
<dateborn>1980-03-27</dateborn>

The Schema definition corresponding to these three elements is as follows

<xs:element name="lastname" type="xs:string"/>
<xs:element name="age" type="xs:integer"/>
<xs:element name="dateborn" type="xs:date"/> 
(2) Attribute definition
  • In the Schema document, the syntax format of attribute definition:<xs:attribute name="xxx" type="yyy"/>
    xxx refers to the attribute name, and yyy refers to the data type of the attribute. Among them, the common data types of attributes are the same as those of elements, and they all use the data types created in XML Schema.
(3) Simple type
  • In XML Schema documents, elements containing only character data are of simple type. Simple types are defined using the xs:simpleType element. If you want to restrict the type of content of an existing element, you need to use the xs:restriction element. Next, we will introduce in detail how to limit the content of simple type elements through the following situations.
    Limitation of values ​​by xs:minInclusive and xs:maxInclusive elements
    For example, when we define the age of an employee, the employee’s age requirement is 18~58 years old At this time, the age element "age" needs to be limited. The specific sample code is as follows:
<xs:element name="age">
	<xs:simpleType>
    	<xs:restriction base="xs:integer">
        	<xs:minInclusive value="18"/>
	        <xs:maxInclusive value="58"/>
    	</xs:restriction>
	</xs:simpleType>
</xs:element> 
  • xs:enumeration element limits a set of values
  • If you want to limit the content of an XML element to a set of acceptable values, you can use an enumeration constraint. For example, to limit an element named Car, the only acceptable values ​​are: Audi, Golf, BMW, Specific examples are as follows:
<xs:element name="car">
	<xs:simpleType>
  		<xs:restriction base="xs:string">
		    <xs:enumeration value="Audi"/>
		    <xs:enumeration value="Golf"/>
		    <xs:enumeration value="BMW"/>
		</xs:restriction>
	</xs:simpleType>
</xs:element> 
  • xs:pattern element limits a series of values
  • If you want to limit the content of an XML element to a series of usable numbers or letters, you can use a pattern constraint. For example, to define a qualified element “letter”, the acceptable value can only be one of the letters a-z. The specific example is as follows:
<xs:element name="letter">
	<xs:simpleType>
  		<xs:restriction base="xs:string">
		    <xs:pattern value="[a-z]"/>
		</xs:restriction>
	</xs:simpleType>
</xs:element> 

xs:restrictionElement limits on whitespace characters

  • In XML documents, whitespace characters are special. If you need to process whitespace characters, you can use the whiteSpace element. The whiteSpace element has three attribute values ​​that can be set, namely preserve, replace and collapse. Among them, preserve means not to process any whitespace characters in the element, replace means to remove all whitespace characters, and collapse means to reduce all whitespace characters into a single character. Next, take preserve as an example to learn how to limit whitespace characters. The specific examples are as follows:
<xs:element name="address">
	<xs:simpleType>
  		<xs:restriction base="xs:string">
			<xs:whiteSpace value="preserve"/>
	  	</xs:restriction>
	</xs:simpleType>
</xs:element> 
(4) Complex type

When defining a complex type, you need to use the xs:complexContent element to define it. Elements of complex types can contain sub-elements and attributes, and such elements are called composite elements. When defining a compound element, if the element's start tag and end tag only contain character data content, then such content is simple content and needs to be defined using the xs:simpleContent element. On the contrary, the content of the element is complex content and needs to be defined using the xs:complexContent element. There are 4 basic types of composite elements. Next, these four basic types will be explained separately.

  • empty element
  • The empty element here refers to an element that does not contain content and only contains attributes. Specific examples are as follows:
<product prodid="2023001" />
  • In the above element definition, the content of the element "product" is not defined. At this time, the corresponding definition of the empty element in the XML Schema document is as follows:
<xs:element name="product">
  	<xs:complexType>
    	<xs:attribute name="prodid" type="xs:positiveInteger"/>
  	</xs:complexType>
</xs:element>
  • Elements that contain other elements
  • For elements in an XML document that contain other elements, such as the following sample code:
<person>
    <firstname>John</firstname>
    <lastname>Smith</lastname>
</person>
  • The elementperson nests two elements, namely “firstname” and “lastname”. At this time, in < /span>SchemaThe corresponding definition in the document is as follows:
<xs:element name="person">
	<xs:complexType>
    	<xs:sequence>
      		<xs:element name="firstname" type="xs:string"/>
		   	<xs:element name="lastname" type="xs:string"/>
	    </xs:sequence>
  	</xs:complexType>
</xs:element>
  • Elements containing only text
  • For text-only compound elements, you need to add content using the "simpleContent" element. When using simple content, extensions or restrictions must be defined within the "simpleContent" element. In this case, you need to use the "extension" or "restriction" element to extend or restrict the basic simple type of the element. Please look at a simple example of XML, where "shoesize" only contains text. The specific example is as follows:
<shoesize country="france">35</shoesize>
```xml
- 元素“shoesize”包含了属性以及元素内容,针对这种仅包含文本的元素,需要使用extension来对元素的类型进行扩展,在Schema文档中对应的定义方式如下所示:
```xml
<xs:element name="shoesize">
	<xs:complexType>
    	<xs:simpleContent>
      		<xs:extension base="xs:integer">
        		<xs:attribute name="country" type="xs:string" />
	      	</xs:extension>
    	</xs:simpleContent>
	</xs:complexType>
</xs:element>
  • Elements containing elements and text
    In XML documents, certain elements often need to contain text and other elements, for example, the following XML document:
<letter>
	Dear Mr.<name>John Smith</name>.
	Your order <orderid>1032</orderid>
	will be shipped on <shipdate>2001-07-13</shipdate>.
</letter>
  • The corresponding definition in the Schema document is as follows:
<xs:element name="letter">
	<xs:complexType mixed="true">
    	<xs:sequence>
      		<xs:element name="name" type="xs:string"/>
		    <xs:element name="orderid" type="xs:positiveInteger"/>
		    <xs:element name="shipdate" type="xs:date"/>
    	</xs:sequence>
	</xs:complexType>
</xs:element>
  • Note: In order to allow character data to appear between child elements of the "letter" element, the mixed attribute is used. This attribute is used to specify whether character data is allowed to appear between child elements of complex types. By default, the value of mixed is false.

2. Program development system architecture

Goal: Understand C/S architecture and B/S architecture

(1) C/S system architecture

C/S is the abbreviation of Client/Server, which is the client/server architecture. During the development process, the client needs to install corresponding software to connect to the server, and the client software is responsible for all logic and operations, while the server only provides an architecture for data interaction.

(2) B/S system architecture

B/S is the abbreviation of Browser/Server, that is, browser/server architecture. During the development process, the client only needs a browser to interact with the server. The server is responsible for all logic and calculations, and the browser is only responsible for displaying the results on the screen. The biggest advantage of the B/S architecture is that there is no need to install a special client program on the client. The business logic processing in the program is concentrated on the Web server. The client only needs to install a browser to interact with the database through the Web server, and The results of the interaction are displayed in the browser in the form of a web page.

3. Tomcat server

(1) First introduction to Tomcat

  • Goal: Understand the role of Tomcat

1. Tomcat concept

  • Tomcat is an important sub-project in the Jakarta project organized by Apache. It is a container (engine) recommended by Sun Corporation (acquired by Oracle) for running Servlets and JSPs. Its source code is completely open to the public. Tomcat not only has the basic functions of a web server, but also provides many common component functions such as database connection pools.

2. Tomcat features

Tomcat运行稳定、可靠、效率高,不仅可以和目前大部分主流的Web服务器(如Apache、IIS服务器)一起工作,还可以作为独立的Web服务器软件。

3、Tomcat官网

https://tomcat.apache.org/

4、下载Tomcat压缩包

https://tomcat.apache.org/download-80.cgi

(二)Tomcat安装和启动

1、解压缩Tomcat压缩包

可以将压缩包解压到任意位置,最后路径不要包含中文和空格,比如解压到D盘根目录,解压后会产生了一个apache-tomcat-8.5.86文件夹。

2、Tomcat的目录结构

  • 打开apache-tomcat-8.5.86文件夹,会看到里面有7个目录
  • Tomcat安装目录中包含一系列的子目录,这些子目录分别用于存放不同功能的文件。
    目录| 作用
    |–|–|
    bin |用于存放Tomcat的可执行文件和脚本文件(扩展名为bat的文件)
    conf| 用于存放Tomcat的各种配置文件,如web.xml、server.xml
    lib| 用于存放Tomcat服务器和所有Web应用程序需要访问的JAR文件
    logs| 用于存放Tomcat的日志文件
    temp |用于存放Tomcat运行时产生的临时文件
    webapps |Web应用程序的主要发布目录,通常将要发布的应用程序放到这个目录下
    work| Tomcat的工作目录,JSP编译生成的Servlet源文件和字节码文件放到这个目录下

3、配置Tomcat环境变量

  • 新建CATALINA_HOME环境变量,然后修改Path变量,添加%CATALINA_HOME%\bin;

4、Tomcat服务器的启动

  • 在Tomcat安装目录的bin目录下存放了许多脚本文件,其中,startup.bat就是启动Tomcat的脚本文件。
  • 双击bin目标中的startup.bat文件,便会启动Tomcat服务器,此时,可以在弹出的命令行看到一些启动信息。(由于给Tomcat配置了环境变量,可以在命令行窗口里执行startup.bat命令,以此启动Tomcat服务器)
  • 看到末行org.apache.catalina.startup.Catalina.start Server startup in 1167 ms信息,表明Tomcat服务器启动成功
  • Tomcat服务器启动后,在浏览器的地址栏中输入http://localhost:8080或者http://127.0.0.1:8080(localhost和127.0.0.1都表示本地计算机)访问Tomcat服务器。

5、访问服务器上的资源

  • 打开Tomcat服务器的webapps目录
    在这里插入图片描述

  • 进入ROOT目录,有Tomcat服务器的首页 - index.jsp
    在这里插入图片描述

  • 通过浏览器来访问主页
    在这里插入图片描述

  • 访问ROOT目录里的静态资源(图片、文本、音视频)
    在这里插入图片描述

  • Root里创建一个网页 - welcome.html
    在这里插入图片描述

  • 在浏览器里访问http://localhost:8080/welcome.txthttp://127.0.0.1:8080/welcome.html
    在这里插入图片描述

  • 也可以用本机的IP地址
    在这里插入图片描述

  • 访问http://10.0.1.30:8080/welcome.html
    在这里插入图片描述

  • webapps里创建应用目录shop,在该目录里创建文本文件 - whipser.txt
    在这里插入图片描述

  • 浏览器里访问 - http://localhost:8080/shop/whisper.txt
    在这里插入图片描述

6、Tomcat服务器的关闭

  • 在Tomcat安装目录的bin目录下存放了许多脚本文件,其中,shutdown.bat就是关闭Tomcat的脚本文件。
    在这里插入图片描述

关闭Tomcat服务器之后,再访问http://localhost:8080/,就会报错
在这里插入图片描述

7、解决Tomcat启动窗口里的中文乱码问题

  • 大家可以看到启动Tomcat服务器之后弹出的窗口里出现了中文乱码

  • 修改conf目录里的logging.properties文件
    在这里插入图片描述

  • 将涉及编码的五条语句全部注释掉

  • 重启Tomcat服务器,查看启动窗口里的信息,看还有没有中文乱码

(三)Tomcat诊断

  • 目标:掌握Tomcat诊断,如何解决Tomcat启动后命令行窗口一闪而过的错误

1、启动后命令窗口一闪而过

  • 双击bin目录中的startup.bat脚本文件时,命令行窗口一闪而过。在这种情况下,由于无法查看到错误信息,所以无法对Tomcat进行诊断,分析出错原因。这时,可以先启动一个命令行窗口,在这个命令行窗口中,将目录切换到Tomcat安装目录中的bin目录,然后在该窗口中执行startup.bat命令,就会看到错误信息。
    在这里插入图片描述

  • 看到错误提示为“JRE_HOME环境变量配置不正确,运行该程序需要此环境变量”。这是因为Tomcat服务器是由Java语言开发的,它在运行时需要根据JAVA_HOMEJRE_HOME环境变量来获得JRE的安装位置,从而利用Java虚拟机来运行Tomcat。要解决这个问题,只需要将JAVA_HOME环境变量配置成JDK的安装目录。
    需要注意的是,配置完JAVA_HOME后,可将原来配置在Path环境变量中的JDK安装路径替换为“%JAVA_HOME%\bin;”,其中%JAVA_HOME%代表环境变量JAVA_HOME的当前值。路径末尾用英文半角分号(;)结束,与其他Path变量值路径隔开。这样做的好处是,当JDK的版本或安装路径发生变化时,只需修改JAVA_HOME的变量值,而Path环境变量和其他引用“JAVA_HOME”的位置不需要改变。
    在这里插入图片描述

  • 此时再启动Tomcat服务器,就没有一闪而过的错误了,当然也可以在命令行窗口里通过执行startup.bat命令来启动Tomcat服务器。

2、Tomcat端口号被占用

  • Tomcat在启动时可能会出现启动失败的情况,这种情况还可能是因为Tomcat服务器所使用的网络监听端口被其他服务程序占用所导致。现在很多安全工具都提供查看网络监听端口的功能,如360安全卫士、QQ管家等。此外,也可以通过在命令行窗口中输入“netstat -na”命令,查看本机运行的程序都占用了哪些端口,如果有程序占用了8080端口,则可以在任务管理器的“进程”选项卡中结束它的进程,之后重新启动Tomcat服务器,在浏览器中输入http://localhost:8080就能看到Tomcat的首页。
    在这里插入图片描述

  • 如果在“进程”选项卡中无法结束占用8080端口的程序,就需要在Tomcatserver.xml的配置文件中修改Tomcat监听的端口号。使用记事本打开server.xml文件,在server.xml文件中有一个<Connector>元素,该元素中有一个port属性,这个属性就是用于配置Tomcat服务器监听的端口号。Tomcat监听的端口号可以是0~65535之间的任意一个整数,如果出现端口号被占用的情况,就可以修改这个port属性的值来修改端口号。
    在这里插入图片描述

  • 如果将Tomcat服务器的端口号修改为80,那么在浏览器地址栏中输入http://localhost:80访问Tomcat服务器,此时会发现80端口号自动消失了,这是因为HTTP规定Web服务器使用的默认端口为80,访问监听80端口的Web应用时,端口号可以省略不写,即输入http://localhost就可访问Tomcat服务器。

  • 启动Tomcat服务
    在这里插入图片描述- 访问http://localhost:8888

(四)动手实践:创建Web应用

  • 目标:掌握如何在IDEA中配置Tomcat并创建Web应用

1、方法一:将Java项目添加Web功能

  • 创建Java项目 - WebDemo01
    在这里插入图片描述

  • 设置项目名称与保存位置
    在这里插入图片描述

  • 单击【Finish】按钮
    在这里插入图片描述

  • 在项目结构窗口里给项目添加Web功能,切换到Modules
    在这里插入图片描述

  • 单击【+】按钮,添加Web功能
    在这里插入图片描述

  • 单击【Create Artifact】按钮,修改名称 - WebDemo01
    在这里插入图片描述

  • 配置tomcat服务器,首先单击工具栏上的【Add Configuration…】按钮
    在这里插入图片描述

  • 添加Tomcat的local服务器
    在这里插入图片描述

  • 单击【Configure…】按钮,配置Tomcat服务器
    在这里插入图片描述
    在这里插入图片描述

  • 配置其它信息

在这里插入图片描述

  • 单击【Fix】按钮,将Web项目部署到我们配置的Tomcat服务器上
    在这里插入图片描述
  • 切换到【Server】选项卡
    在这里插入图片描述
  • 单击【OK】按钮
    在这里插入图片描述
  • 在web目录里创建首页 - index.html
    在这里插入图片描述
  • 启动tomcat服务器,查看结果
    在这里插入图片描述
  • 问题:地址栏没有写index.html,为什么可以访问到首页?
    在这里插入图片描述
  • 当然写上index.html,也是一样的效果
    在这里插入图片描述

2、方法二:直接创建Java Web项目

  • 新建Java Enterprise项目,选中Web Application (4.0)
    在这里插入图片描述
  • 设置项目名称与保存位置
    在这里插入图片描述
  • 单击【Finish】按钮
    在这里插入图片描述
  • 打开项目结构窗口,修改项目的Artifact名称
    在这里插入图片描述
  • 设置tomcat服务器
    在这里插入图片描述
  • 切换到【Deployment】选项卡,重新部署WebDemo02项目
    在这里插入图片描述
  • 切换到【Server】选项卡,设置tomcat服务器:默认的浏览器、项目访问的URL、更新动作
    在这里插入图片描述
  • 启动tomcat服务器
    在这里插入图片描述
  • 修改index.jsp文件,设置网页标题,添加一个一级标题显示欢迎信息和一个三级标题显示当前日期时间
    在这里插入图片描述
  • 重启Tomcat服务器,查看程序运行结果
    在这里插入图片描述
  • 刷新页面,你会发觉时间变了
    在这里插入图片描述
  • 当然写上index.jsp,也是一样的效果
    在这里插入图片描述

Guess you like

Origin blog.csdn.net/m0_63887380/article/details/130420906