Crazy God says JavaWeb notes 01

Posted by bhavin12300 on Wed, 16 Feb 2022 01:18:16 +0100

JavaWeb (crazy God says Java)

Java Web

1. Basic concepts

1.1 Preface

web development:

  • Web is the meaning of web page development, which means that we can get resources from the web page www.baidu.com com

  • Static web

    • html css

    • The data provided to everyone will never change

  • Dynamic web

    • Taobao, almost all websites are dynamic web

    • The data provided to everyone will change. Everyone will see different information at different times and places

    • Technology stack: Servlet/JSP, ASP,PHP

      In Java, the technology of dynamic web resource development is called JavaWeb

1.2 web application

web application: a program that provides browser access

  • Many web resources, such as a.html and b.html, can be accessed by the outside world and provide services
  • Any page or resource we can access exists on a computer in a corner of the world
  • url: uniform resource locator? Look back
  • This unified web resource will be placed under the same folder, but we can't access these web applications directly. We need to use Tomcat server
  • A web application consists of several parts (static web, dynamic web)
    • html css js
    • jsp servlet
    • java program
    • jar package
    • Configuration files (Properties)

After the web application is written, if you want to provide access to the outside world, you need a server to manage it uniformly

1.3 static web

  • .html,.htm these are suffixes of web pages. If these things always exist on the server, we can read them directly

  • Disadvantages of static web

    • The web page cannot be updated dynamically. Everyone sees the same page

      • Rotation map, click special effects: made a pseudo dynamic effect
      • We can use JavaScript to make dynamic effects
    • It cannot interact with the database, data cannot be persisted, and users cannot interact

1.4 dynamic web

The page will be displayed dynamically, and its display effect varies from person to person

Disadvantages:

  • If there is an error in the dynamic web resources added to the server, we need to rewrite our background program and publish it again
  • This means shutdown and maintenance

advantage

  • web pages can be updated dynamically

  • It can interact with the database (data persistence, such as registration information)

[external link image transfer fails, and the source station may have anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-0zkoz899-164496614664) (C: \ users \ 17614 \ appdata \ roaming \ typora \ typora user images \ image-20220215222108427. PNG)]

2.web server

2.1 technical explanation

JSP/Servlet:

B/S: browser, server

C/S client and server

  • b/s architecture mainly promoted by sun company
  • Based on Java language (all large companies or some open source components are written in Java)
  • It can bear the impact of three high problems: high performance, high concurrency and high availability

2.2 web server

Receive the user request and return the response to the user

Server is a passive operation, which is used to process some requests of users and give users some corresponding information

Tomcat

Tomcat actually runs JSP pages and servlets

After working for 3-5 years, you can try to write Tomcat server

Download Tomcat:

1. Install or unzip

2. Understand the configuration file and directory structure

3. Know the function of this thing

3.Tomcat

3.1 installing Tomcat

1.Tomcat official website https://tomcat.apache.org/

3.2 startup and configuration

Folder function

bin directory: used to put startup and shutdown script files

conf: Configuration

lib: put the dependent jar package

Logs: send and store the logs of the website

A folder represents a web application?

Startup and shutdown

Click the bin directory - startup Bat - double click to start

Note: startup Do not close bat after it is opened

Then enter localhost:8080 on the website

In this way, you can enter the tomcat website

Access test http://localhost:8080

Possible problems:

1. The Java environment is not configured because Tomcaat is written in Java

2. Flash back problem: compatibility needs to be configured

3. Garbled code problem: it needs to be set in the configuration file

Click conf

3.3 configuration

  • The default port number of Tomcat is 8080
  • mysql:3306
  • http:80
  • https:443
<Connector port="8081" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />

You can configure the name of the host

  • The default host name is localhost – > 127.0.0.1
  • Location of default website application: webapps
<Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">

3.4 difficult interview questions

Please talk about how the website is accessed

1. Enter a domain name: then press enter

2. Check whether there is a mapping of this domain name under the configuration file C:\Windows\System32\drivers\etc\hosts of this machine

	1.Yes: directly return the corresponding ip Address. In this address, we need to visit web program

‚Äč 2. No: go to the DNS server (DNS is where all domain names are managed)

Return if found, return if not found

understand:

The client initiates a request, assuming that the request sent is: www.baidu.com COM, this request is not received directly

The client has a hosts configuration file in C:\Windows\System32\drivers\etc

The client will first go to hosts to see if there is a configuration file for this domain name

3.5 publish a web site

If not, imitate first

  • Put the website written by yourself under the web apps folder specified in the server (Tomcat),

You can access the structure of the website

---webapps :Tomcat Server web catalogue
    -ROOT
    -kuangstudy  :Directory name of the web site
        -WEB -INF
        	-classes  : Java program
        	-lib:web Application dependent jar package
            -web.xml:Site profile
        -index.html:Default home page of the website
        -static  :Static resource file
            -css
            	-style
            -js
            -img
		...

4.Http

4.1 what is HTTP

HTTP (Hypertext Transfer Protocol) is a simple request response protocol, which usually runs on top of TCP

  • Text: html, string
  • Picture, sound, video, location, map
  • Default port number: 80

HTTPS: Secure

Default port: 443

4.2 two times

  • http1.0
    • HTTP/1.0: after the client can connect with the web server, it can only obtain one web resource
    • When we request a second time, we can't get resources because we're disconnected
  • http2.0
    • HTTP/1.1: after the client can connect with the web server, it can obtain multiple web resources

