【Java WEB】Nginx&Tomcat&HTTP协议

Personal homepage: Hello Code.
This article column: "Java WEB from entry to actual combat"
Please click to go to Java WEB from entry to actual combat for the complete content of Java WEB. If you
have any questions, please correct me and learn together~~



Nginx

  • Nginx is a server software
  • Its core function is that it can be combined with server hardware, so that the program can be published to the Nginx server, allowing more users to browse

Install

  1. Upload compressed package: put nginx压缩包位置(press alt+p in CRT to enter sftp)

  2. Unzip the compressed package:tar -zxvf 压缩包名

  3. Enter the decompression directory:cd nginx解压目录

  4. Install dependencies

    yum -y install pcre pcre-devel

    yum -y install zlib zlib-devel

    yum -y install openssl openssl-devel
    yum -y install gcc

  5. Install nginx
    ./configure
    make
    make install(there will be an nginx directory under /usr/local/ after installation)

  6. Enter the corresponding directory:cd /usr/local/nginx/sbin

  7. Start nginx service: ./nginx
    stop: ./nginx -s stop
    restart:./nginx -s reload

  8. Check the nginx service status:ps -ef | grep nginx

  9. Test the nginx service: the browser opens the corresponding Linux server ip address

Finally, open the corresponding ip address in the browser and cannot access it. The solution:
the first step, configure the firewall for port 80: the firewall-cmd --zone=public --add-port=80/tcp --permanent
second step, restart the firewall service: systemctl restart firewalld.service
then re-visit your ip in the browser, it should be able to access .

Publish the project

  1. exist/homeCreate a web directory under:mkdir web

  2. Upload the project to this directory:put web项目压缩包

  3. Unzip the project archive:unzip web程序压缩包

  4. Edit the nginx configuration file: vim /home/nginx-1.18.0/conf/nginx.conf
    find the curly bracket range of the server and modify the path of the location
    insert image description here

  5. Enter the corresponding directory:cd /usr/local/nginx/sbin

  6. Shut down the nginx service:./nginx -s stop

  7. Start the nginx service and load the configuration file:/usr/local/nginx/sbin/nginx -c/home/nginx-1.18.0/conf/nginx.conf

  8. Test the website through your browser


Introduction to Enterprise Development

JavaEE Specification

  • JavaEE (Java Enterprise Edition): Java Enterprise Edition
  • It is an industry standard that is led by SUN and jointly developed by various manufacturers and is widely recognized.
  • JavaEE was called J2EE in the early days, but it did not continue to use its naming convention. The version of J2EE starts from 1.0 to the end of 1.4. The JavaEE version starts from the JavaEE 5 version, and the latest version is JavaEE 8.
  • The JavaEE specification is an umbrella term for many Java development techniques. These technical specifications are inherited from J2EE. A total of 13 technical specifications are included.
  • 包括:JDBC, JNDI, EJB, RMI, IDL/CORBA, JSP, Servlet, XML, JMS, JTA, JTS, JavaMail, JAF。

WEB overview

Overview

  • WEB in the computer field representsThe internet
  • Like the WWW we used before, it's an acronym for the three-word World Wide Web, called:World Wide Web
  • The emergence of network-related technologies is to allow us to live in a networked worldAccess to resources, where these resources are stored, we call itwebsite
  • By entering the address (URL) of the website, we can access the resources provided in the website (without distinguishing between local area network and wide area network)

Resource classification

  • Static resources
    The resources provided to people on the website are immutable, that is to say, different people or at different times, see the same content.

    As developers, the HTML, CSS, and JavaScript we write are all static resources.

  • Dynamic resources
    The resources displayed to people on the website are generated by programs, and different people see different content at different times or due to different identities.

    As a website developer, the JSPs and Servlets we write are all dynamic resources.

system structure

In the previous study, all Java projects were developed. These projects are called projects or products in the enterprise. It's all about the system!

Infrastructure division

  • CS structure: (Client Server)Client + Serverway (installing different applications directly on the client)
  • BS structure: (Browser Server)browser + serverThe way

Technology selection division

  • Model1 model
  • Model2 model
  • MVC model
  • Three-tier architecture + MVC model

Deployment method division

  • Integrated structure
  • vertical split structure
  • Distributed structure
  • Microservice Architecture

Tomcat

server

A server is a type of computer that runs faster, has a higher load, and is more expensive than a normal computer.

  • The server provides computing or application services for other clients (such as PCs, smart devices, etc.) in the network.
  • The server has high-speed CPU computing power, long-term reliable operation, powerful I/O external data throughput and better scalability.

The server we are talking about here is actually a web server, or an application server. It is essentially a piece of software that, by combining with hardware, can help us publish applications and allow users to access our applications through clients.

