One of javaWeb: introduction to javaWeb, configuration and use of Tomcat and maven

Posted by DusterG20 on Tue, 08 Feb 2022 22:17:07 +0100

One of javaWeb: introduction to javaWeb, configuration and use of Tomcat and maven

Start with this article to explain the learning of Java Web 😊, A series of learning articles are advancing steadily.

1, HTTP

1. Introduction

HTTP Hypertext Transfer Protocol (HTTP) is a simple request response protocol, which usually runs in TCP above. It specifies what messages the client may send to the server and what response it will get. Headers of request and response messages ASCII Given in form;

HTTPS (full name: Hyper Text Transfer Protocol over SecureSocket Layer) is an HTTP channel with security as the goal. It is encrypted and encrypted through transmission on the basis of HTTP identity authentication It ensures the security of the transmission process [1]. Join the foundation of HTTP S SSL , the security foundation of HTTPS is SSL, so the details of encryption need SSL.

2. Relevant knowledge

2.1 HTTP message:

HTTP request message: when users use the browser, such as accessing URL address, clicking web page hyperlink or submitting form, the browser will send request data to the server, which is a request message.

HTTP response message: after receiving the request data, the server will send the processed data back to the client. The data is a response message

2.2 HTTP request message

A complete request message consists of three parts: request line, request header and entity content.

2.2.1 HTTP request line

Request line: located in the first line of the request message, it contains three parts: request method, resource path and HTTP version

  • The request methods include: GET, POST, HEAD, PUT, DELETE, TRACE, CONNECT and OPTIONS
GET /index.html HTTP/1.1
2.2.2 HTTP request header

Request header: the request line is followed by the request header, such as the data type, compression method, language that the client can receive, and the URL address of the hyperlink that sends the request. When the browser sends a request to the server, the message header of the sent request message is also different according to different functional requirements.

  • Common request headers include: Accept, Accept encoding, Host, if modified since, Referer, and user agent
Accept:*/*
Accept-Encoding:gzip,compress
Host:www.baidu.com
2.3 HTTP response message

A complete response message mainly consists of three parts: response status line, response message header and entity content.

2.3.1 response status line

The response status line is located in the first line of the response message. It contains three parts: HTTP version, integer code indicating success (status code), and description of the status code

  • Common status codes: 200 (success), 302, 304, 404 (not found), 500 (server error)
HTTP/1.1 200 OK
2.3.2 response message header

The response status line is followed by the response header

  • Common response headers: Location, Server, Refresh, content disposition
Server:Apache-Coyote/1.1
Content-Encoding:gzip

2, Web application directory (website structure) and tomcat

1. Web application directory (website structure)

Referring to the ROOT directory structure under webapps under tomcat, the directory structure of a Web application should include:

  • Webapps
    • HTML files, JSP files, CSS files (display of web pages)
    • WEB-INF directory
      • web.xml (network configuration file), taglib tld
      • class directory (java program includes Servlet and JAvaBean)
      • lib directory (various jar files required by Web applications)

2,tomcat:

tomcat has the basic functions of Web server and provides general component functions such as database connection pool.

Configure tomcat in IDEA

Find Add at the top and click enter

After selecting Local, enter the configuration page:

Click + to fix the warning

3, Maven project architecture management tool

1. Maven introduction and configuration

Function: in Java Web development, a large number of rack packages need to be used. Maven can automatically import and configure jar packages.

Maven's core idea: the Convention is greater than the configuration, and write java code according to the specification

Modify the configuration file settings XML: set image:

<mirrors>
      <mirror>
         <id>nexus-aliyun</id>
         <mirrorOf>*</mirrorOf>
         <name>Nexus aliyun</name>
         <url>http://maven.aliyun.com/nexus/content/groups/public</url>
      </mirror> 
</mirrors>

Establish a local warehouse: create a folder C: \ program files \ apache-maven-3.8.1 \ Maven repo, and modify the configuration file settings xml

<localRepository>C:\Program Files\apache-maven-3.8.1\maven-repo</localRepository>

2. Using Maven a in IDE

1. Create a maven web project in IDEA:
  • Create Maven Project > check create from grchetype > check Maven archetype webapp > next

  • Fill in Groupld, Artifactid > next

  • Fill in Maven home directory: select maven path

  • Wait for the warehouse to download

2. Set Maven in IDEA

3. Create an ordinary Maven project: uncheck the module Create from grchetype

4. Select the folder and mark it

5. Start Tomcat

6. Maven sidebar use

maven plugin

maven project dependency

7. pom file

pom.xml is Maven's core configuration file

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>Maven-02-learn</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>
8. Maven resource export problem

In POM Add the following code to XML

<build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>
9. Use of Maven warehouse

Address: https://mvnrepository.com/

Find the dependent code through Maven warehouse and write it to POM XML, refresh Maven to download.

After the introductory knowledge and preparation are explained, you can officially learn about Servlet. See the next chapter for details:
Java Web II: the use of servlets


If it's helpful to you, just press one button three times 🤭

Topics: Java Maven Tomcat