4.3 HTTP request

  • Client – send request – server

    We enter Baidu address, click enter and view the request process on the web page

Request URL:https://www.baidu.com / request address
Request Method:GET   Request method
Status Code:200 OK     Status code
Remote(long-range) Address:14.215.177.39:443   
Reffer Policy:no-referrer-when-downgrade
Accept:text/html
Accept-Encoding:gzip,deflate,br
Accept-Language:zh-CN,zh;q=0.9  language
Cache-Control:max-age=0
Connection:keep-alive    

1. Request line

  • Request method in request line: GET
  • Request method: * * get * *, post, head, delete, put, trace
    • get: the request method can carry fewer parameters and has limited size. It will display data information in the URL address bar of the browser. It is not safe, but it is efficient
    • post: there is no limit on the parameters that can be carried by the request method, and there is no limit on the size. The data information will not be displayed at the URL address of the browser, but it is safe

2. Message header

Accept:  Tell the browser what data types it supports
Accept-Encoding:Which encoding method is supported
Accept-Language:  Tell the browser its locale
Cache-Control:   Cache control
Connection:		Tell the browser whether to disconnect or remain connected when the request is completed
HOST: host

4.4 HTTP response

  • Server - response - client

Baidu:

Cache-Control:private  Cache control
Connection:Keep-Alive	connect
Content-Encoding:gzip   code
Content-Type:text/html  type

1. Responder

Accept:  Tell the browser what data types it supports
Accept-Encoding:Which encoding method is supported
Accept-Language:  Tell the browser its locale
Cache-Control:   Cache control
Connection:		Tell the browser whether to disconnect or remain connected when the request is completed
HOST: host
    
Refresh:   Tell the client how often to refresh
Location:  Repositioning web pages
    
    

2. Response status code (key)

200: request response succeeded

3 * *: request redirection

  • Redirect: you go back to the new location I assigned you

4 * *: resource 404 Not Found

  • Resource does not exist

5 * *: server code error 500

502 call gateway error

Common interview questions (during Java Web Learning)

When you enter the address in the address bar of your browser and press enter, what happens when the page can be displayed back?

5.Maven

Why learn this technology

1. In Java Web development, you need to use a large number of jar packages and import them manually

2. How can we make something automatically import and configure this jar package for us

Thus Maven was born

Maven is foreign

5.1Maven project architecture management tool

The jar package we are using to import

Maven's core idea: Convention is greater than configuration

  • There are constraints. Don't break them

Maven will specify how to write our java code. We must follow this requirement, mainly to standardize which directory our code should be written in

5.2 download and install Maven

Official website https://maven.apache.org

After downloading, unzip it

5.3 configuring environment variables

In our system environment variables

Configure the following variables:

  • M2_ bin directory under home Maven directory
  • MAVEN_ Home: Maven directory
  • Configure Maven in the path of the system_ HOME %MAVEN_ HOME%\bin
  • Test whether maven is installed

5.4 Alibaba cloud image

  • Mirroring: mirrors
    • Function: speed up our download
  • Alibaba cloud image is recommended in China
 <mirrors>
    <mirror>
    <id>aliyunmaven</id>
    <mirrorOf>central</mirrorOf>
    <name>aliyun maven</name>
    <url>https://maven.aliyun.com/repository/public </url>
    </mirror>
  </mirrors>

5.5 local warehouse

Local warehouse, original warehouse

Set up a local repository: localRepository

<localRepository>
    D:\Program Files\apache-maven-3.8.4-bin\apache-maven-3.8.4\maven-repo
</localRepository>

5.6 maven operation in IDEA

Create maven project (created through template)

1. Create Maven web project


2. Wait for the initialization of the project

BUILD SUCCESS indicates success

3. Observe what's more about maven project

4. maven setting in idea

4. maven setting in idea

After the idea project is successfully created, take a look at the maven configuration

Click menu bar view - > tool windows - > Maven projects

Create a normal maven project


This can only be found in Web applications

5.7 marking folder function


5.8 configuring Tomcat


Resolve warning issues

Why is there a warning problem

Must be configured: why is this problem: when we visit a website, we need to specify the name of a folder


Then start Tomcat

5.9 pom documents

pom.xml is the core configuration file of maven

<?xml version="1.0" encoding="UTF-8"?>


<!--Maven Version and header file -->

<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>

  <!--Here is what we just configured GAV-->

  <groupId>com.kuang</groupId>
  <artifactId>javaweb-01-maven</artifactId>
  <version>1.0-SNAPSHOT</version>

  <!--java Packaging method of

  jar:java application
  war:JavaWeb application
  -->

  <packaging>war</packaging>



  <name>javaweb-01-maven Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>


  <!--configuration file-->
  <properties>

    <!--Default code of the project-->

    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <!--Compiled version-->
    <maven.compiler.source>1.8</maven.compiler.source>
    <!--configuration file-->
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <!--Project dependency-->
  <dependencies>
    <!--Statically dependent jar Package configuration file-->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
     
    </dependency>
  </dependencies>

  <!--Things for project construction-->
  <build>
    <finalName>javaweb-01-maven</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

maven's high-level feature is that it will help you import other jar packages needed for this jar package

maven's convention is greater than configuration. It specifies which package our java code should be written under. If we don't do so, we can't even build Java

maven's agreement is greater than the configuration. We may encounter the problem that the configuration file we wrote cannot be exported or take effect later. Solution:

<!--stay build Medium configuration resources: To prevent our resource export from failing--> 
<build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>

5.10 idea operation

Topics: Java Front-end server