Common application server

server nickname illustrate
weblogic Implements the JavaEE specification, heavyweight server, also known as JavaEE container
websphereAS Implemented the JavaEE specification, heavyweight server
JBOSSAS Implements the JavaEE specification, heavyweight server, free
Tomcat It implements the jsp/servlet specification and is a lightweight server, open source and free

Tomcat

  • Tomcat is a core project in the Jakarta project group of the Apache Software Foundation, jointly developed by Apache, Sun and some other companies and individuals.

    Thanks to Sun's participation and support, the latest Servlet and JSP specifications can always be reflected in Tomcat.

  • Because of its advanced technology, stable performance, and free, Tomcat is deeply loved by Java enthusiasts and has been recognized by some software developers. It has become a popular Web application server.
  • Tomcat official website: https://tomcat.apache.org/
  • Support required for each version of Tomcat
    insert image description here

Download and install

  • Download: official website download
  • Installation: Just unzip it directly

directory composition

  • bin: some binary executables
  • conf: The path to save the configuration file
  • lib: jar package required by Tomcat during operation
  • logs: log file
  • temp: temporary file
  • webapps: The project publishing directory (a folder represents a web application) (ROOT represents the root project)
  • work: working directory

basic use

  1. Startup
    startup.bat: Start the executable file under Windows: Start the executable file
    startup.shunder Linux

    After startup, browser access: http://localhost:8080 to enter the welcome interface (Tomcat default port is 8080)

  2. Stop
    shutdown.bat: Close the executable file under Windows: Close the executable file
    shutdown.shunder Linux

  3. Deploy the project
    Create a folder in the webapps directory
    Put the resources in the folder
    Start tomcat and enter the correct path

common problem

  1. Startup problem
    The startup window flashes by: jdk environment variables are not configured
    insert image description here

    java.net.BindException: Port 8080 is occupied

  2. Console garbled problem solving
    conf-logging.properties
    modificationjava.util.logging.ConsoleHandler.encoding = UTF-8

    Tomcat defaults to UTF-8, and the CMD command window defaults to GBK. Changing UTF-8 to GBK can solve the problem of garbled characters

IDEA integrates Tomcat

  1. Click on Run -> Edit Configurations
  2. 点击Defaults -》 Tomcat Servlet -》 Local
  3. Click Configure - "Tomcat Home -" to select the path where tomcat is located

Installation under Linux

  1. Upload the compressed package to the /home path:put d:/apache-tomcat-9.0.58.tar.gz
  2. Unzip the compressed package:tar -zxvf 压缩包名
  3. Enter the bin directory:cd apache-tomcat-9.0.58/bin
  4. Start the tomcat service:./startup.sh
  5. Use browser test: browser open the corresponding Linux server ip address: 8080

Java WEB project

  1. Create a new project model and select Java Enterprise
    to determine the JDK version and Application Server version
  2. 右键Add Framework Support…
  3. Check the Web Application option

Detailed project composition

  • src: store Java source code
  • web: store project-related resources (html, css, js, jsp, pictures, etc.)
  • WEB-INFO: Stores related configurations (web.xml, etc.)

IDEA publishes the project

  1. Click on Run -> Edit Configurations
  2. Click Tomcat Server -> Deployment
    Application Context is the project access path, / represents the default path, there can only be one default path in multiple projects
  3. Click Tomcat Server -> Server
    settings associated browser
    Two Update resources settings
    Set JDK, port number
  4. Start the Tomcat service
  5. Validation results (browser)

Publish project via war package

  1. Open the war package in the web path of the project:jar -cvf myweb.war .
  2. Cut the prepared war package to the webapps path of tomcat
  3. Start the tomcat service and automatically decompress the war package
  4. Validation results

configuration file

Configure the default port number

main configuration fileserver.xml

<Connector>In the label, the port attribute represents the default port number of Tomcat (8080)
insert image description here

The default port number of the http protocol is 80, and the default port number of Tomcat is inconsistent with it, so
you need to add the port number every time you visit
the website. It is 80, so you don't need to add the port number when accessing the website

Configure virtual directory

Role: You can publish projects in any directory

  1. editserver.xmlconfig file, find the <Host>tag

  2. add the following
    <Context path="/mytest" docBase="e:/test" />
    insert image description here

path: The virtual directory name of the access resource (representing the access path in the browser address bar)
docBaserepresents the real path of the project that needs to be published

Configure virtual hosts

Role: You can specify the name of the access path

  1. editserver.xmlconfig file, find the <Engine>tag
  2. add the following
    insert image description here

name: Virtual host name
appBase: The root path where the project is saved
unpackWARs: Whether to automatically decompress the war package
autoDeploy: Whether to automatically publish
Context: the same as the virtual directory

  1. Modify the hosts file
    to bind the IP address and the corresponding domain name
    file path:c:\Windows\System32\drivers\etc
    insert image description here

HTTP protocol

Overview

  • HTTP(Hyper Text Transfer Protocol):Hypertext Transfer Protocol
  • HTTP protocol is based on TCP/IP protocol (secure)
  • Hypertext: more powerful than normal text (plus pictures, audio, etc.)
  • Transmission protocol: communication rules between client and server (handshake rules)

component

  • Request: The client initiates a request to the server
  • Response: The server sends a response to the client after receiving the client's request

In addition to manually initiated requests, JavaScript, CSS, images and other resources will automatically initiate requests

request for agreement

components of the request

  1. Request line: Request method submission path (submission parameters) HTTP/version number

  2. request header

    name illustrate
    Accept MIME types supported by client browsers
    Accept-Encoding The compression encoding format supported by the client browser. The most commonly used is gzip compression
    Accept-Language The languages ​​supported by the client browser. Usually zh_CN or en_US, etc.
    Referer Tell the server where the current request is coming from
    Content-Type MIME types supported by the request body
    Content-Length the length of the request body
    User-Agent Browser related information
    Connection The status of the connection. Keep-Alive stay connected
    If-Modified-Since The last modification time of the client browser cache file
    Cookie Session management related, very important
  3. Request blank line: normal newline, used to distinguish request header and request body

  4. Request body: Only the POST submission method has a request body, which is used to display the submission parameters

way of request

GET

insert image description here

POST

insert image description here

Only the POST request method has a request body, and the GET request method does not have a request body
. The parameters submitted by get are in the request line, and the parameters submitted by post are in the request body.

protocol response

Components of a Response

  1. Response Line: Request Mode HTTP/Version Number Status Code Status Description
    Common Status Codes

    status code illustrate
    200 everything is ok
    302/307 Request redirection, two requests, address bar changes
    304 The requested resource has not changed, use the cache
    404 Requested resource not found
    500 Server Error
  2. response header

    name illustrate
    Location The address to request redirection, often used in conjunction with 302 and 307
    Server Server related information
    Content-Type MIME type of the response body
    Content-Length the length of the response body
    Content-Disposition Tell the client browser to open the response body as a download
    Refresh Timed refresh
    Last-Modified The last modification time of the server resource
    Set-Cookie Session management related, very important
    Expires:-1 The cache time after the server resource is sent to the client browser
    Catch-Control:no-catch don't cache
  3. Response blank line: a normal newline, used to distinguish the response header from the response body

  4. Response body: Send the resource file to the client browser for parsing


Publish resource case

Publish static resources

  1. Create a Java WEB project

  2. Import the resources required by the static page into the web directory of the project

  3. Modify the web.xml configuration file, modify the default home page

    <!--在web.xml中加入-->
    <welcome-file-list>
        <welcome-file>/路径/文件名.html</welcome-file>
    </welcome-file-list>
    
  4. Deploy the project to tomcat

  5. Start the tomcat service

  6. Open the browser to view the page

Introduction to Servlet

  • Servlet is a program running on the Java server side to receive and respond to requests from clients based on the HTTP protocol.
  • If you want to achieve the function of Servlet, you can achievejavax.servlet.Servletinterface or an implementation class that inherits it.
  • Core method: service(), any client request will go through this method.

Publish dynamic assets

  1. Create a JavaWEB project

  2. Import the resources required by the static page into the web directory of the project

  3. Modify the web.xml configuration file, modify the default home page

  4. Write a class under the src path of the project to implement the Servlet interface

  5. Rewrite the service method and output a sentence

  6. Modify the web.xml configuration file to configure servlet-related resources

    <!--Servlet声明-->
    <servlet>
        <servlet-name>自定义名称</servlet-name>
        <servlet-class>java全类名(包名.类名)</servlet-class>
    </servlet>
    
    <!--Servlet映射-->
    <servlet-mapping>
        <servlet-name>和声明中的名称保持一致</servlet-name>
        <url-pattern>/访问路径(浏览器地址栏要输入的访问路径)</url-pattern>
    </servlet-mapping>
    
  7. Deploy the project to tomcat

  8. Start the tomcat service

  9. Open the browser test function

Implementation process

  1. Access to the specified resource path through a browser
  2. Find the corresponding name tag through url-pattern
  3. Find the corresponding servlet declaration by name
  4. Find the corresponding Java implementation class in the declaration
  5. Execute the service method

Recommended reading: [Java WEB] Servlet & Annotation Development

Guess you like

Origin blog.csdn.net/qq_24980365/article/details/123260695